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.16 1996/04/04 04:04:19 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 openlog("pppd", LOG_PID);
93 /* Get an internet socket for doing socket ioctl's on. */
94 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
95 syslog(LOG_ERR, "Couldn't create IP socket: %m");
101 * sys_cleanup - restore any system state we modified before exiting:
102 * mark the interface down, delete default route and/or proxy arp entry.
103 * This should call die() because it's called from die().
111 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
112 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
113 && ((ifr.ifr_flags & IFF_UP) != 0)) {
114 ifr.ifr_flags &= ~IFF_UP;
115 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
119 cifaddr(0, ifaddrs[0], ifaddrs[1]);
120 if (default_route_gateway)
121 cifdefaultroute(0, default_route_gateway);
123 cifproxyarp(0, proxy_arp_addr);
127 * sys_close - Clean up in a child process before execing.
133 if (loop_slave >= 0) {
141 * sys_check_options - check the options that the user specified
148 "Sorry - demand-dialling is not supported under Ultrix\n");
155 * note_debug_level - note a change in the debug level.
164 * daemon - Detach us from the terminal session.
167 daemon(nochdir, noclose)
168 int nochdir, noclose;
172 if ((pid = fork()) < 0)
175 exit(0); /* parent dies */
180 fclose(stdin); /* don't need stdin, stdout, stderr */
188 * ppp_available - check whether the system has any ppp interfaces
189 * (in fact we check whether we can do an ioctl on ppp0).
196 extern char *no_ppp_msg;
198 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
199 return 1; /* can't tell */
201 strncpy(ifr.ifr_name, "ppp0", sizeof (ifr.ifr_name));
202 ok = ioctl(s, SIOCGIFFLAGS, (caddr_t) &ifr) >= 0;
206 This system lacks kernel support for PPP. To include PPP support\n\
207 in the kernel, please follow the steps detailed in the README.ultrix\n\
208 file in the ppp-2.2 distribution.\n";
213 * establish_ppp - Turn the serial port into a ppp interface.
219 int pppdisc = PPPDISC;
223 * Save the old line discipline of fd, and set it to PPP.
225 if (ioctl(fd, TIOCGETD, &initdisc) < 0) {
226 syslog(LOG_ERR, "ioctl(TIOCGETD): %m");
229 if (ioctl(fd, TIOCSETD, &pppdisc) < 0) {
230 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
235 * Find out which interface we were given.
237 if (ioctl(fd, PPPIOCGUNIT, &ifunit) < 0) {
238 syslog(LOG_ERR, "ioctl(PPPIOCGUNIT): %m");
245 * Enable debug in the driver if requested.
248 if (ioctl(fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
249 syslog(LOG_WARNING, "ioctl (PPPIOCGFLAGS): %m");
251 x |= (kdebugflag & 0xFF) * SC_DEBUG;
252 if (ioctl(fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
253 syslog(LOG_WARNING, "ioctl(PPPIOCSFLAGS): %m");
258 * Set device for non-blocking reads.
260 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
261 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
262 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
267 * restore_loop - reattach the ppp unit to the loopback.
275 * disestablish_ppp - Restore the serial port to normal operation.
276 * This shouldn't call die() because it's called from die().
282 /* Reset non-blocking mode on fd. */
283 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
284 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
287 /* Restore old line discipline. */
288 if (initdisc >= 0 && ioctl(fd, TIOCSETD, &initdisc) < 0)
289 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
297 * Check whether the link seems not to be 8-bit clean.
305 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) == 0) {
307 switch (~x & (SC_RCV_B7_0|SC_RCV_B7_1|SC_RCV_EVNP|SC_RCV_ODDP)) {
309 s = "bit 7 set to 1";
312 s = "bit 7 set to 0";
322 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
323 syslog(LOG_WARNING, "All received characters had %s", s);
330 * List of valid speeds.
333 int speed_int, speed_val;
405 * Translate from bits/second to a speed_t.
411 struct speed *speedp;
415 for (speedp = speeds; speedp->speed_int; speedp++)
416 if (bps == speedp->speed_int)
417 return speedp->speed_val;
418 syslog(LOG_WARNING, "speed %d not supported", bps);
423 * Translate from a speed_t to bits/second.
429 struct speed *speedp;
433 for (speedp = speeds; speedp->speed_int; speedp++)
434 if (speed == speedp->speed_val)
435 return speedp->speed_int;
440 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
441 * at the requested speed, etc. If `local' is true, set CLOCAL
442 * regardless of whether the modem option was specified.
445 set_up_tty(fd, local)
451 if (tcgetattr(fd, &tios) < 0) {
452 syslog(LOG_ERR, "tcgetattr: %m");
458 ioctl(fd, TIOCGWINSZ, &wsinfo);
461 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
463 if (crtscts > 0 && !local)
464 tios.c_cflag |= CRTSCTS;
465 else if (crtscts < 0)
466 tios.c_cflag &= ~CRTSCTS;
469 tios.c_cflag |= CS8 | CREAD | HUPCL;
471 tios.c_cflag |= CLOCAL;
472 tios.c_iflag = IGNBRK | IGNPAR;
476 tios.c_cc[VTIME] = 0;
479 tios.c_iflag |= IXON | IXOFF;
480 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
481 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
484 speed = translate_speed(inspeed);
486 cfsetospeed(&tios, speed);
487 cfsetispeed(&tios, speed);
489 speed = cfgetospeed(&tios);
491 * We can't proceed if the serial port speed is B0,
492 * since that implies that the serial port is disabled.
495 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
501 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
502 syslog(LOG_ERR, "tcsetattr: %m");
507 if (ioctl(fd, (crtscts > 0 || modem)? TIOCMODEM: TIOCNMODEM, &x) < 0)
508 syslog(LOG_WARNING, "TIOC(N)MODEM: %m");
509 if (ioctl(fd, (local || !modem)? TIOCNCAR: TIOCCAR) < 0)
510 syslog(LOG_WARNING, "TIOC(N)CAR: %m");
512 baud_rate = inspeed = baud_rate_of(speed);
517 * restore_tty - restore the terminal to the saved settings.
524 if (!default_device) {
526 * Turn off echoing, because otherwise we can get into
527 * a loop with the tty and the modem echoing to each other.
528 * We presume we are the sole user of this tty device, so
529 * when we close it, it will revert to its defaults anyway.
531 inittermios.c_lflag &= ~(ECHO | ECHONL);
533 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
535 syslog(LOG_WARNING, "tcsetattr: %m");
536 ioctl(fd, TIOCSWINSZ, &wsinfo);
537 restore_term = FALSE;
542 * setdtr - control the DTR line on the serial port.
543 * This is called from die(), so it shouldn't call die().
549 int modembits = TIOCM_DTR;
551 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
556 * open_ppp_loopback - open the device we use for getting
557 * packets in demand mode, and connect it to a ppp interface.
558 * Here we use would use a pty, except that Ultrix ptys are brain-dead.
563 syslog(LOG_ERR, "open_ppp_loopback called!");
569 * output - Output PPP packet.
578 log_packet(p, len, "sent ");
580 if (write(ttyfd, p, len) < 0) {
582 syslog(LOG_ERR, "write: %m");
588 * wait_input - wait until there is data available on ttyfd,
589 * for the length of time specified by *timo (indefinite
594 struct timeval *timo;
600 FD_SET(ttyfd, &ready);
601 n = select(ttyfd+1, &ready, NULL, &ready, timo);
602 if (n < 0 && errno != EINTR) {
603 syslog(LOG_ERR, "select: %m");
610 * wait_loop_output - wait until there is data available on the
611 * loopback, for the length of time specified by *timo (indefinite
614 wait_loop_output(timo)
615 struct timeval *timo;
622 * wait_time - wait for a given length of time or until a
623 * signal is received.
626 struct timeval *timo;
630 n = select(0, NULL, NULL, NULL, timo);
631 if (n < 0 && errno != EINTR) {
632 syslog(LOG_ERR, "select: %m");
639 * read_packet - get a PPP packet from the serial device.
647 if ((len = read(ttyfd, buf, PPP_MTU + PPP_HDRLEN)) < 0) {
648 if (errno == EWOULDBLOCK || errno == EINTR)
650 syslog(LOG_ERR, "read(fd): %m");
658 * get_loop_output - read characters from the loopback, form them
659 * into frames, and detect when we want to bring the real link up.
660 * Return value is 1 if we need to bring up the link, 0 otherwise.
670 * ppp_send_config - configure the transmit characteristics of
674 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
681 if (ioctl(ppp_fd, PPPIOCSMTU, &mtu) < 0) {
682 syslog(LOG_ERR, "ioctl(PPPIOCSMTU): %m");
686 if (ioctl(ppp_fd, PPPIOCSASYNCMAP, (caddr_t) &asyncmap) < 0) {
687 syslog(LOG_ERR, "ioctl(PPPIOCSASYNCMAP): %m");
691 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
692 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
695 x = pcomp? x | SC_COMP_PROT: x &~ SC_COMP_PROT;
696 x = accomp? x | SC_COMP_AC: x &~ SC_COMP_AC;
697 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
698 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
705 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
708 ppp_set_xaccm(unit, accm)
712 if (ioctl(ppp_fd, PPPIOCSXASYNCMAP, accm) < 0 && errno != ENOTTY)
713 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
718 * ppp_recv_config - configure the receive-side characteristics of
722 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
729 if (ioctl(ppp_fd, PPPIOCSMRU, (caddr_t) &mru) < 0) {
730 syslog(LOG_ERR, "ioctl(PPPIOCSMRU): %m");
733 if (ioctl(ppp_fd, PPPIOCSRASYNCMAP, (caddr_t) &asyncmap) < 0) {
734 syslog(LOG_ERR, "ioctl(PPPIOCSRASYNCMAP): %m");
737 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
738 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
741 x = !accomp? x | SC_REJ_COMP_AC: x &~ SC_REJ_COMP_AC;
742 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
743 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
749 * ccp_test - ask kernel whether a given compression method
750 * is acceptable for use. Returns 1 if the method and parameters
751 * are OK, 0 if the method is known but the parameters are not OK
752 * (e.g. code size should be reduced), or -1 if the method is unknown.
755 ccp_test(unit, opt_ptr, opt_len, for_transmit)
756 int unit, opt_len, for_transmit;
759 struct ppp_option_data data;
762 data.length = opt_len;
763 data.transmit = for_transmit;
764 if (ioctl(ttyfd, PPPIOCSCOMPRESS, (caddr_t) &data) >= 0)
766 return (errno == ENOBUFS)? 0: -1;
770 * ccp_flags_set - inform kernel about the current state of CCP.
773 ccp_flags_set(unit, isopen, isup)
774 int unit, isopen, isup;
778 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
779 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
782 x = isopen? x | SC_CCP_OPEN: x &~ SC_CCP_OPEN;
783 x = isup? x | SC_CCP_UP: x &~ SC_CCP_UP;
784 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0)
785 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
789 * ccp_fatal_error - returns 1 if decompression was disabled as a
790 * result of an error detected after decompression of a packet,
791 * 0 otherwise. This is necessary because of patent nonsense.
794 ccp_fatal_error(unit)
799 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
800 syslog(LOG_ERR, "ioctl(PPPIOCGFLAGS): %m");
803 return x & SC_DC_FERROR;
807 * get_idle_time - return how long the link has been idle.
814 return ioctl(ppp_fd, PPPIOCGIDLE, ip) >= 0;
819 * set_filters - transfer the pass and active filters to the kernel.
822 set_filters(pass, active)
823 struct bpf_program *pass, *active;
827 if (pass->bf_len > 0) {
828 if (ioctl(ppp_fd, PPPIOCSPASS, pass) < 0) {
829 syslog(LOG_ERR, "Couldn't set pass-filter in kernel: %m");
833 if (active->bf_len > 0) {
834 if (ioctl(ppp_fd, PPPIOCSACTIVE, active) < 0) {
835 syslog(LOG_ERR, "Couldn't set active-filter in kernel: %m");
844 * sifvjcomp - config tcp header compression
847 sifvjcomp(u, vjcomp, cidcomp, maxcid)
848 int u, vjcomp, cidcomp, maxcid;
852 if (ioctl(ppp_fd, PPPIOCGFLAGS, (caddr_t) &x) < 0) {
853 syslog(LOG_ERR, "ioctl (PPPIOCGFLAGS): %m");
856 x = vjcomp ? x | SC_COMP_TCP: x &~ SC_COMP_TCP;
857 x = cidcomp? x & ~SC_NO_TCP_CCID: x | SC_NO_TCP_CCID;
858 if (ioctl(ppp_fd, PPPIOCSFLAGS, (caddr_t) &x) < 0) {
859 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
862 if (ioctl(ppp_fd, PPPIOCSMAXCID, (caddr_t) &maxcid) < 0) {
863 syslog(LOG_ERR, "ioctl(PPPIOCSFLAGS): %m");
870 * sifup - Config the interface up and enable IP packets to pass.
879 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
880 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
881 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
884 ifr.ifr_flags |= IFF_UP;
885 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
886 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
890 npi.protocol = PPP_IP;
891 npi.mode = NPMODE_PASS;
892 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
893 syslog(LOG_ERR, "ioctl(set IP mode to PASS): %m");
900 * sifnpmode - Set the mode for handling packets for a given NP.
903 sifnpmode(u, proto, mode)
910 npi.protocol = proto;
912 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
913 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
920 * sifdown - Config the interface down and disable IP.
931 npi.protocol = PPP_IP;
932 npi.mode = NPMODE_ERROR;
933 ioctl(ppp_fd, PPPIOCSNPMODE, (caddr_t) &npi);
934 /* ignore errors, because ppp_fd might have been closed by now. */
936 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
937 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
938 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
941 ifr.ifr_flags &= ~IFF_UP;
942 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
943 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
952 * SET_SA_FAMILY - set the sa_family field of a struct sockaddr,
955 #define SET_SA_FAMILY(addr, family) \
956 BZERO((char *) &(addr), sizeof(addr)); \
957 addr.sa_family = (family);
960 * sifaddr - Config the interface IP addresses and netmask.
971 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
972 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
974 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
975 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
976 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
977 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
981 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
982 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
983 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
986 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
987 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
988 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
997 * cifaddr - Clear the interface IP addresses, and delete routes
998 * through the interface if possible.
1008 BZERO(&rt, sizeof(rt));
1009 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1010 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1011 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1012 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1013 rt.rt_flags = RTF_HOST;
1014 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1015 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
1022 * sifdefaultroute - assign a default route through the address given.
1025 sifdefaultroute(u, g)
1029 BZERO(&rt, sizeof(rt));
1030 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1031 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1032 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1033 rt.rt_flags = RTF_GATEWAY;
1034 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1035 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1038 default_route_gateway = g;
1043 * cifdefaultroute - delete a default route through the address given.
1046 cifdefaultroute(u, g)
1050 BZERO(&rt, sizeof(rt));
1051 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1052 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1053 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1054 rt.rt_flags = RTF_GATEWAY;
1055 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1056 syslog(LOG_WARNING, "default route ioctl(SIOCDELRT): %m");
1057 default_route_gateway = 0;
1062 * sifproxyarp - Make a proxy ARP entry for the peer.
1065 sifproxyarp(unit, hisaddr)
1069 struct arpreq arpreq;
1071 BZERO(&arpreq, sizeof(arpreq));
1074 * Get the hardware address of an interface on the same subnet
1075 * as our local address.
1077 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1078 syslog(LOG_ERR, "Cannot determine ethernet address for proxy ARP");
1082 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1083 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1084 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1085 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1086 syslog(LOG_ERR, "Couldn't add proxy arp entry: %m");
1090 proxy_arp_addr = hisaddr;
1095 * cifproxyarp - Delete the proxy ARP entry for the peer.
1098 cifproxyarp(unit, hisaddr)
1102 struct arpreq arpreq;
1104 BZERO(&arpreq, sizeof(arpreq));
1105 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1106 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1107 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1108 syslog(LOG_WARNING, "Couldn't delete proxy arp entry: %m");
1116 * get_ether_addr - get the hardware address of an interface on the
1117 * the same subnet as ipaddr.
1122 get_ether_addr(ipaddr, hwaddr)
1124 struct sockaddr *hwaddr;
1126 struct ifreq *ifr, *ifend;
1127 u_int32_t ina, mask;
1130 struct ifreq ifs[MAX_IFS];
1131 struct ifdevea ifdevea;
1133 ifc.ifc_len = sizeof(ifs);
1135 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1136 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1141 * Scan through looking for an interface with an Internet
1142 * address on the same subnet as `ipaddr'.
1144 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1145 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1146 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1147 if (ifr->ifr_addr.sa_family == AF_INET) {
1148 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1149 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1151 * Check that the interface is up, and not point-to-point
1154 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1156 if ((ifreq.ifr_flags &
1157 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1158 != (IFF_UP|IFF_BROADCAST))
1161 * Get its netmask and check that it's on the right subnet.
1163 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1165 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1166 if ((ipaddr & mask) != (ina & mask))
1175 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1178 * Grab the physical address for this interface.
1180 strncpy(ifdevea.ifr_name, ifr->ifr_name, sizeof(ifdevea.ifr_name));
1181 if (ioctl(sockfd, SIOCRPHYSADDR, &ifdevea) < 0) {
1182 syslog(LOG_ERR, "Couldn't get h/w address for %s: %m", ifr->ifr_name);
1186 hwaddr->sa_family = AF_UNSPEC;
1187 BCOPY(ifdevea.current_pa, hwaddr->sa_data, 6);
1192 * Return user specified netmask, modified by any mask we might determine
1193 * for address `addr' (in network byte order).
1194 * Here we scan through the system's list of interfaces, looking for
1195 * any non-point-to-point interfaces which might appear to be on the same
1196 * network as `addr'. If we find any, we OR in their netmask to the
1197 * user-specified netmask.
1203 u_int32_t mask, nmask, ina;
1204 struct ifreq *ifr, *ifend, ifreq;
1206 struct ifreq ifs[MAX_IFS];
1209 if (IN_CLASSA(addr)) /* determine network mask for address class */
1210 nmask = IN_CLASSA_NET;
1211 else if (IN_CLASSB(addr))
1212 nmask = IN_CLASSB_NET;
1214 nmask = IN_CLASSC_NET;
1215 /* class D nets are disallowed by bad_ip_adrs */
1216 mask = netmask | htonl(nmask);
1219 * Scan through the system's network interfaces.
1221 ifc.ifc_len = sizeof(ifs);
1223 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1224 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1227 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1228 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1229 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1231 * Check the interface's internet address.
1233 if (ifr->ifr_addr.sa_family != AF_INET)
1235 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1236 if ((ntohl(ina) & nmask) != (addr & nmask))
1239 * Check that the interface is up, and not point-to-point or loopback.
1241 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1242 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1244 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1248 * Get its netmask and OR it into our mask.
1250 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1252 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1260 Seems like strdup() is not part of string package in Ultrix.
1261 If I understood the man-page on the sun this should work.
1266 char *strdup( in ) char *in;
1269 if(! (dup = (char *) malloc( strlen( in ) +1 ))) return NULL;
1270 (void) strcpy( dup, in );
1275 * This logwtmp() implementation is subject to the following copyright:
1277 * Copyright (c) 1988 The Regents of the University of California.
1278 * All rights reserved.
1280 * Redistribution and use in source and binary forms are permitted
1281 * provided that the above copyright notice and this paragraph are
1282 * duplicated in all such forms and that any documentation,
1283 * advertising materials, and other materials related to such
1284 * distribution and use acknowledge that the software was developed
1285 * by the University of California, Berkeley. The name of the
1286 * University may not be used to endorse or promote products derived
1287 * from this software without specific prior written permission.
1288 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1289 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1290 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1293 #define WTMPFILE "/usr/adm/wtmp"
1296 logwtmp(line, name, host)
1297 char *line, *name, *host;
1303 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1305 if (!fstat(fd, &buf)) {
1306 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1307 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1308 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1309 (void)time(&ut.ut_time);
1310 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1311 (void)ftruncate(fd, buf.st_size);
1317 * Routines for locking and unlocking the serial device, moved here
1321 #define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1324 * lock - create a lock file for the named device
1333 if ((p = strrchr(dev, '/')) != NULL)
1335 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1336 if (lock_file == NULL)
1337 novm("lock file name");
1338 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1340 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1342 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1343 /* Read the lock file to find out who has the device locked */
1344 n = read(fd, &pid, sizeof(pid));
1346 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1349 if (kill(pid, 0) == -1 && errno == ESRCH) {
1350 /* pid no longer exists - remove the lock file */
1351 if (unlink(lock_file) == 0) {
1353 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1357 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1360 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1365 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1372 write(fd, &pid, sizeof pid);
1379 * unlock - remove our lockfile