2 * sys-ultrix.c - System-dependent procedures for setting up
3 * PPP interfaces on Ultrix systems.
5 * Copyright (c) 1995-2002 Paul Mackerras. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. The name(s) of the authors of this software must not be used to
15 * endorse or promote products derived from this software without
16 * prior written permission.
18 * 3. Redistributions of any form whatsoever must retain the following
20 * "This product includes software developed by Paul Mackerras
21 * <paulus@samba.org>".
23 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
24 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
28 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
29 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 * Derived from main.c and pppd.h, which are:
33 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
47 * 3. The name "Carnegie Mellon University" must not be used to
48 * endorse or promote products derived from this software without
49 * prior written permission. For permission or any legal
50 * details, please contact
51 * Office of Technology Transfer
52 * Carnegie Mellon University
54 * Pittsburgh, PA 15213-3890
55 * (412) 268-4387, fax: (412) 268-7395
56 * tech-transfer@andrew.cmu.edu
58 * 4. Redistributions of any form whatsoever must retain the following
60 * "This product includes software developed by Computing Services
61 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
63 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
64 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
65 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
66 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
68 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
69 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
72 #define RCSID "$Id: sys-ultrix.c,v 1.35 2004/11/04 10:02:26 paulus Exp $"
86 #include <sys/types.h>
88 #include <sys/socket.h>
89 #include <sys/ioctl.h>
91 #include <sys/errno.h>
95 #include <net/ppp_defs.h>
96 #include <net/if_ppp.h>
97 #include <net/route.h>
98 #include <netinet/in.h>
102 static const char rcsid[] = RCSID;
104 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
105 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
106 static int ppp_fd = -1; /* fd which is set to PPP discipline */
108 static int restore_term; /* 1 => we've munged the terminal */
109 static struct termios inittermios; /* Initial TTY termios */
110 static struct winsize wsinfo; /* Initial window size info */
112 static char *lock_file; /* name of lock file created */
114 static int loop_slave = -1;
115 static int loop_master;
116 static char loop_name[20];
118 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
120 static int sockfd; /* socket for doing interface ioctls */
122 static fd_set in_fds; /* set of fds that wait_input waits for */
123 static int max_in_fd; /* highest fd set in in_fds */
125 static int if_is_up; /* the interface is currently up */
126 static u_int32_t ifaddrs[2]; /* local and remote addresses */
127 static u_int32_t default_route_gateway; /* gateway addr for default route */
128 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
130 /* Prototypes for procedures local to this file. */
131 static int translate_speed __P((int));
132 static int baud_rate_of __P((int));
133 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
137 * sys_init - System-dependent initialization.
142 /* Get an internet socket for doing socket ioctl's on. */
143 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
144 fatal("Couldn't create IP socket: %m");
151 * sys_cleanup - restore any system state we modified before exiting:
152 * mark the interface down, delete default route and/or proxy arp entry.
153 * This shouldn't call die() because it's called from die().
161 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
162 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
163 && ((ifr.ifr_flags & IFF_UP) != 0)) {
164 ifr.ifr_flags &= ~IFF_UP;
165 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
169 cifaddr(0, ifaddrs[0], ifaddrs[1]);
170 if (default_route_gateway)
171 cifdefaultroute(0, 0, default_route_gateway);
173 cifproxyarp(0, proxy_arp_addr);
177 * sys_close - Clean up in a child process before execing.
183 if (loop_slave >= 0) {
190 * sys_check_options - check the options that the user specified
196 option_error("Sorry - demand-dialling is not supported under Ultrix\n");
204 * daemon - Detach us from the terminal session.
207 daemon(nochdir, noclose)
208 int nochdir, noclose;
212 if ((pid = fork()) < 0)
215 exit(0); /* parent dies */
220 fclose(stdin); /* don't need stdin, stdout, stderr */
229 * ppp_available - check whether the system has any ppp interfaces
230 * (in fact we check whether we can do an ioctl on ppp0).
237 extern char *no_ppp_msg;
239 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
240 return 1; /* can't tell */
242 strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
243 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
247 This system lacks kernel support for PPP. To include PPP support\n\
248 in the kernel, please follow the steps detailed in the README.ultrix\n\
249 file in the ppp-2.2 distribution.\n";
254 * establish_ppp - Turn the serial port into a ppp interface.
260 int pppdisc = PPPDISC;
264 * Save the old line discipline of fd, and set it to PPP.
266 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
267 fatal("ioctl(TIOCGETD): %m");
268 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
269 fatal("ioctl(TIOCSETD): %m");
272 * Find out which interface we were given.
274 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
275 fatal("ioctl(PPPIOCGUNIT): %m");
280 * Enable debug in the driver if requested.
283 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
284 warn("ioctl (PPPIOCGFLAGS): %m");
286 x |= (kdebugflag & 0xFF) * SC_DEBUG;
287 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
288 warn("ioctl(PPPIOCSFLAGS): %m");
293 * Set device for non-blocking reads.
295 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
296 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
297 warn("Couldn't set device to non-blocking mode: %m");
304 * restore_loop - reattach the ppp unit to the loopback.
312 * disestablish_ppp - Restore the serial port to normal operation.
313 * This shouldn't call die() because it's called from die().
319 /* Reset non-blocking mode on fd. */
320 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
321 warn("Couldn't restore device fd flags: %m");
324 /* Restore old line discipline. */
325 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
326 error("ioctl(TIOCSETD): %m");
334 * Check whether the link seems not to be 8-bit clean.
342 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
344 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
346 s = "bit 7 set to 1";
349 s = "bit 7 set to 0";
359 warn("Serial link is not 8-bit clean:");
360 warn("All received characters had %s", s);
367 * List of valid speeds.
370 int speed_int, speed_val;
442 * Translate from bits/second to a speed_t.
448 struct speed *speedp;
452 for (speedp = speeds; speedp->speed_int; speedp++)
453 if (bps == speedp->speed_int)
454 return speedp->speed_val;
455 warn("speed %d not supported", bps);
460 * Translate from a speed_t to bits/second.
466 struct speed *speedp;
470 for (speedp = speeds; speedp->speed_int; speedp++)
471 if (speed == speedp->speed_val)
472 return speedp->speed_int;
477 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
478 * at the requested speed, etc. If `local' is true, set CLOCAL
479 * regardless of whether the modem option was specified.
482 set_up_tty(fd, local)
488 if (tcgetattr(fd, &tios) < 0)
489 fatal("tcgetattr: %m");
493 ioctl(fd, TIOCGWINSZ, &wsinfo);
496 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
498 if (crtscts > 0 && !local)
499 tios.c_cflag |= CRTSCTS;
500 else if (crtscts < 0)
501 tios.c_cflag &= ~CRTSCTS;
504 tios.c_cflag |= CS8 | CREAD | HUPCL;
506 tios.c_cflag |= CLOCAL;
507 tios.c_iflag = IGNBRK | IGNPAR;
511 tios.c_cc[VTIME] = 0;
514 tios.c_iflag |= IXON | IXOFF;
515 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
516 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
519 speed = translate_speed(inspeed);
521 cfsetospeed(&tios, speed);
522 cfsetispeed(&tios, speed);
524 speed = cfgetospeed(&tios);
526 * We can't proceed if the serial port speed is B0,
527 * since that implies that the serial port is disabled.
530 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
533 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
534 fatal("tcsetattr: %m");
537 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
538 warn("TIOC(N)MODEM: %m");
539 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
540 warn("TIOC(N)CAR: %m");
542 baud_rate = inspeed = baud_rate_of(speed);
547 * restore_tty - restore the terminal to the saved settings.
554 if (!default_device) {
556 * Turn off echoing, because otherwise we can get into
557 * a loop with the tty and the modem echoing to each other.
558 * We presume we are the sole user of this tty device, so
559 * when we close it, it will revert to its defaults anyway.
561 inittermios.c_lflag &= ~(ECHO | ECHONL);
563 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
565 warn("tcsetattr: %m");
566 ioctl(fd, TIOCSWINSZ, &wsinfo);
572 * setdtr - control the DTR line on the serial port.
573 * This is called from die(), so it shouldn't call die().
579 int modembits = TIOCM_DTR;
581 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
586 * open_ppp_loopback - open the device we use for getting
587 * packets in demand mode, and connect it to a ppp interface.
588 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
593 fatal("open_ppp_loopback called!");
594 /* return loop_master; */
599 * output - Output PPP packet.
608 dbglog("sent %P", p, len);
610 if (write(ttyfd, p, len) < 0) {
618 * wait_input - wait until there is data available,
619 * for the length of time specified by *timo (indefinite
624 struct timeval *timo;
630 n = select(ttyfd+1, &ready, NULL, &ready, timo);
631 if (n < 0 && errno != EINTR)
637 * add_fd - add an fd to the set that wait_input waits for.
648 * remove_fd - remove an fd from the set that wait_input waits for.
658 * wait_loop_output - wait until there is data available on the
659 * loopback, for the length of time specified by *timo (indefinite
663 wait_loop_output(timo)
664 struct timeval *timo;
671 * wait_time - wait for a given length of time or until a
672 * signal is received.
676 struct timeval *timo;
680 n = select(0, NULL, NULL, NULL, timo);
681 if (n < 0 && errno != EINTR)
687 * read_packet - get a PPP packet from the serial device.
695 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
696 if (errno == EWOULDBLOCK || errno == EINTR)
698 fatal("read(fd): %m");
705 * get_loop_output - read characters from the loopback, form them
706 * into frames, and detect when we want to bring the real link up.
707 * Return value is 1 if we need to bring up the link, 0 otherwise.
717 * ppp_send_config - configure the transmit characteristics of
721 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
728 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0)
729 fatal("ioctl(PPPIOCSMTU): %m");
731 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
732 fatal("ioctl(PPPIOCSASYNCMAP): %m");
734 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
735 fatal("ioctl (PPPIOCGFLAGS): %m");
736 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
737 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
738 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
739 fatal("ioctl(PPPIOCSFLAGS): %m");
744 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
747 ppp_set_xaccm(unit, accm)
751 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
752 warn("ioctl(set extended ACCM): %m");
757 * ppp_recv_config - configure the receive-side characteristics of
761 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
768 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
769 fatal("ioctl(PPPIOCSMRU): %m");
770 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
771 fatal("ioctl(PPPIOCSRASYNCMAP): %m");
772 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
773 fatal("ioctl (PPPIOCGFLAGS): %m");
774 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
775 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
776 fatal("ioctl(PPPIOCSFLAGS): %m");
780 * ccp_test - ask kernel whether a given compression method
781 * is acceptable for use. Returns 1 if the method and parameters
782 * are OK, 0 if the method is known but the parameters are not OK
783 * (e.g. code size should be reduced), or -1 if the method is unknown.
786 ccp_test(unit, opt_ptr, opt_len, for_transmit)
787 int unit, opt_len, for_transmit;
790 struct ppp_option_data data;
793 data.length = opt_len;
794 data.transmit = for_transmit;
795 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
797 return (errno == ENOBUFS)? 0: -1;
801 * ccp_flags_set - inform kernel about the current state of CCP.
804 ccp_flags_set(unit, isopen, isup)
805 int unit, isopen, isup;
809 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
810 error("ioctl (PPPIOCGFLAGS): %m");
813 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
814 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
815 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
816 error("ioctl(PPPIOCSFLAGS): %m");
820 * ccp_fatal_error - returns 1 if decompression was disabled as a
821 * result of an error detected after decompression of a packet,
822 * 0 otherwise. This is necessary because of patent nonsense.
825 ccp_fatal_error(unit)
830 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
831 error("ioctl(PPPIOCGFLAGS): %m");
834 return x & SC_DC_FERROR;
838 * get_idle_time - return how long the link has been idle.
845 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
849 * get_ppp_stats - return statistics for the link.
852 get_ppp_stats(u, stats)
854 struct pppd_stats *stats;
856 struct ifpppstatsreq req;
858 memset (&req, 0, sizeof (req));
859 strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
860 if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
861 error("Couldn't get PPP statistics: %m");
864 stats->bytes_in = req.stats.p.ppp_ibytes;
865 stats->bytes_out = req.stats.p.ppp_obytes;
871 * sifvjcomp - config tcp header compression
874 sifvjcomp(u, vjcomp, cidcomp, maxcid)
875 int u, vjcomp, cidcomp, maxcid;
879 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
880 error("ioctl (PPPIOCGFLAGS): %m");
883 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
884 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
885 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
886 error("ioctl(PPPIOCSFLAGS): %m");
889 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
890 error("ioctl(PPPIOCSFLAGS): %m");
897 * sifup - Config the interface up and enable IP packets to pass.
905 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
906 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
907 error("ioctl (SIOCGIFFLAGS): %m");
910 ifr.ifr_flags |= IFF_UP;
911 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
912 error("ioctl(SIOCSIFFLAGS): %m");
920 * sifnpmode - Set the mode for handling packets for a given NP.
923 sifnpmode(u, proto, mode)
930 npi.protocol = proto;
932 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
933 error("ioctl(set NP %d mode to %d): %m", proto, mode);
940 * sifdown - Config the interface down and disable IP.
951 npi.protocol = PPP_IP;
952 npi.mode = NPMODE_ERROR;
953 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
954 /* ignore errors, because ppp_fd might have been closed by now. */
956 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
957 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
958 error("ioctl (SIOCGIFFLAGS): %m");
961 ifr.ifr_flags &= ~IFF_UP;
962 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
963 error("ioctl(SIOCSIFFLAGS): %m");
972 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
975 #define SET_SA_FAMILY(addr, family) \
976 BZERO((char *) &(addr), sizeof(addr)); \
977 addr.sa_family = (family);
980 * sifaddr - Config the interface IP addresses and netmask.
991 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
992 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
994 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
995 info("Setting interface mask to %s\n", ip_ntoa(m));
996 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
997 error("ioctl(SIOCSIFNETMASK): %m");
1001 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1002 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1003 error("ioctl(SIOCSIFADDR): %m");
1006 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1007 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1008 error("ioctl(SIOCSIFDSTADDR): %m");
1017 * cifaddr - Clear the interface IP addresses, and delete routes
1018 * through the interface if possible.
1028 BZERO(&rt, sizeof(rt));
1029 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1030 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1031 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1032 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1033 rt.rt_flags = RTF_HOST;
1034 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1035 error("ioctl(SIOCDELRT): %m");
1042 * sifdefaultroute - assign a default route through the address given.
1045 sifdefaultroute(u, l, g)
1051 BZERO(&rt, sizeof(rt));
1052 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1053 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1054 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1055 rt.rt_flags = RTF_GATEWAY;
1056 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1057 error("default route ioctl(SIOCADDRT): %m");
1060 default_route_gateway = g;
1065 * cifdefaultroute - delete a default route through the address given.
1068 cifdefaultroute(u, l, g)
1074 BZERO(&rt, sizeof(rt));
1075 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1076 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1077 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1078 rt.rt_flags = RTF_GATEWAY;
1079 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1080 warn("default route ioctl(SIOCDELRT): %m");
1081 default_route_gateway = 0;
1086 * sifproxyarp - Make a proxy ARP entry for the peer.
1089 sifproxyarp(unit, hisaddr)
1093 struct arpreq arpreq;
1095 BZERO(&arpreq, sizeof(arpreq));
1098 * Get the hardware address of an interface on the same subnet
1099 * as our local address.
1101 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1102 error("Cannot determine ethernet address for proxy ARP");
1106 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1107 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1108 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1109 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1110 error("Couldn't add proxy arp entry: %m");
1114 proxy_arp_addr = hisaddr;
1119 * cifproxyarp - Delete the proxy ARP entry for the peer.
1122 cifproxyarp(unit, hisaddr)
1126 struct arpreq arpreq;
1128 BZERO(&arpreq, sizeof(arpreq));
1129 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1130 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1131 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1132 warn("Couldn't delete proxy arp entry: %m");
1140 * get_ether_addr - get the hardware address of an interface on the
1141 * the same subnet as ipaddr.
1146 get_ether_addr(ipaddr, hwaddr)
1148 struct sockaddr *hwaddr;
1150 struct ifreq *ifr, *ifend;
1151 u_int32_t ina, mask;
1154 struct ifreq ifs[MAX_IFS];
1155 struct ifdevea ifdevea;
1157 ifc.ifc_len = sizeof(ifs);
1159 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1160 error("ioctl(SIOCGIFCONF): %m");
1165 * Scan through looking for an interface with an Internet
1166 * address on the same subnet as `ipaddr'.
1168 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1169 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1170 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1171 if (ifr->ifr_addr.sa_family == AF_INET) {
1172 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1173 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1175 * Check that the interface is up, and not point-to-point
1178 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1180 if ((ifreq.ifr_flags &
1181 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1182 != (IFF_UP|IFF_BROADCAST))
1185 * Get its netmask and check that it's on the right subnet.
1187 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1189 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1190 if ((ipaddr & mask) != (ina & mask))
1199 info("found interface %s for proxy arp", ifr->ifr_name);
1202 * Grab the physical address for this interface.
1204 strlcpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1205 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1206 error("Couldn't get h/w address for %s: %m", ifr->ifr_name);
1210 hwaddr->sa_family = AF_UNSPEC;
1211 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1216 * Return user specified netmask, modified by any mask we might determine
1217 * for address `addr' (in network byte order).
1218 * Here we scan through the system's list of interfaces, looking for
1219 * any non-point-to-point interfaces which might appear to be on the same
1220 * network as `addr'. If we find any, we OR in their netmask to the
1221 * user-specified netmask.
1227 u_int32_t mask, nmask, ina;
1228 struct ifreq *ifr, *ifend, ifreq;
1230 struct ifreq ifs[MAX_IFS];
1233 if (IN_CLASSA(addr)) /* determine network mask for address class */
1234 nmask = IN_CLASSA_NET;
1235 else if (IN_CLASSB(addr))
1236 nmask = IN_CLASSB_NET;
1238 nmask = IN_CLASSC_NET;
1239 /* class D nets are disallowed by bad_ip_adrs */
1240 mask = netmask | htonl(nmask);
1243 * Scan through the system's network interfaces.
1245 ifc.ifc_len = sizeof(ifs);
1247 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1248 warn("ioctl(SIOCGIFCONF): %m");
1251 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1252 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1253 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1255 * Check the interface's internet address.
1257 if (ifr->ifr_addr.sa_family != AF_INET)
1259 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1260 if ((ntohl(ina) & nmask) != (addr & nmask))
1263 * Check that the interface is up, and not point-to-point or loopback.
1265 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1266 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1268 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1272 * Get its netmask and OR it into our mask.
1274 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1276 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1283 * have_route_to - determine if the system has any route to
1284 * a given IP address.
1285 * For demand mode to work properly, we have to ignore routes
1286 * through our own interface.
1288 int have_route_to(u_int32_t addr)
1295 * Use the hostid as part of the random number seed.
1305 Seems like strdup() is not part of string package in Ultrix.
1306 If I understood the man-page on the sun this should work.
1311 char *strdup( in ) char *in;
1314 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1315 (void) strcpy( dup, in );
1320 * This logwtmp() implementation is subject to the following copyright:
1322 * Copyright (c) 1988 The Regents of the University of California.
1323 * All rights reserved.
1325 * Redistribution and use in source and binary forms are permitted
1326 * provided that the above copyright notice and this paragraph are
1327 * duplicated in all such forms and that any documentation,
1328 * advertising materials, and other materials related to such
1329 * distribution and use acknowledge that the software was developed
1330 * by the University of California, Berkeley. The name of the
1331 * University may not be used to endorse or promote products derived
1332 * from this software without specific prior written permission.
1333 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1334 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1335 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1338 #define WTMPFILE "/usr/adm/wtmp"
1341 logwtmp(line, name, host)
1342 const char *line, *name, *host;
1348 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1350 if (!fstat(fd, &buf)) {
1351 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1352 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1353 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1354 (void)time(&ut.ut_time);
1355 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1356 (void)ftruncate(fd, buf.st_size);
1363 * Routines for locking and unlocking the serial device, moved here
1367 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1370 * lock - create a lock file for the named device
1380 if ((p = strrchr(dev, '/')) != NULL)
1382 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1383 lock_file = malloc(l);
1384 if (lock_file == NULL)
1385 novm("lock file name");
1386 slprintf(lock_file, l, "%s%s, LOCK_PREFIX, dev);
1388 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1390 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1391 /* Read the lock file to find out who has the device locked */
1392 n = read(fd, &pid, sizeof(pid));
1394 error("Can't read pid from lock file %s", lock_file);
1397 if (kill(pid, 0) == -1 && errno == ESRCH) {
1398 /* pid no longer exists - remove the lock file */
1399 if (unlink(lock_file) == 0) {
1401 notice("Removed stale lock on %s (pid %d)",
1405 warn("Couldn't remove stale lock on %s",
1408 notice("Device %s is locked by pid %d",
1413 error("Can't create lock file %s: %m", lock_file);
1420 write(fd, &pid, sizeof pid);
1427 * unlock - remove our lockfile