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.27 1999/03/16 22:53:49 paulus Exp $";
39 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
44 #include <sys/errno.h>
48 #include <net/ppp_defs.h>
49 #include <net/if_ppp.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
55 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
56 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
57 static int ppp_fd = -1; /* fd which is set to PPP discipline */
59 static int restore_term; /* 1 => we've munged the terminal */
60 static struct termios inittermios; /* Initial TTY termios */
61 static struct winsize wsinfo; /* Initial window size info */
63 static char *lock_file; /* name of lock file created */
65 static int loop_slave = -1;
66 static int loop_master;
67 static char loop_name[20];
69 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
71 static int sockfd; /* socket for doing interface ioctls */
73 static fd_set in_fds; /* set of fds that wait_input waits for */
74 static int max_in_fd; /* highest fd set in in_fds */
76 static int if_is_up; /* the interface is currently up */
77 static u_int32_t ifaddrs[2]; /* local and remote addresses */
78 static u_int32_t default_route_gateway; /* gateway addr for default route */
79 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
81 /* Prototypes for procedures local to this file. */
82 static int translate_speed __P((int));
83 static int baud_rate_of __P((int));
84 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
88 * sys_init - System-dependent initialization.
93 /* Get an internet socket for doing socket ioctl's on. */
94 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
95 error("Couldn't create IP socket: %m");
104 * sys_cleanup - restore any system state we modified before exiting:
105 * mark the interface down, delete default route and/or proxy arp entry.
106 * This should call die() because it's called from die().
114 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
115 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
116 && ((ifr.ifr_flags & IFF_UP) != 0)) {
117 ifr.ifr_flags &= ~IFF_UP;
118 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
122 cifaddr(0, ifaddrs[0], ifaddrs[1]);
123 if (default_route_gateway)
124 cifdefaultroute(0, 0, default_route_gateway);
126 cifproxyarp(0, proxy_arp_addr);
130 * sys_close - Clean up in a child process before execing.
136 if (loop_slave >= 0) {
143 * sys_check_options - check the options that the user specified
149 option_error("Sorry - demand-dialling is not supported under Ultrix\n");
157 * daemon - Detach us from the terminal session.
160 daemon(nochdir, noclose)
161 int nochdir, noclose;
165 if ((pid = fork()) < 0)
168 exit(0); /* parent dies */
173 fclose(stdin); /* don't need stdin, stdout, stderr */
181 * ppp_available - check whether the system has any ppp interfaces
182 * (in fact we check whether we can do an ioctl on ppp0).
189 extern char *no_ppp_msg;
191 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
192 return 1; /* can't tell */
194 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), "ppp0");
195 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
199 This system lacks kernel support for PPP. To include PPP support\n\
200 in the kernel, please follow the steps detailed in the README.ultrix\n\
201 file in the ppp-2.2 distribution.\n";
206 * establish_ppp - Turn the serial port into a ppp interface.
212 int pppdisc = PPPDISC;
216 * Save the old line discipline of fd, and set it to PPP.
218 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
219 error("ioctl(TIOCGETD): %m");
222 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
223 error("ioctl(TIOCSETD): %m");
228 * Find out which interface we were given.
230 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
231 error("ioctl(PPPIOCGUNIT): %m");
238 * Enable debug in the driver if requested.
241 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
242 warn("ioctl (PPPIOCGFLAGS): %m");
244 x |= (kdebugflag & 0xFF) * SC_DEBUG;
245 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
246 warn("ioctl(PPPIOCSFLAGS): %m");
251 * Set device for non-blocking reads.
253 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
254 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
255 warn("Couldn't set device to non-blocking mode: %m");
262 * restore_loop - reattach the ppp unit to the loopback.
270 * disestablish_ppp - Restore the serial port to normal operation.
271 * This shouldn't call die() because it's called from die().
277 /* Reset non-blocking mode on fd. */
278 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
279 warn("Couldn't restore device fd flags: %m");
282 /* Restore old line discipline. */
283 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
284 error("ioctl(TIOCSETD): %m");
292 * Check whether the link seems not to be 8-bit clean.
300 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
302 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
304 s = "bit 7 set to 1";
307 s = "bit 7 set to 0";
317 warn("Serial link is not 8-bit clean:");
318 warn("All received characters had %s", s);
325 * List of valid speeds.
328 int speed_int, speed_val;
400 * Translate from bits/second to a speed_t.
406 struct speed *speedp;
410 for (speedp = speeds; speedp->speed_int; speedp++)
411 if (bps == speedp->speed_int)
412 return speedp->speed_val;
413 warn("speed %d not supported", bps);
418 * Translate from a speed_t to bits/second.
424 struct speed *speedp;
428 for (speedp = speeds; speedp->speed_int; speedp++)
429 if (speed == speedp->speed_val)
430 return speedp->speed_int;
435 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
436 * at the requested speed, etc. If `local' is true, set CLOCAL
437 * regardless of whether the modem option was specified.
440 set_up_tty(fd, local)
446 if (tcgetattr(fd, &tios) < 0) {
447 error("tcgetattr: %m");
453 ioctl(fd, TIOCGWINSZ, &wsinfo);
456 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
458 if (crtscts > 0 && !local)
459 tios.c_cflag |= CRTSCTS;
460 else if (crtscts < 0)
461 tios.c_cflag &= ~CRTSCTS;
464 tios.c_cflag |= CS8 | CREAD | HUPCL;
466 tios.c_cflag |= CLOCAL;
467 tios.c_iflag = IGNBRK | IGNPAR;
471 tios.c_cc[VTIME] = 0;
474 tios.c_iflag |= IXON | IXOFF;
475 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
476 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
479 speed = translate_speed(inspeed);
481 cfsetospeed(&tios, speed);
482 cfsetispeed(&tios, speed);
484 speed = cfgetospeed(&tios);
486 * We can't proceed if the serial port speed is B0,
487 * since that implies that the serial port is disabled.
490 error("Baud rate for %s is 0; need explicit baud rate",
496 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
497 error("tcsetattr: %m");
502 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
503 warn("TIOC(N)MODEM: %m");
504 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
505 warn("TIOC(N)CAR: %m");
507 baud_rate = inspeed = baud_rate_of(speed);
512 * restore_tty - restore the terminal to the saved settings.
519 if (!default_device) {
521 * Turn off echoing, because otherwise we can get into
522 * a loop with the tty and the modem echoing to each other.
523 * We presume we are the sole user of this tty device, so
524 * when we close it, it will revert to its defaults anyway.
526 inittermios.c_lflag &= ~(ECHO | ECHONL);
528 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
530 warn("tcsetattr: %m");
531 ioctl(fd, TIOCSWINSZ, &wsinfo);
532 restore_term = FALSE;
537 * setdtr - control the DTR line on the serial port.
538 * This is called from die(), so it shouldn't call die().
544 int modembits = TIOCM_DTR;
546 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
551 * open_ppp_loopback - open the device we use for getting
552 * packets in demand mode, and connect it to a ppp interface.
553 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
558 fatal("open_ppp_loopback called!");
559 /* return loop_master; */
564 * output - Output PPP packet.
573 dbglog("sent %P", p, len);
575 if (write(ttyfd, p, len) < 0) {
583 * wait_input - wait until there is data available,
584 * for the length of time specified by *timo (indefinite
589 struct timeval *timo;
595 n = select(ttyfd+1, &ready, NULL, &ready, timo);
596 if (n < 0 && errno != EINTR)
602 * add_fd - add an fd to the set that wait_input waits for.
612 * remove_fd - remove an fd from the set that wait_input waits for.
614 void remove_fd(int fd)
621 * wait_loop_output - wait until there is data available on the
622 * loopback, for the length of time specified by *timo (indefinite
626 wait_loop_output(timo)
627 struct timeval *timo;
634 * wait_time - wait for a given length of time or until a
635 * signal is received.
639 struct timeval *timo;
643 n = select(0, NULL, NULL, NULL, timo);
644 if (n < 0 && errno != EINTR) {
652 * read_packet - get a PPP packet from the serial device.
660 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
661 if (errno == EWOULDBLOCK || errno == EINTR)
663 error("read(fd): %m");
671 * get_loop_output - read characters from the loopback, form them
672 * into frames, and detect when we want to bring the real link up.
673 * Return value is 1 if we need to bring up the link, 0 otherwise.
683 * ppp_send_config - configure the transmit characteristics of
687 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
694 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0) {
695 error("ioctl(PPPIOCSMTU): %m");
699 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
700 error("ioctl(PPPIOCSASYNCMAP): %m");
704 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
705 error("ioctl (PPPIOCGFLAGS): %m");
708 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
709 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
710 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
711 error("ioctl(PPPIOCSFLAGS): %m");
718 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
721 ppp_set_xaccm(unit, accm)
725 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
726 warn("ioctl(set extended ACCM): %m");
731 * ppp_recv_config - configure the receive-side characteristics of
735 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
742 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
743 error("ioctl(PPPIOCSMRU): %m");
746 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
747 error("ioctl(PPPIOCSRASYNCMAP): %m");
750 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
751 error("ioctl (PPPIOCGFLAGS): %m");
754 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
755 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
756 error("ioctl(PPPIOCSFLAGS): %m");
762 * ccp_test - ask kernel whether a given compression method
763 * is acceptable for use. Returns 1 if the method and parameters
764 * are OK, 0 if the method is known but the parameters are not OK
765 * (e.g. code size should be reduced), or -1 if the method is unknown.
768 ccp_test(unit, opt_ptr, opt_len, for_transmit)
769 int unit, opt_len, for_transmit;
772 struct ppp_option_data data;
775 data.length = opt_len;
776 data.transmit = for_transmit;
777 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
779 return (errno == ENOBUFS)? 0: -1;
783 * ccp_flags_set - inform kernel about the current state of CCP.
786 ccp_flags_set(unit, isopen, isup)
787 int unit, isopen, isup;
791 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
792 error("ioctl (PPPIOCGFLAGS): %m");
795 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
796 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
797 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
798 error("ioctl(PPPIOCSFLAGS): %m");
802 * ccp_fatal_error - returns 1 if decompression was disabled as a
803 * result of an error detected after decompression of a packet,
804 * 0 otherwise. This is necessary because of patent nonsense.
807 ccp_fatal_error(unit)
812 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
813 error("ioctl(PPPIOCGFLAGS): %m");
816 return x & SC_DC_FERROR;
820 * get_idle_time - return how long the link has been idle.
827 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
832 * sifvjcomp - config tcp header compression
835 sifvjcomp(u, vjcomp, cidcomp, maxcid)
836 int u, vjcomp, cidcomp, maxcid;
840 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
841 error("ioctl (PPPIOCGFLAGS): %m");
844 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
845 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
846 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
847 error("ioctl(PPPIOCSFLAGS): %m");
850 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
851 error("ioctl(PPPIOCSFLAGS): %m");
858 * sifup - Config the interface up and enable IP packets to pass.
866 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
867 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
868 error("ioctl (SIOCGIFFLAGS): %m");
871 ifr.ifr_flags |= IFF_UP;
872 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
873 error("ioctl(SIOCSIFFLAGS): %m");
881 * sifnpmode - Set the mode for handling packets for a given NP.
884 sifnpmode(u, proto, mode)
891 npi.protocol = proto;
893 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
894 error("ioctl(set NP %d mode to %d): %m", proto, mode);
901 * sifdown - Config the interface down and disable IP.
912 npi.protocol = PPP_IP;
913 npi.mode = NPMODE_ERROR;
914 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
915 /* ignore errors, because ppp_fd might have been closed by now. */
917 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
918 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
919 error("ioctl (SIOCGIFFLAGS): %m");
922 ifr.ifr_flags &= ~IFF_UP;
923 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
924 error("ioctl(SIOCSIFFLAGS): %m");
933 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
936 #define SET_SA_FAMILY(addr, family) \
937 BZERO((char *) &(addr), sizeof(addr)); \
938 addr.sa_family = (family);
941 * sifaddr - Config the interface IP addresses and netmask.
952 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
953 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
955 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
956 info("Setting interface mask to %s\n", ip_ntoa(m));
957 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
958 error("ioctl(SIOCSIFNETMASK): %m");
962 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
963 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
964 error("ioctl(SIOCSIFADDR): %m");
967 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
968 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
969 error("ioctl(SIOCSIFDSTADDR): %m");
978 * cifaddr - Clear the interface IP addresses, and delete routes
979 * through the interface if possible.
989 BZERO(&rt, sizeof(rt));
990 SET_SA_FAMILY(rt.rt_dst, AF_INET);
991 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
992 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
993 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
994 rt.rt_flags = RTF_HOST;
995 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
996 error("ioctl(SIOCDELRT): %m");
1003 * sifdefaultroute - assign a default route through the address given.
1006 sifdefaultroute(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, SIOCADDRT, &rt) < 0) {
1018 error("default route ioctl(SIOCADDRT): %m");
1021 default_route_gateway = g;
1026 * cifdefaultroute - delete a default route through the address given.
1029 cifdefaultroute(u, l, g)
1035 BZERO(&rt, sizeof(rt));
1036 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1037 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1038 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1039 rt.rt_flags = RTF_GATEWAY;
1040 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1041 warn("default route ioctl(SIOCDELRT): %m");
1042 default_route_gateway = 0;
1047 * sifproxyarp - Make a proxy ARP entry for the peer.
1050 sifproxyarp(unit, hisaddr)
1054 struct arpreq arpreq;
1056 BZERO(&arpreq, sizeof(arpreq));
1059 * Get the hardware address of an interface on the same subnet
1060 * as our local address.
1062 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1063 error("Cannot determine ethernet address for proxy ARP");
1067 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1068 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1069 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1070 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1071 error("Couldn't add proxy arp entry: %m");
1075 proxy_arp_addr = hisaddr;
1080 * cifproxyarp - Delete the proxy ARP entry for the peer.
1083 cifproxyarp(unit, hisaddr)
1087 struct arpreq arpreq;
1089 BZERO(&arpreq, sizeof(arpreq));
1090 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1091 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1092 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1093 warn("Couldn't delete proxy arp entry: %m");
1101 * get_ether_addr - get the hardware address of an interface on the
1102 * the same subnet as ipaddr.
1107 get_ether_addr(ipaddr, hwaddr)
1109 struct sockaddr *hwaddr;
1111 struct ifreq *ifr, *ifend;
1112 u_int32_t ina, mask;
1115 struct ifreq ifs[MAX_IFS];
1116 struct ifdevea ifdevea;
1118 ifc.ifc_len = sizeof(ifs);
1120 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1121 error("ioctl(SIOCGIFCONF): %m");
1126 * Scan through looking for an interface with an Internet
1127 * address on the same subnet as `ipaddr'.
1129 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1130 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1131 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1132 if (ifr->ifr_addr.sa_family == AF_INET) {
1133 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1134 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1136 * Check that the interface is up, and not point-to-point
1139 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1141 if ((ifreq.ifr_flags &
1142 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1143 != (IFF_UP|IFF_BROADCAST))
1146 * Get its netmask and check that it's on the right subnet.
1148 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1150 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1151 if ((ipaddr & mask) != (ina & mask))
1160 info("found interface %s for proxy arp", ifr->ifr_name);
1163 * Grab the physical address for this interface.
1165 strlcpy(ifdevea.ifr_name, sizeof(ifdevea.ifr_name), ifr->ifr_name);
1166 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1167 error("Couldn't get h/w address for %s: %m", ifr->ifr_name);
1171 hwaddr->sa_family = AF_UNSPEC;
1172 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1177 * Return user specified netmask, modified by any mask we might determine
1178 * for address `addr' (in network byte order).
1179 * Here we scan through the system's list of interfaces, looking for
1180 * any non-point-to-point interfaces which might appear to be on the same
1181 * network as `addr'. If we find any, we OR in their netmask to the
1182 * user-specified netmask.
1188 u_int32_t mask, nmask, ina;
1189 struct ifreq *ifr, *ifend, ifreq;
1191 struct ifreq ifs[MAX_IFS];
1194 if (IN_CLASSA(addr)) /* determine network mask for address class */
1195 nmask = IN_CLASSA_NET;
1196 else if (IN_CLASSB(addr))
1197 nmask = IN_CLASSB_NET;
1199 nmask = IN_CLASSC_NET;
1200 /* class D nets are disallowed by bad_ip_adrs */
1201 mask = netmask | htonl(nmask);
1204 * Scan through the system's network interfaces.
1206 ifc.ifc_len = sizeof(ifs);
1208 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1209 warn("ioctl(SIOCGIFCONF): %m");
1212 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1213 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1214 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1216 * Check the interface's internet address.
1218 if (ifr->ifr_addr.sa_family != AF_INET)
1220 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1221 if ((ntohl(ina) & nmask) != (addr & nmask))
1224 * Check that the interface is up, and not point-to-point or loopback.
1226 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1227 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1229 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1233 * Get its netmask and OR it into our mask.
1235 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1237 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1244 * have_route_to - determine if the system has any route to
1245 * a given IP address.
1246 * For demand mode to work properly, we have to ignore routes
1247 * through our own interface.
1249 int have_route_to(u_int32_t addr)
1256 * Use the hostid as part of the random number seed.
1266 Seems like strdup() is not part of string package in Ultrix.
1267 If I understood the man-page on the sun this should work.
1272 char *strdup( in ) char *in;
1275 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1276 (void) strcpy( dup, in );
1281 * This logwtmp() implementation is subject to the following copyright:
1283 * Copyright (c) 1988 The Regents of the University of California.
1284 * All rights reserved.
1286 * Redistribution and use in source and binary forms are permitted
1287 * provided that the above copyright notice and this paragraph are
1288 * duplicated in all such forms and that any documentation,
1289 * advertising materials, and other materials related to such
1290 * distribution and use acknowledge that the software was developed
1291 * by the University of California, Berkeley. The name of the
1292 * University may not be used to endorse or promote products derived
1293 * from this software without specific prior written permission.
1294 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1295 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1296 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1299 #define WTMPFILE "/usr/adm/wtmp"
1302 logwtmp(line, name, host)
1303 const char *line, *name, *host;
1309 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1311 if (!fstat(fd, &buf)) {
1312 strlcpy(ut.ut_line, sizeof(ut.ut_line), line);
1313 strlcpy(ut.ut_name, sizeof(ut.ut_name), name);
1314 strlcpy(ut.ut_host, sizeof(ut.ut_host), host);
1315 (void)time(&ut.ut_time);
1316 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1317 (void)ftruncate(fd, buf.st_size);
1323 * Routines for locking and unlocking the serial device, moved here
1327 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1330 * lock - create a lock file for the named device
1340 if ((p = strrchr(dev, '/')) != NULL)
1342 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1343 lock_file = malloc(l);
1344 if (lock_file == NULL)
1345 novm("lock file name");
1346 slprintf(lock_file, l, "%s%s, LOCK_PREFIX, dev);
1348 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1350 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1351 /* Read the lock file to find out who has the device locked */
1352 n = read(fd, &pid, sizeof(pid));
1354 error("Can't read pid from lock file %s", lock_file);
1357 if (kill(pid, 0) == -1 && errno == ESRCH) {
1358 /* pid no longer exists - remove the lock file */
1359 if (unlink(lock_file) == 0) {
1361 notice("Removed stale lock on %s (pid %d)",
1365 warn("Couldn't remove stale lock on %s",
1368 notice("Device %s is locked by pid %d",
1373 error("Can't create lock file %s: %m", lock_file);
1380 write(fd, &pid, sizeof pid);
1387 * unlock - remove our lockfile