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.21 1997/04/30 05:59:38 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");
151 * daemon - Detach us from the terminal session.
154 daemon(nochdir, noclose)
155 int nochdir, noclose;
159 if ((pid = fork()) < 0)
162 exit(0); /* parent dies */
167 fclose(stdin); /* don't need stdin, stdout, stderr */
175 * ppp_available - check whether the system has any ppp interfaces
176 * (in fact we check whether we can do an ioctl on ppp0).
183 extern char *no_ppp_msg;
185 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
186 return 1; /* can't tell */
188 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
189 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
193 This system lacks kernel support for PPP. To include PPP support\n\
194 in the kernel, please follow the steps detailed in the README.ultrix\n\
195 file in the ppp-2.2 distribution.\n";
200 * establish_ppp - Turn the serial port into a ppp interface.
206 int pppdisc = PPPDISC;
210 * Save the old line discipline of fd, and set it to PPP.
212 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
213 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
216 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
217 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
222 * Find out which interface we were given.
224 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
225 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
232 * Enable debug in the driver if requested.
235 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
236 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
238 x |= (kdebugflag & 0xFF) * SC_DEBUG;
239 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
240 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
245 * Set device for non-blocking reads.
247 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
248 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
249 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
254 * restore_loop - reattach the ppp unit to the loopback.
262 * disestablish_ppp - Restore the serial port to normal operation.
263 * This shouldn't call die() because it's called from die().
269 /* Reset non-blocking mode on fd. */
270 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
271 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
274 /* Restore old line discipline. */
275 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
276 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
284 * Check whether the link seems not to be 8-bit clean.
292 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
294 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
296 s = "bit 7 set to 1";
299 s = "bit 7 set to 0";
309 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
310 syslog(LOG_WARNING, "All received characters had %s", s);
317 * List of valid speeds.
320 int speed_int, speed_val;
392 * Translate from bits/second to a speed_t.
398 struct speed *speedp;
402 for (speedp = speeds; speedp->speed_int; speedp++)
403 if (bps == speedp->speed_int)
404 return speedp->speed_val;
405 syslog(LOG_WARNING, "speed %d not supported", bps);
410 * Translate from a speed_t to bits/second.
416 struct speed *speedp;
420 for (speedp = speeds; speedp->speed_int; speedp++)
421 if (speed == speedp->speed_val)
422 return speedp->speed_int;
427 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
428 * at the requested speed, etc. If `local' is true, set CLOCAL
429 * regardless of whether the modem option was specified.
432 set_up_tty(fd, local)
438 if (tcgetattr(fd, &tios) < 0) {
439 syslog(LOG_ERR, "tcgetattr: %m");
445 ioctl(fd, TIOCGWINSZ, &wsinfo);
448 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
450 if (crtscts > 0 && !local)
451 tios.c_cflag |= CRTSCTS;
452 else if (crtscts < 0)
453 tios.c_cflag &= ~CRTSCTS;
456 tios.c_cflag |= CS8 | CREAD | HUPCL;
458 tios.c_cflag |= CLOCAL;
459 tios.c_iflag = IGNBRK | IGNPAR;
463 tios.c_cc[VTIME] = 0;
466 tios.c_iflag |= IXON | IXOFF;
467 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
468 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
471 speed = translate_speed(inspeed);
473 cfsetospeed(&tios, speed);
474 cfsetispeed(&tios, speed);
476 speed = cfgetospeed(&tios);
478 * We can't proceed if the serial port speed is B0,
479 * since that implies that the serial port is disabled.
482 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
488 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
489 syslog(LOG_ERR, "tcsetattr: %m");
494 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
495 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
496 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
497 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
499 baud_rate = inspeed = baud_rate_of(speed);
504 * restore_tty - restore the terminal to the saved settings.
511 if (!default_device) {
513 * Turn off echoing, because otherwise we can get into
514 * a loop with the tty and the modem echoing to each other.
515 * We presume we are the sole user of this tty device, so
516 * when we close it, it will revert to its defaults anyway.
518 inittermios.c_lflag &= ~(ECHO | ECHONL);
520 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
522 syslog(LOG_WARNING, "tcsetattr: %m");
523 ioctl(fd, TIOCSWINSZ, &wsinfo);
524 restore_term = FALSE;
529 * setdtr - control the DTR line on the serial port.
530 * This is called from die(), so it shouldn't call die().
536 int modembits = TIOCM_DTR;
538 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
543 * open_ppp_loopback - open the device we use for getting
544 * packets in demand mode, and connect it to a ppp interface.
545 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
550 syslog(LOG_ERR, "open_ppp_loopback called!");
556 * output - Output PPP packet.
565 log_packet(p, len, "sent ", LOG_DEBUG);
567 if (write(ttyfd, p, len) < 0) {
569 syslog(LOG_ERR, "write: %m");
575 * wait_input - wait until there is data available on ttyfd,
576 * for the length of time specified by *timo (indefinite
581 struct timeval *timo;
587 FD_SET(ttyfd, &ready);
588 n = select(ttyfd+1, &ready, NULL, &ready, timo);
589 if (n < 0 && errno != EINTR) {
590 syslog(LOG_ERR, "select: %m");
597 * wait_loop_output - wait until there is data available on the
598 * loopback, for the length of time specified by *timo (indefinite
602 wait_loop_output(timo)
603 struct timeval *timo;
610 * wait_time - wait for a given length of time or until a
611 * signal is received.
615 struct timeval *timo;
619 n = select(0, NULL, NULL, NULL, timo);
620 if (n < 0 && errno != EINTR) {
621 syslog(LOG_ERR, "select: %m");
628 * read_packet - get a PPP packet from the serial device.
636 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
637 if (errno == EWOULDBLOCK || errno == EINTR)
639 syslog(LOG_ERR, "read(fd): %m");
647 * get_loop_output - read characters from the loopback, form them
648 * into frames, and detect when we want to bring the real link up.
649 * Return value is 1 if we need to bring up the link, 0 otherwise.
659 * ppp_send_config - configure the transmit characteristics of
663 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
670 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0) {
671 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
675 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
676 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
680 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
681 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
684 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
685 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
686 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
687 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
694 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
697 ppp_set_xaccm(unit, accm)
701 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
702 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
707 * ppp_recv_config - configure the receive-side characteristics of
711 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
718 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
719 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
722 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
723 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
726 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
727 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
730 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
731 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
732 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
738 * ccp_test - ask kernel whether a given compression method
739 * is acceptable for use. Returns 1 if the method and parameters
740 * are OK, 0 if the method is known but the parameters are not OK
741 * (e.g. code size should be reduced), or -1 if the method is unknown.
744 ccp_test(unit, opt_ptr, opt_len, for_transmit)
745 int unit, opt_len, for_transmit;
748 struct ppp_option_data data;
751 data.length = opt_len;
752 data.transmit = for_transmit;
753 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
755 return (errno == ENOBUFS)? 0: -1;
759 * ccp_flags_set - inform kernel about the current state of CCP.
762 ccp_flags_set(unit, isopen, isup)
763 int unit, isopen, isup;
767 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
768 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
771 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
772 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
773 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
774 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
778 * ccp_fatal_error - returns 1 if decompression was disabled as a
779 * result of an error detected after decompression of a packet,
780 * 0 otherwise. This is necessary because of patent nonsense.
783 ccp_fatal_error(unit)
788 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
789 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
792 return x & SC_DC_FERROR;
796 * get_idle_time - return how long the link has been idle.
803 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
808 * sifvjcomp - config tcp header compression
811 sifvjcomp(u, vjcomp, cidcomp, maxcid)
812 int u, vjcomp, cidcomp, maxcid;
816 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
817 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
820 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
821 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
822 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
823 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
826 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
827 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
834 * sifup - Config the interface up and enable IP packets to pass.
842 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
843 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
844 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
847 ifr.ifr_flags |= IFF_UP;
848 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
849 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
857 * sifnpmode - Set the mode for handling packets for a given NP.
860 sifnpmode(u, proto, mode)
867 npi.protocol = proto;
869 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
870 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
877 * sifdown - Config the interface down and disable IP.
888 npi.protocol = PPP_IP;
889 npi.mode = NPMODE_ERROR;
890 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
891 /* ignore errors, because ppp_fd might have been closed by now. */
893 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
894 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
895 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
898 ifr.ifr_flags &= ~IFF_UP;
899 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
900 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
909 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
912 #define SET_SA_FAMILY(addr, family) \
913 BZERO((char *) &(addr), sizeof(addr)); \
914 addr.sa_family = (family);
917 * sifaddr - Config the interface IP addresses and netmask.
928 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
929 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
931 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
932 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
933 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
934 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
938 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
939 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
940 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
943 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
944 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
945 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
954 * cifaddr - Clear the interface IP addresses, and delete routes
955 * through the interface if possible.
965 BZERO(&rt, sizeof(rt));
966 SET_SA_FAMILY(rt.rt_dst, AF_INET);
967 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
968 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
969 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
970 rt.rt_flags = RTF_HOST;
971 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
972 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
979 * sifdefaultroute - assign a default route through the address given.
982 sifdefaultroute(u, l, g)
988 BZERO(&rt, sizeof(rt));
989 SET_SA_FAMILY(rt.rt_dst, AF_INET);
990 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
991 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
992 rt.rt_flags = RTF_GATEWAY;
993 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
994 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
997 default_route_gateway = g;
1002 * cifdefaultroute - delete a default route through the address given.
1005 cifdefaultroute(u, l, g)
1011 BZERO(&rt, sizeof(rt));
1012 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1013 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1014 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1015 rt.rt_flags = RTF_GATEWAY;
1016 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1017 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
1018 default_route_gateway = 0;
1023 * sifproxyarp - Make a proxy ARP entry for the peer.
1026 sifproxyarp(unit, hisaddr)
1030 struct arpreq arpreq;
1032 BZERO(&arpreq, sizeof(arpreq));
1035 * Get the hardware address of an interface on the same subnet
1036 * as our local address.
1038 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1039 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1043 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1044 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1045 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1046 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1047 syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1051 proxy_arp_addr = hisaddr;
1056 * cifproxyarp - Delete the proxy ARP entry for the peer.
1059 cifproxyarp(unit, hisaddr)
1063 struct arpreq arpreq;
1065 BZERO(&arpreq, sizeof(arpreq));
1066 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1067 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1068 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1069 syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1077 * get_ether_addr - get the hardware address of an interface on the
1078 * the same subnet as ipaddr.
1083 get_ether_addr(ipaddr, hwaddr)
1085 struct sockaddr *hwaddr;
1087 struct ifreq *ifr, *ifend;
1088 u_int32_t ina, mask;
1091 struct ifreq ifs[MAX_IFS];
1092 struct ifdevea ifdevea;
1094 ifc.ifc_len = sizeof(ifs);
1096 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1097 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1102 * Scan through looking for an interface with an Internet
1103 * address on the same subnet as `ipaddr'.
1105 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1106 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1107 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1108 if (ifr->ifr_addr.sa_family == AF_INET) {
1109 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1110 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1112 * Check that the interface is up, and not point-to-point
1115 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1117 if ((ifreq.ifr_flags &
1118 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1119 != (IFF_UP|IFF_BROADCAST))
1122 * Get its netmask and check that it's on the right subnet.
1124 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1126 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1127 if ((ipaddr & mask) != (ina & mask))
1136 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1139 * Grab the physical address for this interface.
1141 strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1142 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1143 syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1147 hwaddr->sa_family = AF_UNSPEC;
1148 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1153 * Return user specified netmask, modified by any mask we might determine
1154 * for address `addr' (in network byte order).
1155 * Here we scan through the system's list of interfaces, looking for
1156 * any non-point-to-point interfaces which might appear to be on the same
1157 * network as `addr'. If we find any, we OR in their netmask to the
1158 * user-specified netmask.
1164 u_int32_t mask, nmask, ina;
1165 struct ifreq *ifr, *ifend, ifreq;
1167 struct ifreq ifs[MAX_IFS];
1170 if (IN_CLASSA(addr)) /* determine network mask for address class */
1171 nmask = IN_CLASSA_NET;
1172 else if (IN_CLASSB(addr))
1173 nmask = IN_CLASSB_NET;
1175 nmask = IN_CLASSC_NET;
1176 /* class D nets are disallowed by bad_ip_adrs */
1177 mask = netmask | htonl(nmask);
1180 * Scan through the system's network interfaces.
1182 ifc.ifc_len = sizeof(ifs);
1184 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1185 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1188 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1189 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1190 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1192 * Check the interface's internet address.
1194 if (ifr->ifr_addr.sa_family != AF_INET)
1196 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1197 if ((ntohl(ina) & nmask) != (addr & nmask))
1200 * Check that the interface is up, and not point-to-point or loopback.
1202 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1203 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1205 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1209 * Get its netmask and OR it into our mask.
1211 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1213 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1221 Seems like strdup() is not part of string package in Ultrix.
1222 If I understood the man-page on the sun this should work.
1227 char *strdup( in ) char *in;
1230 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1231 (void) strcpy( dup, in );
1236 * This logwtmp() implementation is subject to the following copyright:
1238 * Copyright (c) 1988 The Regents of the University of California.
1239 * All rights reserved.
1241 * Redistribution and use in source and binary forms are permitted
1242 * provided that the above copyright notice and this paragraph are
1243 * duplicated in all such forms and that any documentation,
1244 * advertising materials, and other materials related to such
1245 * distribution and use acknowledge that the software was developed
1246 * by the University of California, Berkeley. The name of the
1247 * University may not be used to endorse or promote products derived
1248 * from this software without specific prior written permission.
1249 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1250 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1251 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1254 #define WTMPFILE "/usr/adm/wtmp"
1257 logwtmp(line, name, host)
1258 const char *line, *name, *host;
1264 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1266 if (!fstat(fd, &buf)) {
1267 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1268 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1269 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1270 (void)time(&ut.ut_time);
1271 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1272 (void)ftruncate(fd, buf.st_size);
1278 * Routines for locking and unlocking the serial device, moved here
1282 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1285 * lock - create a lock file for the named device
1294 if ((p = strrchr(dev, '/')) != NULL)
1296 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1297 if (lock_file == NULL)
1298 novm("lock file name");
1299 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1301 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1303 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1304 /* Read the lock file to find out who has the device locked */
1305 n = read(fd, &pid, sizeof(pid));
1307 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1310 if (kill(pid, 0) == -1 && errno == ESRCH) {
1311 /* pid no longer exists - remove the lock file */
1312 if (unlink(lock_file) == 0) {
1314 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1318 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1321 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1326 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1333 write(fd, &pid, sizeof pid);
1340 * unlock - remove our lockfile