2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
5 * Copyright (c) 1989 Carnegie Mellon University.
6 * Copyright (c) 1995 The Australian National University.
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 and The Australian National University.
15 * The names of the Universities may not be used to endorse or promote
16 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 static char rcsid[] = "$Id: sys-ultrix.c,v 1.23 1998/11/07 06:59:32 paulus Exp $";
40 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
45 #include <sys/errno.h>
49 #include <net/ppp_defs.h>
50 #include <net/if_ppp.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
56 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
57 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
58 static int ppp_fd = -1; /* fd which is set to PPP discipline */
60 static int restore_term; /* 1 => we've munged the terminal */
61 static struct termios inittermios; /* Initial TTY termios */
62 static struct winsize wsinfo; /* Initial window size info */
64 static char *lock_file; /* name of lock file created */
66 static int loop_slave = -1;
67 static int loop_master;
68 static char loop_name[20];
70 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
72 static int sockfd; /* socket for doing interface ioctls */
74 static int if_is_up; /* the interface is currently up */
75 static u_int32_t ifaddrs[2]; /* local and remote addresses */
76 static u_int32_t default_route_gateway; /* gateway addr for default route */
77 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
79 /* Prototypes for procedures local to this file. */
80 static int translate_speed __P((int));
81 static int baud_rate_of __P((int));
82 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
86 * sys_init - System-dependent initialization.
91 /* Get an internet socket for doing socket ioctl's on. */
92 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
93 syslog(LOG_ERR, "Couldn't create IP socket: %m");
99 * sys_cleanup - restore any system state we modified before exiting:
100 * mark the interface down, delete default route and/or proxy arp entry.
101 * This should call die() because it's called from die().
109 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
110 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
111 && ((ifr.ifr_flags & IFF_UP) != 0)) {
112 ifr.ifr_flags &= ~IFF_UP;
113 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
117 cifaddr(0, ifaddrs[0], ifaddrs[1]);
118 if (default_route_gateway)
119 cifdefaultroute(0, 0, default_route_gateway);
121 cifproxyarp(0, proxy_arp_addr);
125 * sys_close - Clean up in a child process before execing.
131 if (loop_slave >= 0) {
138 * sys_check_options - check the options that the user specified
144 option_error("Sorry - demand-dialling is not supported under Ultrix\n");
152 * daemon - Detach us from the terminal session.
155 daemon(nochdir, noclose)
156 int nochdir, noclose;
160 if ((pid = fork()) < 0)
163 exit(0); /* parent dies */
168 fclose(stdin); /* don't need stdin, stdout, stderr */
176 * ppp_available - check whether the system has any ppp interfaces
177 * (in fact we check whether we can do an ioctl on ppp0).
184 extern char *no_ppp_msg;
186 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
187 return 1; /* can't tell */
189 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
190 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
194 This system lacks kernel support for PPP. To include PPP support\n\
195 in the kernel, please follow the steps detailed in the README.ultrix\n\
196 file in the ppp-2.2 distribution.\n";
201 * establish_ppp - Turn the serial port into a ppp interface.
207 int pppdisc = PPPDISC;
211 * Save the old line discipline of fd, and set it to PPP.
213 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
214 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
217 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
218 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
223 * Find out which interface we were given.
225 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
226 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
233 * Enable debug in the driver if requested.
236 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
237 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
239 x |= (kdebugflag & 0xFF) * SC_DEBUG;
240 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
241 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
246 * Set device for non-blocking reads.
248 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
249 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
250 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
255 * restore_loop - reattach the ppp unit to the loopback.
263 * disestablish_ppp - Restore the serial port to normal operation.
264 * This shouldn't call die() because it's called from die().
270 /* Reset non-blocking mode on fd. */
271 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
272 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
275 /* Restore old line discipline. */
276 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
277 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
285 * Check whether the link seems not to be 8-bit clean.
293 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
295 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
297 s = "bit 7 set to 1";
300 s = "bit 7 set to 0";
310 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
311 syslog(LOG_WARNING, "All received characters had %s", s);
318 * List of valid speeds.
321 int speed_int, speed_val;
393 * Translate from bits/second to a speed_t.
399 struct speed *speedp;
403 for (speedp = speeds; speedp->speed_int; speedp++)
404 if (bps == speedp->speed_int)
405 return speedp->speed_val;
406 syslog(LOG_WARNING, "speed %d not supported", bps);
411 * Translate from a speed_t to bits/second.
417 struct speed *speedp;
421 for (speedp = speeds; speedp->speed_int; speedp++)
422 if (speed == speedp->speed_val)
423 return speedp->speed_int;
428 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
429 * at the requested speed, etc. If `local' is true, set CLOCAL
430 * regardless of whether the modem option was specified.
433 set_up_tty(fd, local)
439 if (tcgetattr(fd, &tios) < 0) {
440 syslog(LOG_ERR, "tcgetattr: %m");
446 ioctl(fd, TIOCGWINSZ, &wsinfo);
449 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
451 if (crtscts > 0 && !local)
452 tios.c_cflag |= CRTSCTS;
453 else if (crtscts < 0)
454 tios.c_cflag &= ~CRTSCTS;
457 tios.c_cflag |= CS8 | CREAD | HUPCL;
459 tios.c_cflag |= CLOCAL;
460 tios.c_iflag = IGNBRK | IGNPAR;
464 tios.c_cc[VTIME] = 0;
467 tios.c_iflag |= IXON | IXOFF;
468 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
469 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
472 speed = translate_speed(inspeed);
474 cfsetospeed(&tios, speed);
475 cfsetispeed(&tios, speed);
477 speed = cfgetospeed(&tios);
479 * We can't proceed if the serial port speed is B0,
480 * since that implies that the serial port is disabled.
483 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
489 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
490 syslog(LOG_ERR, "tcsetattr: %m");
495 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
496 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
497 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
498 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
500 baud_rate = inspeed = baud_rate_of(speed);
505 * restore_tty - restore the terminal to the saved settings.
512 if (!default_device) {
514 * Turn off echoing, because otherwise we can get into
515 * a loop with the tty and the modem echoing to each other.
516 * We presume we are the sole user of this tty device, so
517 * when we close it, it will revert to its defaults anyway.
519 inittermios.c_lflag &= ~(ECHO | ECHONL);
521 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
523 syslog(LOG_WARNING, "tcsetattr: %m");
524 ioctl(fd, TIOCSWINSZ, &wsinfo);
525 restore_term = FALSE;
530 * setdtr - control the DTR line on the serial port.
531 * This is called from die(), so it shouldn't call die().
537 int modembits = TIOCM_DTR;
539 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
544 * open_ppp_loopback - open the device we use for getting
545 * packets in demand mode, and connect it to a ppp interface.
546 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
551 syslog(LOG_ERR, "open_ppp_loopback called!");
557 * output - Output PPP packet.
566 log_packet(p, len, "sent ", LOG_DEBUG);
568 if (write(ttyfd, p, len) < 0) {
570 syslog(LOG_ERR, "write: %m");
576 * wait_input - wait until there is data available on ttyfd,
577 * for the length of time specified by *timo (indefinite
582 struct timeval *timo;
588 FD_SET(ttyfd, &ready);
589 n = select(ttyfd+1, &ready, NULL, &ready, timo);
590 if (n < 0 && errno != EINTR) {
591 syslog(LOG_ERR, "select: %m");
598 * wait_loop_output - wait until there is data available on the
599 * loopback, for the length of time specified by *timo (indefinite
603 wait_loop_output(timo)
604 struct timeval *timo;
611 * wait_time - wait for a given length of time or until a
612 * signal is received.
616 struct timeval *timo;
620 n = select(0, NULL, NULL, NULL, timo);
621 if (n < 0 && errno != EINTR) {
622 syslog(LOG_ERR, "select: %m");
629 * read_packet - get a PPP packet from the serial device.
637 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
638 if (errno == EWOULDBLOCK || errno == EINTR)
640 syslog(LOG_ERR, "read(fd): %m");
648 * get_loop_output - read characters from the loopback, form them
649 * into frames, and detect when we want to bring the real link up.
650 * Return value is 1 if we need to bring up the link, 0 otherwise.
660 * ppp_send_config - configure the transmit characteristics of
664 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
671 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0) {
672 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
676 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
677 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
681 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
682 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
685 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
686 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
687 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
688 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
695 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
698 ppp_set_xaccm(unit, accm)
702 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
703 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
708 * ppp_recv_config - configure the receive-side characteristics of
712 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
719 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
720 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
723 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
724 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
727 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
728 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
731 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
732 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
733 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
739 * ccp_test - ask kernel whether a given compression method
740 * is acceptable for use. Returns 1 if the method and parameters
741 * are OK, 0 if the method is known but the parameters are not OK
742 * (e.g. code size should be reduced), or -1 if the method is unknown.
745 ccp_test(unit, opt_ptr, opt_len, for_transmit)
746 int unit, opt_len, for_transmit;
749 struct ppp_option_data data;
752 data.length = opt_len;
753 data.transmit = for_transmit;
754 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
756 return (errno == ENOBUFS)? 0: -1;
760 * ccp_flags_set - inform kernel about the current state of CCP.
763 ccp_flags_set(unit, isopen, isup)
764 int unit, isopen, isup;
768 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
769 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
772 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
773 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
774 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
775 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
779 * ccp_fatal_error - returns 1 if decompression was disabled as a
780 * result of an error detected after decompression of a packet,
781 * 0 otherwise. This is necessary because of patent nonsense.
784 ccp_fatal_error(unit)
789 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
790 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
793 return x & SC_DC_FERROR;
797 * get_idle_time - return how long the link has been idle.
804 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
809 * sifvjcomp - config tcp header compression
812 sifvjcomp(u, vjcomp, cidcomp, maxcid)
813 int u, vjcomp, cidcomp, maxcid;
817 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
818 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
821 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
822 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
823 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
824 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
827 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
828 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
835 * sifup - Config the interface up and enable IP packets to pass.
843 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
844 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
845 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
848 ifr.ifr_flags |= IFF_UP;
849 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
850 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
858 * sifnpmode - Set the mode for handling packets for a given NP.
861 sifnpmode(u, proto, mode)
868 npi.protocol = proto;
870 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
871 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
878 * sifdown - Config the interface down and disable IP.
889 npi.protocol = PPP_IP;
890 npi.mode = NPMODE_ERROR;
891 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
892 /* ignore errors, because ppp_fd might have been closed by now. */
894 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
895 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
896 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
899 ifr.ifr_flags &= ~IFF_UP;
900 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
901 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
910 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
913 #define SET_SA_FAMILY(addr, family) \
914 BZERO((char *) &(addr), sizeof(addr)); \
915 addr.sa_family = (family);
918 * sifaddr - Config the interface IP addresses and netmask.
929 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
930 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
932 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
933 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
934 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
935 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
939 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
940 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
941 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
944 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
945 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
946 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
955 * cifaddr - Clear the interface IP addresses, and delete routes
956 * through the interface if possible.
966 BZERO(&rt, sizeof(rt));
967 SET_SA_FAMILY(rt.rt_dst, AF_INET);
968 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
969 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
970 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
971 rt.rt_flags = RTF_HOST;
972 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
973 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
980 * sifdefaultroute - assign a default route through the address given.
983 sifdefaultroute(u, l, g)
989 BZERO(&rt, sizeof(rt));
990 SET_SA_FAMILY(rt.rt_dst, AF_INET);
991 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
992 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
993 rt.rt_flags = RTF_GATEWAY;
994 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
995 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
998 default_route_gateway = g;
1003 * cifdefaultroute - delete a default route through the address given.
1006 cifdefaultroute(u, l, g)
1012 BZERO(&rt, sizeof(rt));
1013 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1014 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1015 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1016 rt.rt_flags = RTF_GATEWAY;
1017 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1018 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
1019 default_route_gateway = 0;
1024 * sifproxyarp - Make a proxy ARP entry for the peer.
1027 sifproxyarp(unit, hisaddr)
1031 struct arpreq arpreq;
1033 BZERO(&arpreq, sizeof(arpreq));
1036 * Get the hardware address of an interface on the same subnet
1037 * as our local address.
1039 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1040 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1044 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1045 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1046 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1047 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1048 syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1052 proxy_arp_addr = hisaddr;
1057 * cifproxyarp - Delete the proxy ARP entry for the peer.
1060 cifproxyarp(unit, hisaddr)
1064 struct arpreq arpreq;
1066 BZERO(&arpreq, sizeof(arpreq));
1067 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1068 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1069 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1070 syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1078 * get_ether_addr - get the hardware address of an interface on the
1079 * the same subnet as ipaddr.
1084 get_ether_addr(ipaddr, hwaddr)
1086 struct sockaddr *hwaddr;
1088 struct ifreq *ifr, *ifend;
1089 u_int32_t ina, mask;
1092 struct ifreq ifs[MAX_IFS];
1093 struct ifdevea ifdevea;
1095 ifc.ifc_len = sizeof(ifs);
1097 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1098 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1103 * Scan through looking for an interface with an Internet
1104 * address on the same subnet as `ipaddr'.
1106 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1107 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1108 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1109 if (ifr->ifr_addr.sa_family == AF_INET) {
1110 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1111 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1113 * Check that the interface is up, and not point-to-point
1116 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1118 if ((ifreq.ifr_flags &
1119 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1120 != (IFF_UP|IFF_BROADCAST))
1123 * Get its netmask and check that it's on the right subnet.
1125 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1127 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1128 if ((ipaddr & mask) != (ina & mask))
1137 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1140 * Grab the physical address for this interface.
1142 strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1143 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1144 syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1148 hwaddr->sa_family = AF_UNSPEC;
1149 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1154 * Return user specified netmask, modified by any mask we might determine
1155 * for address `addr' (in network byte order).
1156 * Here we scan through the system's list of interfaces, looking for
1157 * any non-point-to-point interfaces which might appear to be on the same
1158 * network as `addr'. If we find any, we OR in their netmask to the
1159 * user-specified netmask.
1165 u_int32_t mask, nmask, ina;
1166 struct ifreq *ifr, *ifend, ifreq;
1168 struct ifreq ifs[MAX_IFS];
1171 if (IN_CLASSA(addr)) /* determine network mask for address class */
1172 nmask = IN_CLASSA_NET;
1173 else if (IN_CLASSB(addr))
1174 nmask = IN_CLASSB_NET;
1176 nmask = IN_CLASSC_NET;
1177 /* class D nets are disallowed by bad_ip_adrs */
1178 mask = netmask | htonl(nmask);
1181 * Scan through the system's network interfaces.
1183 ifc.ifc_len = sizeof(ifs);
1185 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1186 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1189 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1190 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1191 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1193 * Check the interface's internet address.
1195 if (ifr->ifr_addr.sa_family != AF_INET)
1197 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1198 if ((ntohl(ina) & nmask) != (addr & nmask))
1201 * Check that the interface is up, and not point-to-point or loopback.
1203 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1204 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1206 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1210 * Get its netmask and OR it into our mask.
1212 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1214 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1222 * Use the hostid as part of the random number seed.
1232 Seems like strdup() is not part of string package in Ultrix.
1233 If I understood the man-page on the sun this should work.
1238 char *strdup( in ) char *in;
1241 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1242 (void) strcpy( dup, in );
1247 * This logwtmp() implementation is subject to the following copyright:
1249 * Copyright (c) 1988 The Regents of the University of California.
1250 * All rights reserved.
1252 * Redistribution and use in source and binary forms are permitted
1253 * provided that the above copyright notice and this paragraph are
1254 * duplicated in all such forms and that any documentation,
1255 * advertising materials, and other materials related to such
1256 * distribution and use acknowledge that the software was developed
1257 * by the University of California, Berkeley. The name of the
1258 * University may not be used to endorse or promote products derived
1259 * from this software without specific prior written permission.
1260 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1261 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1262 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1265 #define WTMPFILE "/usr/adm/wtmp"
1268 logwtmp(line, name, host)
1269 const char *line, *name, *host;
1275 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1277 if (!fstat(fd, &buf)) {
1278 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1279 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1280 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1281 (void)time(&ut.ut_time);
1282 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1283 (void)ftruncate(fd, buf.st_size);
1289 * Routines for locking and unlocking the serial device, moved here
1293 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1296 * lock - create a lock file for the named device
1305 if ((p = strrchr(dev, '/')) != NULL)
1307 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1308 if (lock_file == NULL)
1309 novm("lock file name");
1310 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1312 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1314 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1315 /* Read the lock file to find out who has the device locked */
1316 n = read(fd, &pid, sizeof(pid));
1318 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1321 if (kill(pid, 0) == -1 && errno == ESRCH) {
1322 /* pid no longer exists - remove the lock file */
1323 if (unlink(lock_file) == 0) {
1325 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1329 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1332 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1337 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1344 write(fd, &pid, sizeof pid);
1351 * unlock - remove our lockfile