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. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The name(s) of the authors of this software must not be used to
20 * endorse or promote products derived from this software without
21 * prior written permission.
23 * 4. Redistributions of any form whatsoever must retain the following
25 * "This product includes software developed by Paul Mackerras
26 * <paulus@samba.org>".
28 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
29 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
30 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
31 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
33 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
34 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 * Derived from main.c and pppd.h, which are:
38 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in
49 * the documentation and/or other materials provided with the
52 * 3. The name "Carnegie Mellon University" must not be used to
53 * endorse or promote products derived from this software without
54 * prior written permission. For permission or any legal
55 * details, please contact
56 * Office of Technology Transfer
57 * Carnegie Mellon University
59 * Pittsburgh, PA 15213-3890
60 * (412) 268-4387, fax: (412) 268-7395
61 * tech-transfer@andrew.cmu.edu
63 * 4. Redistributions of any form whatsoever must retain the following
65 * "This product includes software developed by Computing Services
66 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
68 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
69 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
70 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
71 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
72 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
73 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
74 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
77 #define RCSID "$Id: sys-ultrix.c,v 1.34 2002/12/04 23:03:33 paulus Exp $"
91 #include <sys/types.h>
93 #include <sys/socket.h>
94 #include <sys/ioctl.h>
96 #include <sys/errno.h>
100 #include <net/ppp_defs.h>
101 #include <net/if_ppp.h>
102 #include <net/route.h>
103 #include <netinet/in.h>
107 static const char rcsid[] = RCSID;
109 static int initdisc = -1; /* Initial TTY discipline for ppp_fd */
110 static int initfdflags = -1; /* Initial file descriptor flags for ppp_fd */
111 static int ppp_fd = -1; /* fd which is set to PPP discipline */
113 static int restore_term; /* 1 => we've munged the terminal */
114 static struct termios inittermios; /* Initial TTY termios */
115 static struct winsize wsinfo; /* Initial window size info */
117 static char *lock_file; /* name of lock file created */
119 static int loop_slave = -1;
120 static int loop_master;
121 static char loop_name[20];
123 static unsigned char inbuf[512]; /* buffer for chars read from loopback */
125 static int sockfd; /* socket for doing interface ioctls */
127 static fd_set in_fds; /* set of fds that wait_input waits for */
128 static int max_in_fd; /* highest fd set in in_fds */
130 static int if_is_up; /* the interface is currently up */
131 static u_int32_t ifaddrs[2]; /* local and remote addresses */
132 static u_int32_t default_route_gateway; /* gateway addr for default route */
133 static u_int32_t proxy_arp_addr; /* remote addr for proxy arp */
135 /* Prototypes for procedures local to this file. */
136 static int translate_speed __P((int));
137 static int baud_rate_of __P((int));
138 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
142 * sys_init - System-dependent initialization.
147 /* Get an internet socket for doing socket ioctl's on. */
148 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
149 fatal("Couldn't create IP socket: %m");
156 * sys_cleanup - restore any system state we modified before exiting:
157 * mark the interface down, delete default route and/or proxy arp entry.
158 * This shouldn't call die() because it's called from die().
166 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
167 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
168 && ((ifr.ifr_flags & IFF_UP) != 0)) {
169 ifr.ifr_flags &= ~IFF_UP;
170 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
174 cifaddr(0, ifaddrs[0], ifaddrs[1]);
175 if (default_route_gateway)
176 cifdefaultroute(0, 0, default_route_gateway);
178 cifproxyarp(0, proxy_arp_addr);
182 * sys_close - Clean up in a child process before execing.
188 if (loop_slave >= 0) {
195 * sys_check_options - check the options that the user specified
201 option_error("Sorry - demand-dialling is not supported under Ultrix\n");
209 * daemon - Detach us from the terminal session.
212 daemon(nochdir, noclose)
213 int nochdir, noclose;
217 if ((pid = fork()) < 0)
220 exit(0); /* parent dies */
225 fclose(stdin); /* don't need stdin, stdout, stderr */
234 * ppp_available - check whether the system has any ppp interfaces
235 * (in fact we check whether we can do an ioctl on ppp0).
242 extern char *no_ppp_msg;
244 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
245 return 1; /* can't tell */
247 strlcpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
248 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
252 This system lacks kernel support for PPP. To include PPP support\n\
253 in the kernel, please follow the steps detailed in the README.ultrix\n\
254 file in the ppp-2.2 distribution.\n";
259 * establish_ppp - Turn the serial port into a ppp interface.
265 int pppdisc = PPPDISC;
269 * Save the old line discipline of fd, and set it to PPP.
271 if (ioctl(fd, TIOCGETD, &initdisc) < 0)
272 fatal("ioctl(TIOCGETD): %m");
273 if (ioctl(fd, TIOCSETD, &pppdisc) < 0)
274 fatal("ioctl(TIOCSETD): %m");
277 * Find out which interface we were given.
279 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
280 fatal("ioctl(PPPIOCGUNIT): %m");
285 * Enable debug in the driver if requested.
288 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
289 warn("ioctl (PPPIOCGFLAGS): %m");
291 x |= (kdebugflag & 0xFF) * SC_DEBUG;
292 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
293 warn("ioctl(PPPIOCSFLAGS): %m");
298 * Set device for non-blocking reads.
300 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
301 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
302 warn("Couldn't set device to non-blocking mode: %m");
309 * restore_loop - reattach the ppp unit to the loopback.
317 * disestablish_ppp - Restore the serial port to normal operation.
318 * This shouldn't call die() because it's called from die().
324 /* Reset non-blocking mode on fd. */
325 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
326 warn("Couldn't restore device fd flags: %m");
329 /* Restore old line discipline. */
330 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
331 error("ioctl(TIOCSETD): %m");
339 * Check whether the link seems not to be 8-bit clean.
347 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
349 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
351 s = "bit 7 set to 1";
354 s = "bit 7 set to 0";
364 warn("Serial link is not 8-bit clean:");
365 warn("All received characters had %s", s);
372 * List of valid speeds.
375 int speed_int, speed_val;
447 * Translate from bits/second to a speed_t.
453 struct speed *speedp;
457 for (speedp = speeds; speedp->speed_int; speedp++)
458 if (bps == speedp->speed_int)
459 return speedp->speed_val;
460 warn("speed %d not supported", bps);
465 * Translate from a speed_t to bits/second.
471 struct speed *speedp;
475 for (speedp = speeds; speedp->speed_int; speedp++)
476 if (speed == speedp->speed_val)
477 return speedp->speed_int;
482 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
483 * at the requested speed, etc. If `local' is true, set CLOCAL
484 * regardless of whether the modem option was specified.
487 set_up_tty(fd, local)
493 if (tcgetattr(fd, &tios) < 0)
494 fatal("tcgetattr: %m");
498 ioctl(fd, TIOCGWINSZ, &wsinfo);
501 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
503 if (crtscts > 0 && !local)
504 tios.c_cflag |= CRTSCTS;
505 else if (crtscts < 0)
506 tios.c_cflag &= ~CRTSCTS;
509 tios.c_cflag |= CS8 | CREAD | HUPCL;
511 tios.c_cflag |= CLOCAL;
512 tios.c_iflag = IGNBRK | IGNPAR;
516 tios.c_cc[VTIME] = 0;
519 tios.c_iflag |= IXON | IXOFF;
520 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
521 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
524 speed = translate_speed(inspeed);
526 cfsetospeed(&tios, speed);
527 cfsetispeed(&tios, speed);
529 speed = cfgetospeed(&tios);
531 * We can't proceed if the serial port speed is B0,
532 * since that implies that the serial port is disabled.
535 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
538 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
539 fatal("tcsetattr: %m");
542 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
543 warn("TIOC(N)MODEM: %m");
544 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
545 warn("TIOC(N)CAR: %m");
547 baud_rate = inspeed = baud_rate_of(speed);
552 * restore_tty - restore the terminal to the saved settings.
559 if (!default_device) {
561 * Turn off echoing, because otherwise we can get into
562 * a loop with the tty and the modem echoing to each other.
563 * We presume we are the sole user of this tty device, so
564 * when we close it, it will revert to its defaults anyway.
566 inittermios.c_lflag &= ~(ECHO | ECHONL);
568 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
570 warn("tcsetattr: %m");
571 ioctl(fd, TIOCSWINSZ, &wsinfo);
577 * setdtr - control the DTR line on the serial port.
578 * This is called from die(), so it shouldn't call die().
584 int modembits = TIOCM_DTR;
586 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
591 * open_ppp_loopback - open the device we use for getting
592 * packets in demand mode, and connect it to a ppp interface.
593 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
598 fatal("open_ppp_loopback called!");
599 /* return loop_master; */
604 * output - Output PPP packet.
613 dbglog("sent %P", p, len);
615 if (write(ttyfd, p, len) < 0) {
623 * wait_input - wait until there is data available,
624 * for the length of time specified by *timo (indefinite
629 struct timeval *timo;
635 n = select(ttyfd+1, &ready, NULL, &ready, timo);
636 if (n < 0 && errno != EINTR)
642 * add_fd - add an fd to the set that wait_input waits for.
653 * remove_fd - remove an fd from the set that wait_input waits for.
663 * wait_loop_output - wait until there is data available on the
664 * loopback, for the length of time specified by *timo (indefinite
668 wait_loop_output(timo)
669 struct timeval *timo;
676 * wait_time - wait for a given length of time or until a
677 * signal is received.
681 struct timeval *timo;
685 n = select(0, NULL, NULL, NULL, timo);
686 if (n < 0 && errno != EINTR)
692 * read_packet - get a PPP packet from the serial device.
700 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
701 if (errno == EWOULDBLOCK || errno == EINTR)
703 fatal("read(fd): %m");
710 * get_loop_output - read characters from the loopback, form them
711 * into frames, and detect when we want to bring the real link up.
712 * Return value is 1 if we need to bring up the link, 0 otherwise.
722 * ppp_send_config - configure the transmit characteristics of
726 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
733 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0)
734 fatal("ioctl(PPPIOCSMTU): %m");
736 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0)
737 fatal("ioctl(PPPIOCSASYNCMAP): %m");
739 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
740 fatal("ioctl (PPPIOCGFLAGS): %m");
741 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
742 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
743 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
744 fatal("ioctl(PPPIOCSFLAGS): %m");
749 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
752 ppp_set_xaccm(unit, accm)
756 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
757 warn("ioctl(set extended ACCM): %m");
762 * ppp_recv_config - configure the receive-side characteristics of
766 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
773 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0)
774 fatal("ioctl(PPPIOCSMRU): %m");
775 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0)
776 fatal("ioctl(PPPIOCSRASYNCMAP): %m");
777 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0)
778 fatal("ioctl (PPPIOCGFLAGS): %m");
779 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
780 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
781 fatal("ioctl(PPPIOCSFLAGS): %m");
785 * ccp_test - ask kernel whether a given compression method
786 * is acceptable for use. Returns 1 if the method and parameters
787 * are OK, 0 if the method is known but the parameters are not OK
788 * (e.g. code size should be reduced), or -1 if the method is unknown.
791 ccp_test(unit, opt_ptr, opt_len, for_transmit)
792 int unit, opt_len, for_transmit;
795 struct ppp_option_data data;
798 data.length = opt_len;
799 data.transmit = for_transmit;
800 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
802 return (errno == ENOBUFS)? 0: -1;
806 * ccp_flags_set - inform kernel about the current state of CCP.
809 ccp_flags_set(unit, isopen, isup)
810 int unit, isopen, isup;
814 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
815 error("ioctl (PPPIOCGFLAGS): %m");
818 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
819 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
820 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
821 error("ioctl(PPPIOCSFLAGS): %m");
825 * ccp_fatal_error - returns 1 if decompression was disabled as a
826 * result of an error detected after decompression of a packet,
827 * 0 otherwise. This is necessary because of patent nonsense.
830 ccp_fatal_error(unit)
835 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
836 error("ioctl(PPPIOCGFLAGS): %m");
839 return x & SC_DC_FERROR;
843 * get_idle_time - return how long the link has been idle.
850 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
854 * get_ppp_stats - return statistics for the link.
857 get_ppp_stats(u, stats)
859 struct pppd_stats *stats;
861 struct ifpppstatsreq req;
863 memset (&req, 0, sizeof (req));
864 strlcpy(req.ifr_name, interface, sizeof(req.ifr_name));
865 if (ioctl(sockfd, SIOCGPPPSTATS, &req) < 0) {
866 error("Couldn't get PPP statistics: %m");
869 stats->bytes_in = req.stats.p.ppp_ibytes;
870 stats->bytes_out = req.stats.p.ppp_obytes;
876 * sifvjcomp - config tcp header compression
879 sifvjcomp(u, vjcomp, cidcomp, maxcid)
880 int u, vjcomp, cidcomp, maxcid;
884 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
885 error("ioctl (PPPIOCGFLAGS): %m");
888 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
889 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
890 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
891 error("ioctl(PPPIOCSFLAGS): %m");
894 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
895 error("ioctl(PPPIOCSFLAGS): %m");
902 * sifup - Config the interface up and enable IP packets to pass.
910 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
911 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
912 error("ioctl (SIOCGIFFLAGS): %m");
915 ifr.ifr_flags |= IFF_UP;
916 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
917 error("ioctl(SIOCSIFFLAGS): %m");
925 * sifnpmode - Set the mode for handling packets for a given NP.
928 sifnpmode(u, proto, mode)
935 npi.protocol = proto;
937 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
938 error("ioctl(set NP %d mode to %d): %m", proto, mode);
945 * sifdown - Config the interface down and disable IP.
956 npi.protocol = PPP_IP;
957 npi.mode = NPMODE_ERROR;
958 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
959 /* ignore errors, because ppp_fd might have been closed by now. */
961 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
962 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
963 error("ioctl (SIOCGIFFLAGS): %m");
966 ifr.ifr_flags &= ~IFF_UP;
967 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
968 error("ioctl(SIOCSIFFLAGS): %m");
977 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
980 #define SET_SA_FAMILY(addr, family) \
981 BZERO((char *) &(addr), sizeof(addr)); \
982 addr.sa_family = (family);
985 * sifaddr - Config the interface IP addresses and netmask.
996 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
997 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
999 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
1000 info("Setting interface mask to %s\n", ip_ntoa(m));
1001 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
1002 error("ioctl(SIOCSIFNETMASK): %m");
1006 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1007 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1008 error("ioctl(SIOCSIFADDR): %m");
1011 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1012 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1013 error("ioctl(SIOCSIFDSTADDR): %m");
1022 * cifaddr - Clear the interface IP addresses, and delete routes
1023 * through the interface if possible.
1033 BZERO(&rt, sizeof(rt));
1034 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1035 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1036 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1037 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1038 rt.rt_flags = RTF_HOST;
1039 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1040 error("ioctl(SIOCDELRT): %m");
1047 * sifdefaultroute - assign a default route through the address given.
1050 sifdefaultroute(u, l, g)
1056 BZERO(&rt, sizeof(rt));
1057 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1058 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1059 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1060 rt.rt_flags = RTF_GATEWAY;
1061 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1062 error("default route ioctl(SIOCADDRT): %m");
1065 default_route_gateway = g;
1070 * cifdefaultroute - delete a default route through the address given.
1073 cifdefaultroute(u, l, g)
1079 BZERO(&rt, sizeof(rt));
1080 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1081 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1082 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1083 rt.rt_flags = RTF_GATEWAY;
1084 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1085 warn("default route ioctl(SIOCDELRT): %m");
1086 default_route_gateway = 0;
1091 * sifproxyarp - Make a proxy ARP entry for the peer.
1094 sifproxyarp(unit, hisaddr)
1098 struct arpreq arpreq;
1100 BZERO(&arpreq, sizeof(arpreq));
1103 * Get the hardware address of an interface on the same subnet
1104 * as our local address.
1106 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1107 error("Cannot determine ethernet address for proxy ARP");
1111 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1112 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1113 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1114 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1115 error("Couldn't add proxy arp entry: %m");
1119 proxy_arp_addr = hisaddr;
1124 * cifproxyarp - Delete the proxy ARP entry for the peer.
1127 cifproxyarp(unit, hisaddr)
1131 struct arpreq arpreq;
1133 BZERO(&arpreq, sizeof(arpreq));
1134 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1135 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1136 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1137 warn("Couldn't delete proxy arp entry: %m");
1145 * get_ether_addr - get the hardware address of an interface on the
1146 * the same subnet as ipaddr.
1151 get_ether_addr(ipaddr, hwaddr)
1153 struct sockaddr *hwaddr;
1155 struct ifreq *ifr, *ifend;
1156 u_int32_t ina, mask;
1159 struct ifreq ifs[MAX_IFS];
1160 struct ifdevea ifdevea;
1162 ifc.ifc_len = sizeof(ifs);
1164 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1165 error("ioctl(SIOCGIFCONF): %m");
1170 * Scan through looking for an interface with an Internet
1171 * address on the same subnet as `ipaddr'.
1173 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1174 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1175 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1176 if (ifr->ifr_addr.sa_family == AF_INET) {
1177 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1178 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1180 * Check that the interface is up, and not point-to-point
1183 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1185 if ((ifreq.ifr_flags &
1186 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1187 != (IFF_UP|IFF_BROADCAST))
1190 * Get its netmask and check that it's on the right subnet.
1192 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1194 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1195 if ((ipaddr & mask) != (ina & mask))
1204 info("found interface %s for proxy arp", ifr->ifr_name);
1207 * Grab the physical address for this interface.
1209 strlcpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1210 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1211 error("Couldn't get h/w address for %s: %m", ifr->ifr_name);
1215 hwaddr->sa_family = AF_UNSPEC;
1216 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1221 * Return user specified netmask, modified by any mask we might determine
1222 * for address `addr' (in network byte order).
1223 * Here we scan through the system's list of interfaces, looking for
1224 * any non-point-to-point interfaces which might appear to be on the same
1225 * network as `addr'. If we find any, we OR in their netmask to the
1226 * user-specified netmask.
1232 u_int32_t mask, nmask, ina;
1233 struct ifreq *ifr, *ifend, ifreq;
1235 struct ifreq ifs[MAX_IFS];
1238 if (IN_CLASSA(addr)) /* determine network mask for address class */
1239 nmask = IN_CLASSA_NET;
1240 else if (IN_CLASSB(addr))
1241 nmask = IN_CLASSB_NET;
1243 nmask = IN_CLASSC_NET;
1244 /* class D nets are disallowed by bad_ip_adrs */
1245 mask = netmask | htonl(nmask);
1248 * Scan through the system's network interfaces.
1250 ifc.ifc_len = sizeof(ifs);
1252 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1253 warn("ioctl(SIOCGIFCONF): %m");
1256 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1257 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1258 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1260 * Check the interface's internet address.
1262 if (ifr->ifr_addr.sa_family != AF_INET)
1264 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1265 if ((ntohl(ina) & nmask) != (addr & nmask))
1268 * Check that the interface is up, and not point-to-point or loopback.
1270 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1271 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1273 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1277 * Get its netmask and OR it into our mask.
1279 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1281 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1288 * have_route_to - determine if the system has any route to
1289 * a given IP address.
1290 * For demand mode to work properly, we have to ignore routes
1291 * through our own interface.
1293 int have_route_to(u_int32_t addr)
1300 * Use the hostid as part of the random number seed.
1310 Seems like strdup() is not part of string package in Ultrix.
1311 If I understood the man-page on the sun this should work.
1316 char *strdup( in ) char *in;
1319 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1320 (void) strcpy( dup, in );
1325 * This logwtmp() implementation is subject to the following copyright:
1327 * Copyright (c) 1988 The Regents of the University of California.
1328 * All rights reserved.
1330 * Redistribution and use in source and binary forms are permitted
1331 * provided that the above copyright notice and this paragraph are
1332 * duplicated in all such forms and that any documentation,
1333 * advertising materials, and other materials related to such
1334 * distribution and use acknowledge that the software was developed
1335 * by the University of California, Berkeley. The name of the
1336 * University may not be used to endorse or promote products derived
1337 * from this software without specific prior written permission.
1338 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1339 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1340 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1343 #define WTMPFILE "/usr/adm/wtmp"
1346 logwtmp(line, name, host)
1347 const char *line, *name, *host;
1353 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1355 if (!fstat(fd, &buf)) {
1356 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1357 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1358 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1359 (void)time(&ut.ut_time);
1360 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1361 (void)ftruncate(fd, buf.st_size);
1368 * Routines for locking and unlocking the serial device, moved here
1372 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1375 * lock - create a lock file for the named device
1385 if ((p = strrchr(dev, '/')) != NULL)
1387 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1388 lock_file = malloc(l);
1389 if (lock_file == NULL)
1390 novm("lock file name");
1391 slprintf(lock_file, l, "%s%s, LOCK_PREFIX, dev);
1393 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1395 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1396 /* Read the lock file to find out who has the device locked */
1397 n = read(fd, &pid, sizeof(pid));
1399 error("Can't read pid from lock file %s", lock_file);
1402 if (kill(pid, 0) == -1 && errno == ESRCH) {
1403 /* pid no longer exists - remove the lock file */
1404 if (unlink(lock_file) == 0) {
1406 notice("Removed stale lock on %s (pid %d)",
1410 warn("Couldn't remove stale lock on %s",
1413 notice("Device %s is locked by pid %d",
1418 error("Can't create lock file %s: %m", lock_file);
1425 write(fd, &pid, sizeof pid);
1432 * unlock - remove our lockfile