2 * sys-osf.c - System-dependent procedures for setting up
3 * PPP interfaces on OSF/1 systems which use the STREAMS ppp interface.
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: sys-osf.c,v 1.4 1995/05/01 00:26:01 paulus Exp $";
40 #include <sys/types.h>
41 #include <sys/socket.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
47 #include <net/ppp_defs.h>
48 #include <net/ppp_str.h>
49 #include <net/route.h>
50 #include <net/if_arp.h>
51 #include <netinet/in.h>
56 #define ifr_mtu ifr_metric
59 #define MAXMODULES 10 /* max number of module names to save */
60 static struct modlist {
61 char modname[FMNAMESZ+1];
62 } str_modules[MAXMODULES];
63 static int str_module_count = 0;
64 static int pushed_ppp;
65 static int closed_stdio;
67 static int restore_term; /* 1 => we've munged the terminal */
68 static struct termios inittermios; /* Initial TTY termios */
69 static struct winsize wsinfo; /* Initial window size info */
71 int sockfd; /* socket for doing interface ioctls */
73 int ttyfd = -1; /* Original ttyfd if we did a streamify() */
76 * sys_init - System-dependent initialization.
81 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
82 setlogmask(LOG_UPTO(LOG_INFO));
84 setlogmask(LOG_UPTO(LOG_DEBUG));
86 /* Get an internet socket for doing socket ioctl's on. */
87 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
88 syslog(LOG_ERR, "Couldn't create IP socket: %m");
94 * note_debug_level - note a change in the debug level.
100 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
101 setlogmask(LOG_UPTO(LOG_DEBUG));
103 setlogmask(LOG_UPTO(LOG_WARNING));
108 * daemon - Detach us from the terminal session.
111 daemon(nochdir, noclose)
112 int nochdir, noclose;
116 if ((pid = fork()) < 0)
119 exit(0); /* parent dies */
124 fclose(stdin); /* don't need stdin, stdout, stderr */
132 char pipename[] = "/dev/streams/pipe";
135 * ppp_available - check if this kernel supports PPP.
142 fd = open(pipename, O_RDONLY, 0);
144 return 0; /* can't find out - assume we don't have ppp */
145 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
152 * streampipe -- Opens a STREAMS based pipe. Used by streamify().
156 streampipe(int fd[2])
158 if ((fd[0]=open(pipename, O_RDWR)) == -1)
160 else if ((fd[1]=open(pipename, O_RDWR)) == -1) {
163 } else if (ioctl(fd[0], I_PIPE, fd[1]) != 0) {
168 return(ioctl(fd[0], I_PUSH, "pipemod"));
173 * streamify -- Needed for DEC OSF/1, since tty devices are not STREAMS
174 * modules (but ptys are, and pipes can be).
177 #define BUFFSIZE 1000 /* Size of buffer for streamify() */
184 int ret, fret, rret, maxfd;
185 static char buffer[BUFFSIZE];
188 if (streampipe(fdes) != 0)
189 syslog(LOG_ERR, "streampipe(): %m\n");
190 else if (isastream(fdes[0]) == 1) {
191 if ((fret=fork()) < 0) {
192 syslog(LOG_ERR, "fork(): %m\n");
193 } else if (fret == 0) {
194 /* Process to forward things from pipe to tty */
195 sigemptyset(&(sa.sa_mask));
196 sa.sa_handler = SIG_DFL;
198 sigaction(SIGHUP, &sa, NULL); /* Go back to default actions */
199 sigaction(SIGINT, &sa, NULL); /* for changed signals. */
200 sigaction(SIGTERM, &sa, NULL);
201 sigaction(SIGCHLD, &sa, NULL);
202 sigaction(SIGUSR1, &sa, NULL);
203 sigaction(SIGUSR2, &sa, NULL);
206 maxfd = (fdes[1]>fd)?fdes[1]:fd;
209 FD_SET(fdes[1], &readfds);
210 FD_SET(fd, &readfds);
211 ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
212 if (FD_ISSET(fd, &readfds)) {
213 rret = read(fd, buffer, BUFFSIZE);
215 MAINDEBUG((LOG_DEBUG, "slave died: EOF on tty."));
218 write(fdes[1], buffer, rret);
221 if (FD_ISSET(fdes[1], &readfds)) {
222 rret = read(fdes[1], buffer, BUFFSIZE);
224 MAINDEBUG((LOG_DEBUG, "slave died: EOF on pipe."));
227 write(fd, buffer, rret);
243 * establish_ppp - Turn the serial port into a ppp interface.
250 if (isastream(fd) != 1) {
251 if ((fd=streamify(fd)) < 0) {
252 syslog(LOG_ERR, "Couldn't get a STREAMS module!\n");
257 /* go through and save the name of all the modules, then pop em */
259 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
260 ioctl(fd, I_POP, 0) < 0)
262 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
263 str_modules[str_module_count].modname));
267 /* now push the async/fcs module */
268 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
269 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
272 /* push the compress module */
273 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
274 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
276 /* finally, push the ppp_if module that actually handles the */
277 /* network interface */
278 if (ioctl(fd, I_PUSH, "pppif") < 0) {
279 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
283 /* read mode, message non-discard mode */
284 if (ioctl(fd, I_SRDOPT, RMSGN|RPROTNORM) < 0) {
285 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
289 * Find out which interface we were given.
290 * (ppp_if handles this ioctl)
292 if (ioctl(fd, (int)SIOCGETU, &ifunit) < 0) {
293 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
297 /* Set debug flags in driver */
298 if (ioctl(fd, (int)SIOCSIFDEBUG, &kdebugflag) < 0) {
299 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
302 /* close stdin, stdout, stderr if they might refer to the device */
303 if (default_device && !closed_stdio) {
306 for (i = 0; i <= 2; ++i)
307 if (i != fd && i != sockfd)
313 /* Debugging routine.... */
315 void prstrstack(int fd)
318 struct str_mlist names[20];
321 sl.sl_modlist = names;
323 ret = ioctl(fd, I_LIST, &sl);
326 MAINDEBUG((LOG_DEBUG, "Current streams modules:\n"));
327 for (i=0; i<sl.sl_nmods; i++)
328 MAINDEBUG((LOG_DEBUG," %s\n", names[i].l_name));
330 MAINDEBUG((LOG_DEBUG,"I_LIST ioctl() failed.\n"));
336 * disestablish_ppp - Restore the serial port to normal operation.
337 * It attempts to reconstruct the stream with the previously popped
338 * modules. This shouldn't call die() because it's called from die().
347 /* we can't push or pop modules after the stream has hung up */
348 str_module_count = 0;
349 restore_term = 0; /* nor can we fix up terminal settings */
355 * Check whether the link seems not to be 8-bit clean.
357 if (ioctl(fd, (int)SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
359 switch (~flags & PAI_FLAGS_HIBITS) {
361 s = "bit 7 set to 1";
364 s = "bit 7 set to 0";
366 case PAI_FLAGS_PAR_EVEN:
369 case PAI_FLAGS_PAR_ODD:
374 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
375 syslog(LOG_WARNING, "All received characters had %s", s);
379 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
383 for (; str_module_count > 0; str_module_count--) {
384 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
386 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
387 str_modules[str_module_count-1].modname);
389 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
390 str_modules[str_module_count-1].modname));
405 * List of valid speeds.
408 int speed_int, speed_val;
480 * Translate from bits/second to a speed_t.
486 struct speed *speedp;
490 for (speedp = speeds; speedp->speed_int; speedp++)
491 if (bps == speedp->speed_int)
492 return speedp->speed_val;
493 syslog(LOG_WARNING, "speed %d not supported", bps);
498 * Translate from a speed_t to bits/second.
504 struct speed *speedp;
508 for (speedp = speeds; speedp->speed_int; speedp++)
509 if (speed == speedp->speed_val)
510 return speedp->speed_int;
515 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
516 * at the requested speed, etc. If `local' is true, set CLOCAL
517 * regardless of whether the modem option was specified.
520 set_up_tty(fd, local)
526 if (tcgetattr(fd, &tios) < 0) {
527 syslog(LOG_ERR, "tcgetattr: %m");
533 ioctl(fd, TIOCGWINSZ, &wsinfo);
536 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
538 tios.c_cflag |= CRTSCTS;
539 else if (crtscts < 0)
540 tios.c_cflag &= ~CRTSCTS;
542 tios.c_cflag |= CS8 | CREAD | HUPCL;
544 tios.c_cflag |= CLOCAL;
545 tios.c_iflag = IGNBRK | IGNPAR;
549 tios.c_cc[VTIME] = 0;
552 tios.c_iflag |= IXOFF;
553 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
554 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
557 speed = translate_speed(inspeed);
559 cfsetospeed(&tios, speed);
560 cfsetispeed(&tios, speed);
562 speed = cfgetospeed(&tios);
564 * We can't proceed if the serial port speed is B0,
565 * since that implies that the serial port is disabled.
568 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
574 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
575 syslog(LOG_ERR, "tcsetattr: %m");
579 baud_rate = inspeed = baud_rate_of(speed);
584 * restore_tty - restore the terminal to the saved settings.
590 if (!default_device) {
592 * Turn off echoing, because otherwise we can get into
593 * a loop with the tty and the modem echoing to each other.
594 * We presume we are the sole user of this tty device, so
595 * when we close it, it will revert to its defaults anyway.
597 inittermios.c_lflag &= ~(ECHO | ECHONL);
599 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
601 syslog(LOG_WARNING, "tcsetattr: %m");
602 ioctl(fd, TIOCSWINSZ, &wsinfo);
608 * setdtr - control the DTR line on the serial port.
609 * This is called from die(), so it shouldn't call die().
615 int modembits = TIOCM_DTR;
617 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
622 * output - Output PPP packet.
633 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
635 log_packet(p, len, "sent ");
638 str.buf = (caddr_t) p;
639 if (putmsg(fd, NULL, &str, 0) < 0) {
640 if (errno != ENXIO) {
641 syslog(LOG_ERR, "putmsg: %m");
648 * wait_input - wait for input, for a length of time specified in *timo.
652 struct timeval *timo;
657 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
659 pfd.events = POLLIN | POLLPRI | POLLHUP;
660 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
661 syslog(LOG_ERR, "poll: %m");
667 * read_packet - get a PPP packet from the serial device.
673 struct strbuf str, ctl;
675 unsigned char ctlbuf[16];
677 str.maxlen = PPP_MTU + PPP_HDRLEN;
678 str.buf = (caddr_t) buf;
679 ctl.maxlen = sizeof(ctlbuf);
680 ctl.buf = (caddr_t) ctlbuf;
682 len = getmsg(fd, &ctl, &str, &i);
684 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
687 syslog(LOG_ERR, "getmsg(fd) %m");
691 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
693 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
694 ctlbuf[0], ctlbuf[1]);
697 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
706 * ppp_send_config - configure the transmit characteristics of
710 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
718 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
720 if (ioctl(sockfd, (int)SIOCSIPMTU, (caddr_t) &ifr) < 0) {
721 syslog(LOG_ERR, "ioctl(SIOCSIPMTU): %m");
725 if(ioctl(fd, (int)SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
726 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
731 if(ioctl(fd, (int)SIOCSIFCOMPPROT, &c) < 0) {
732 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
737 if(ioctl(fd, (int)SIOCSIFCOMPAC, &c) < 0) {
738 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
745 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
748 ppp_set_xaccm(unit, accm)
752 if (ioctl(fd, (int)SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
753 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
758 * ppp_recv_config - configure the receive-side characteristics of
762 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
769 if (ioctl(fd, (int)SIOCSIFMRU, &mru) < 0) {
770 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
773 if (ioctl(fd, (int)SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
774 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
777 c = 2 + (pcomp? 1: 0);
778 if(ioctl(fd, (int)SIOCSIFCOMPPROT, &c) < 0) {
779 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
782 c = 2 + (accomp? 1: 0);
783 if (ioctl(fd, (int)SIOCSIFCOMPAC, &c) < 0) {
784 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
789 * ccp_test - ask kernel whether a given compression method
790 * is acceptable for use.
793 ccp_test(unit, opt_ptr, opt_len, for_transmit)
794 int unit, opt_len, for_transmit;
797 struct ppp_option_data data;
799 if ((unsigned) opt_len > MAX_PPP_OPTION)
800 opt_len = MAX_PPP_OPTION;
801 data.length = opt_len;
802 data.transmit = for_transmit;
803 BCOPY(opt_ptr, data.opt_data, opt_len);
804 return ioctl(fd, (int)SIOCSCOMPRESS, (caddr_t) &data) >= 0;
808 * ccp_flags_set - inform kernel about the current state of CCP.
811 ccp_flags_set(unit, isopen, isup)
812 int unit, isopen, isup;
816 x = (isopen? 1: 0) + (isup? 2: 0);
817 if (ioctl(fd, (int)SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
818 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
822 * ccp_fatal_error - returns 1 if decompression was disabled as a
823 * result of an error detected after decompression of a packet,
824 * 0 otherwise. This is necessary because of patent nonsense.
827 ccp_fatal_error(unit)
832 if (ioctl(fd, (int)SIOCGIFCOMP, (caddr_t) &x) < 0) {
833 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
836 return x & CCP_FATALERROR;
840 * sifvjcomp - config tcp header compression
843 sifvjcomp(u, vjcomp, cidcomp, maxcid)
844 int u, vjcomp, cidcomp, maxcid;
848 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
849 if (ioctl(fd, (int)SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
850 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
857 * sifup - Config the interface up and enable IP packets to pass.
866 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
867 if (ioctl(sockfd, (int)SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
868 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
871 ifr.ifr_flags |= IFF_UP;
872 if (ioctl(sockfd, (int)SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
873 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
876 npi.protocol = PPP_IP;
877 npi.mode = NPMODE_PASS;
878 if (ioctl(fd, (int)SIOCSETNPMODE, &npi) < 0) {
879 if (errno != ENOTTY) {
880 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
888 * sifdown - Config the interface down.
899 npi.protocol = PPP_IP;
900 npi.mode = NPMODE_ERROR;
901 if (ioctl(fd, (int)SIOCSETNPMODE, (caddr_t) &npi) < 0) {
902 if (errno != ENOTTY) {
903 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
908 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
909 if (ioctl(sockfd, (int)SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
910 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
913 ifr.ifr_flags &= ~IFF_UP;
914 if (ioctl(sockfd, (int)SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
915 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
923 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
925 #define SET_SA_FAMILY(addr, family) \
926 BZERO((char *) &(addr), sizeof(addr)); \
927 addr.sa_family = (family);
930 * sifaddr - Config the interface IP addresses and netmask.
941 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
942 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
943 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
944 if (ioctl(sockfd, (int)SIOCSIFADDR, (caddr_t) &ifr) < 0) {
945 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
948 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
949 if (ioctl(sockfd, (int)SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
950 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
954 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
955 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
956 if (ioctl(sockfd, (int)SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
957 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
962 /* Reset if address --- This is stupid, but seems to be necessary... */
964 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
965 if (ioctl(sockfd, (int)SIOCSIFADDR, (caddr_t) &ifr) < 0) {
966 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
974 * cifaddr - Clear the interface IP addresses, and delete routes
975 * through the interface if possible.
984 SET_SA_FAMILY(rt.rt_dst, AF_INET);
985 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
986 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
987 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
988 rt.rt_flags = RTF_HOST;
989 if (ioctl(sockfd, (int)SIOCDELRT, (caddr_t) &rt) < 0) {
990 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
997 * sifdefaultroute - assign a default route through the address given.
1000 sifdefaultroute(u, g)
1006 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1007 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1008 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1009 rt.rt_flags = RTF_GATEWAY;
1010 if (ioctl(sockfd, (int)SIOCADDRT, &rt) < 0) {
1011 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1018 * cifdefaultroute - delete a default route through the address given.
1021 cifdefaultroute(u, g)
1027 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1028 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1029 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1030 rt.rt_flags = RTF_GATEWAY;
1031 if (ioctl(sockfd, (int)SIOCDELRT, &rt) < 0) {
1032 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1039 * sifproxyarp - Make a proxy ARP entry for the peer.
1042 sifproxyarp(unit, hisaddr)
1046 struct arpreq arpreq;
1048 BZERO(&arpreq, sizeof(arpreq));
1051 * Get the hardware address of an interface on the same subnet
1052 * as our local address.
1054 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1055 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
1059 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1060 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1061 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1062 if (ioctl(sockfd, (int)SIOCSARP, (caddr_t)&arpreq) < 0) {
1063 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1071 * cifproxyarp - Delete the proxy ARP entry for the peer.
1074 cifproxyarp(unit, hisaddr)
1078 struct arpreq arpreq;
1080 BZERO(&arpreq, sizeof(arpreq));
1081 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1082 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1083 if (ioctl(sockfd, (int)SIOCDARP, (caddr_t)&arpreq) < 0) {
1084 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1091 * get_ether_addr - get the hardware address of an interface on the
1092 * the same subnet as ipaddr.
1099 get_ether_addr(ipaddr, hwaddr)
1101 struct sockaddr *hwaddr;
1103 struct ifreq *ifr, *ifend, *ifp;
1105 struct sockaddr_dl *dla;
1108 struct ifreq ifs[MAX_IFS];
1109 struct ifdevea ifdevreq;
1111 ifc.ifc_len = sizeof(ifs);
1113 if (ioctl(sockfd, (int)SIOCGIFCONF, &ifc) < 0) {
1114 perror("ioctl(SIOCGIFCONF)");
1119 * Scan through looking for an interface with an Internet
1120 * address on the same subnet as `ipaddr'.
1122 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1123 for (ifr = ifc.ifc_req; ifr < ifend; ) {
1124 if (ifr->ifr_addr.sa_family == AF_INET) {
1125 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1126 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1128 * Check that the interface is up, and not point-to-point
1131 if (ioctl(sockfd, (int)SIOCGIFFLAGS, &ifreq) < 0)
1133 if ((ifreq.ifr_flags &
1134 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1135 != (IFF_UP|IFF_BROADCAST))
1138 * Get its netmask and check that it's on the right subnet.
1140 if (ioctl(sockfd, (int)SIOCGIFNETMASK, &ifreq) < 0)
1142 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1143 if ((ipaddr & mask) != (ina & mask))
1153 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1155 strncpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name));
1157 if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) {
1158 perror("ioctl(SIOCRPHYSADDR)");
1162 hwaddr->sa_family = AF_UNSPEC;
1163 memcpy(hwaddr->sa_data, ifdevreq.current_pa, sizeof(ifdevreq.current_pa));
1169 * Return user specified netmask, modified by any mask we might determine
1170 * for address `addr' (in network byte order).
1171 * Here we scan through the system's list of interfaces, looking for
1172 * any non-point-to-point interfaces which might appear to be on the same
1173 * network as `addr'. If we find any, we OR in their netmask to the
1174 * user-specified netmask.
1180 u_int32_t mask, nmask, ina;
1181 struct ifreq *ifr, *ifend, ifreq;
1183 struct ifreq ifs[MAX_IFS];
1186 if (IN_CLASSA(addr)) /* determine network mask for address class */
1187 nmask = IN_CLASSA_NET;
1188 else if (IN_CLASSB(addr))
1189 nmask = IN_CLASSB_NET;
1191 nmask = IN_CLASSC_NET;
1192 /* class D nets are disallowed by bad_ip_adrs */
1193 mask = netmask | htonl(nmask);
1196 * Scan through the system's network interfaces.
1198 ifc.ifc_len = sizeof(ifs);
1200 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1201 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1204 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1205 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1207 * Check the interface's internet address.
1209 if (ifr->ifr_addr.sa_family != AF_INET)
1211 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1212 if ((ntohl(ina) & nmask) != (addr & nmask))
1215 * Check that the interface is up, and not point-to-point or loopback.
1217 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1218 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1220 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1224 * Get its netmask and OR it into our mask.
1226 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1228 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1235 #define WTMPFILE "/usr/adm/wtmp"
1236 #include <sys/stat.h>
1239 logwtmp(line, name, host)
1240 char *line, *name, *host;
1246 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1248 if (!fstat(fd, &buf)) {
1249 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1250 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1251 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1252 (void)time(&ut.ut_time);
1253 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1254 (void)ftruncate(fd, buf.st_size);
1260 * Code for locking/unlocking the serial device.
1261 * This code is derived from chat.c.
1267 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1269 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1271 #endif /* LOCK_DIR */
1273 static char *lock_file;
1276 * lock - create a lock file for the named device
1282 char hdb_lock_buffer[12];
1286 if ((p = strrchr(dev, '/')) != NULL)
1288 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1289 if (lock_file == NULL)
1290 novm("lock file name");
1291 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1293 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1295 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1296 /* Read the lock file to find out who has the device locked */
1298 n = read(fd, hdb_lock_buffer, 11);
1300 hdb_lock_buffer[n] = 0;
1301 pid = atoi(hdb_lock_buffer);
1304 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 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1334 write(fd, hdb_lock_buffer, 11);
1337 write(fd, &pid, sizeof pid);
1345 * unlock - remove our lockfile