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.9 1996/05/26 23:58:46 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 */
70 static int initfdflags = -1; /* Initial file descriptor flags for fd */
72 static int sockfd; /* socket for doing interface ioctls */
74 int orig_ttyfd = -1; /* Original ttyfd if we did a streamify() */
76 static int if_is_up; /* Interface has been marked up */
77 static u_int32_t ifaddrs[2]; /* Local and remote addrs we've set */
78 static u_int32_t default_route_gateway; /* Gateway for default route added */
79 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
81 /* Prototypes for procedures local to this file. */
82 static int translate_speed __P((int));
83 static int baud_rate_of __P((int));
84 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
88 * sys_init - System-dependent initialization.
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 * daemon - Detach us from the terminal session.
130 daemon(nochdir, noclose)
131 int nochdir, noclose;
135 if ((pid = fork()) < 0)
138 exit(0); /* parent dies */
143 fclose(stdin); /* don't need stdin, stdout, stderr */
151 char pipename[] = "/dev/streams/pipe";
154 * ppp_available - check if this kernel supports PPP.
161 fd = open(pipename, O_RDONLY, 0);
163 return 0; /* can't find out - assume we don't have ppp */
164 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
171 * streampipe -- Opens a STREAMS based pipe. Used by streamify().
175 streampipe(int fd[2])
177 if ((fd[0]=open(pipename, O_RDWR)) == -1)
179 else if ((fd[1]=open(pipename, O_RDWR)) == -1) {
182 } else if (ioctl(fd[0], I_PIPE, fd[1]) != 0) {
187 return(ioctl(fd[0], I_PUSH, "pipemod"));
192 * streamify -- Needed for DEC OSF/1, since tty devices are not STREAMS
193 * modules (but ptys are, and pipes can be).
196 #define BUFFSIZE 1000 /* Size of buffer for streamify() */
203 int ret, fret, rret, maxfd;
204 static char buffer[BUFFSIZE];
207 if (streampipe(fdes) != 0)
208 syslog(LOG_ERR, "streampipe(): %m\n");
209 else if (isastream(fdes[0]) == 1) {
210 if ((fret=fork()) < 0) {
211 syslog(LOG_ERR, "fork(): %m\n");
212 } else if (fret == 0) {
213 /* Process to forward things from pipe to tty */
214 sigemptyset(&(sa.sa_mask));
215 sa.sa_handler = SIG_DFL;
217 sigaction(SIGHUP, &sa, NULL); /* Go back to default actions */
218 sigaction(SIGINT, &sa, NULL); /* for changed signals. */
219 sigaction(SIGTERM, &sa, NULL);
220 sigaction(SIGCHLD, &sa, NULL);
221 sigaction(SIGUSR1, &sa, NULL);
222 sigaction(SIGUSR2, &sa, NULL);
225 maxfd = (fdes[1]>fd)?fdes[1]:fd;
228 FD_SET(fdes[1], &readfds);
229 FD_SET(fd, &readfds);
230 ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
231 if (FD_ISSET(fd, &readfds)) {
232 rret = read(fd, buffer, BUFFSIZE);
234 MAINDEBUG((LOG_DEBUG, "slave died: EOF on tty."));
237 write(fdes[1], buffer, rret);
240 if (FD_ISSET(fdes[1], &readfds)) {
241 rret = read(fdes[1], buffer, BUFFSIZE);
243 MAINDEBUG((LOG_DEBUG, "slave died: EOF on pipe."));
246 write(fd, buffer, rret);
262 * establish_ppp - Turn the serial port into a ppp interface.
270 if (isastream(fd) != 1) {
271 if ((ttyfd = fd = streamify(fd)) < 0) {
272 syslog(LOG_ERR, "Couldn't get a STREAMS module!\n");
277 /* go through and save the name of all the modules, then pop em */
279 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
280 ioctl(fd, I_POP, 0) < 0)
282 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
283 str_modules[str_module_count].modname));
287 /* now push the async/fcs module */
288 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
289 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
292 /* push the compress module */
293 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
294 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
296 /* finally, push the ppp_if module that actually handles the */
297 /* network interface */
298 if (ioctl(fd, I_PUSH, "pppif") < 0) {
299 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
303 /* read mode, message non-discard mode */
304 if (ioctl(fd, I_SRDOPT, RMSGN|RPROTNORM) < 0) {
305 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
309 * Find out which interface we were given.
310 * (ppp_if handles this ioctl)
312 if (ioctl(fd, (int)SIOCGETU, &ifunit) < 0) {
313 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
317 /* Set debug flags in driver */
318 if (ioctl(fd, (int)SIOCSIFDEBUG, &kdebugflag) < 0) {
319 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
322 /* close stdin, stdout, stderr if they might refer to the device */
323 if (default_device && !closed_stdio) {
326 for (i = 0; i <= 2; ++i)
327 if (i != fd && i != sockfd)
333 * Set device for non-blocking reads.
335 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
336 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
337 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
341 /* Debugging routine.... */
343 void prstrstack(int fd)
346 struct str_mlist names[20];
349 sl.sl_modlist = names;
351 ret = ioctl(fd, I_LIST, &sl);
354 MAINDEBUG((LOG_DEBUG, "Current streams modules:\n"));
355 for (i=0; i<sl.sl_nmods; i++)
356 MAINDEBUG((LOG_DEBUG," %s\n", names[i].l_name));
358 MAINDEBUG((LOG_DEBUG,"I_LIST ioctl() failed.\n"));
364 * disestablish_ppp - Restore the serial port to normal operation.
365 * It attempts to reconstruct the stream with the previously popped
366 * modules. This shouldn't call die() because it's called from die().
375 /* Reset non-blocking mode on the file descriptor. */
376 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
377 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
381 /* we can't push or pop modules after the stream has hung up */
382 str_module_count = 0;
383 restore_term = 0; /* nor can we fix up terminal settings */
388 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
392 for (; str_module_count > 0; str_module_count--) {
393 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
395 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
396 str_modules[str_module_count-1].modname);
398 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
399 str_modules[str_module_count-1].modname));
403 if (orig_ttyfd >= 0) {
413 * clean_check - check whether the link seems to be 8-bit clean.
420 * Check whether the link seems not to be 8-bit clean.
422 if (ioctl(ttyfd, (int)SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
424 switch (~flags & PAI_FLAGS_HIBITS) {
426 s = "bit 7 set to 1";
429 s = "bit 7 set to 0";
431 case PAI_FLAGS_PAR_EVEN:
434 case PAI_FLAGS_PAR_ODD:
439 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
440 syslog(LOG_WARNING, "All received characters had %s", s);
447 * List of valid speeds.
450 int speed_int, speed_val;
522 * Translate from bits/second to a speed_t.
528 struct speed *speedp;
532 for (speedp = speeds; speedp->speed_int; speedp++)
533 if (bps == speedp->speed_int)
534 return speedp->speed_val;
535 syslog(LOG_WARNING, "speed %d not supported", bps);
540 * Translate from a speed_t to bits/second.
546 struct speed *speedp;
550 for (speedp = speeds; speedp->speed_int; speedp++)
551 if (speed == speedp->speed_val)
552 return speedp->speed_int;
557 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
558 * at the requested speed, etc. If `local' is true, set CLOCAL
559 * regardless of whether the modem option was specified.
562 set_up_tty(fd, local)
568 if (tcgetattr(fd, &tios) < 0) {
569 syslog(LOG_ERR, "tcgetattr: %m");
575 ioctl(fd, TIOCGWINSZ, &wsinfo);
578 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
580 tios.c_cflag |= CRTSCTS;
581 else if (crtscts < 0)
582 tios.c_cflag &= ~CRTSCTS;
584 tios.c_cflag |= CS8 | CREAD | HUPCL;
586 tios.c_cflag |= CLOCAL;
587 tios.c_iflag = IGNBRK | IGNPAR;
591 tios.c_cc[VTIME] = 0;
594 tios.c_iflag |= IXON | IXOFF;
595 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
596 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
599 speed = translate_speed(inspeed);
601 cfsetospeed(&tios, speed);
602 cfsetispeed(&tios, speed);
604 speed = cfgetospeed(&tios);
606 * We can't proceed if the serial port speed is B0,
607 * since that implies that the serial port is disabled.
610 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
616 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
617 syslog(LOG_ERR, "tcsetattr: %m");
621 baud_rate = inspeed = baud_rate_of(speed);
626 * restore_tty - restore the terminal to the saved settings.
633 if (!default_device) {
635 * Turn off echoing, because otherwise we can get into
636 * a loop with the tty and the modem echoing to each other.
637 * We presume we are the sole user of this tty device, so
638 * when we close it, it will revert to its defaults anyway.
640 inittermios.c_lflag &= ~(ECHO | ECHONL);
642 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
644 syslog(LOG_WARNING, "tcsetattr: %m");
645 ioctl(fd, TIOCSWINSZ, &wsinfo);
651 * setdtr - control the DTR line on the serial port.
652 * This is called from die(), so it shouldn't call die().
658 int modembits = TIOCM_DTR;
660 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
665 * output - Output PPP packet.
678 log_packet(p, len, "sent ");
681 str.buf = (caddr_t) p;
683 while (putmsg(ttyfd, NULL, &str, 0) < 0) {
684 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
686 syslog(LOG_ERR, "Couldn't send packet: %m");
690 pfd.events = POLLOUT;
691 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
696 * wait_input - wait for input, for a length of time specified in *timo.
700 struct timeval *timo;
705 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
707 pfd.events = POLLIN | POLLPRI | POLLHUP;
708 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
709 syslog(LOG_ERR, "poll: %m");
715 * read_packet - get a PPP packet from the serial device.
721 struct strbuf str, ctl;
723 unsigned char ctlbuf[16];
725 str.maxlen = PPP_MTU + PPP_HDRLEN;
726 str.buf = (caddr_t) buf;
727 ctl.maxlen = sizeof(ctlbuf);
728 ctl.buf = (caddr_t) ctlbuf;
730 len = getmsg(ttyfd, &ctl, &str, &i);
732 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
735 syslog(LOG_ERR, "getmsg %m");
739 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
741 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
742 ctlbuf[0], ctlbuf[1]);
745 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
754 * ppp_send_config - configure the transmit characteristics of
758 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
766 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
768 if (ioctl(sockfd, (int)SIOCSIPMTU, (caddr_t) &ifr) < 0) {
769 syslog(LOG_ERR, "ioctl(SIOCSIPMTU): %m");
773 if(ioctl(ttyfd, (int)SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
774 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
779 if(ioctl(ttyfd, (int)SIOCSIFCOMPPROT, &c) < 0) {
780 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
785 if(ioctl(ttyfd, (int)SIOCSIFCOMPAC, &c) < 0) {
786 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
793 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
796 ppp_set_xaccm(unit, accm)
800 if (ioctl(ttyfd, (int)SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
801 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
806 * ppp_recv_config - configure the receive-side characteristics of
810 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
817 if (ioctl(ttyfd, (int)SIOCSIFMRU, &mru) < 0) {
818 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
821 if (ioctl(ttyfd, (int)SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
822 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
825 c = 2 + (pcomp? 1: 0);
826 if(ioctl(ttyfd, (int)SIOCSIFCOMPPROT, &c) < 0) {
827 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
830 c = 2 + (accomp? 1: 0);
831 if (ioctl(ttyfd, (int)SIOCSIFCOMPAC, &c) < 0) {
832 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
837 * ccp_test - ask kernel whether a given compression method
838 * is acceptable for use.
841 ccp_test(unit, opt_ptr, opt_len, for_transmit)
842 int unit, opt_len, for_transmit;
845 struct ppp_option_data data;
847 if ((unsigned) opt_len > MAX_PPP_OPTION)
848 opt_len = MAX_PPP_OPTION;
849 data.length = opt_len;
850 data.transmit = for_transmit;
851 BCOPY(opt_ptr, data.opt_data, opt_len);
852 if (ioctl(ttyfd, (int)SIOCSCOMPRESS, (caddr_t) &data) >= 0)
854 return (errno == ENOSR)? 0: -1;
858 * ccp_flags_set - inform kernel about the current state of CCP.
861 ccp_flags_set(unit, isopen, isup)
862 int unit, isopen, isup;
866 x = (isopen? 1: 0) + (isup? 2: 0);
867 if (ioctl(ttyfd, (int)SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
868 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
872 * ccp_fatal_error - returns 1 if decompression was disabled as a
873 * result of an error detected after decompression of a packet,
874 * 0 otherwise. This is necessary because of patent nonsense.
877 ccp_fatal_error(unit)
882 if (ioctl(ttyfd, (int)SIOCGIFCOMP, (caddr_t) &x) < 0) {
883 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
886 return x & CCP_FATALERROR;
890 * sifvjcomp - config tcp header compression
893 sifvjcomp(u, vjcomp, cidcomp, maxcid)
894 int u, vjcomp, cidcomp, maxcid;
898 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
899 if (ioctl(ttyfd, (int)SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
900 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
907 * sifup - Config the interface up and enable IP packets to pass.
915 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
916 if (ioctl(sockfd, (int)SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
917 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
920 ifr.ifr_flags |= IFF_UP;
921 if (ioctl(sockfd, (int)SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
922 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
930 * sifdown - Config the interface down.
941 npi.protocol = PPP_IP;
942 npi.mode = NPMODE_ERROR;
943 ioctl(ttyfd, (int)SIOCSETNPMODE, (caddr_t) &npi); /* ignore errors */
945 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
946 if (ioctl(sockfd, (int)SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
947 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
950 ifr.ifr_flags &= ~IFF_UP;
951 if (ioctl(sockfd, (int)SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
952 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
961 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
963 #define SET_SA_FAMILY(addr, family) \
964 BZERO((char *) &(addr), sizeof(addr)); \
965 addr.sa_family = (family); \
966 addr.sa_len = sizeof ((addr))
969 * sifaddr - Config the interface IP addresses and netmask.
977 struct ifaliasreq addreq;
982 /* flush old address, if any
984 bzero(&ifr, sizeof (ifr));
985 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
986 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
987 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
988 if ((ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0)
989 && errno != EADDRNOTAVAIL) {
990 syslog(LOG_ERR, "ioctl(SIOCDIFADDR): %m");
994 bzero(&addreq, sizeof (addreq));
995 strncpy(addreq.ifra_name, ifname, sizeof (addreq.ifra_name));
996 SET_SA_FAMILY(addreq.ifra_addr, AF_INET);
997 SET_SA_FAMILY(addreq.ifra_broadaddr, AF_INET);
998 ((struct sockaddr_in *)&addreq.ifra_addr)->sin_addr.s_addr = o;
999 ((struct sockaddr_in *)&addreq.ifra_broadaddr)->sin_addr.s_addr = h;
1002 ((struct sockaddr_in *)&addreq.ifra_mask)->sin_addr.s_addr = m;
1003 addreq.ifra_mask.sa_len = sizeof (struct sockaddr);
1004 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
1007 /* install new src/dst and (possibly) netmask
1009 if (ioctl(sockfd, SIOCPIFADDR, &addreq) < 0) {
1010 syslog(LOG_ERR, "ioctl(SIOCPIFADDR): %m");
1020 * cifaddr - Clear the interface IP addresses, and delete routes
1021 * through the interface if possible.
1031 bzero(&ifr, sizeof (ifr));
1032 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1033 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1034 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1035 if (ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) {
1036 syslog(LOG_ERR, "ioctl(SIOCDIFADDR): %m");
1043 * sifdefaultroute - assign a default route through the address given.
1046 sifdefaultroute(u, g)
1052 BZERO(&rt, sizeof(rt));
1053 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1054 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1055 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1056 rt.rt_flags = RTF_GATEWAY;
1057 if (ioctl(sockfd, (int)SIOCADDRT, &rt) < 0) {
1058 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1061 default_route_gateway = g;
1066 * cifdefaultroute - delete a default route through the address given.
1069 cifdefaultroute(u, g)
1075 BZERO(&rt, sizeof(rt));
1076 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1077 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1078 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1079 rt.rt_flags = RTF_GATEWAY;
1080 if (ioctl(sockfd, (int)SIOCDELRT, &rt) < 0) {
1081 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1084 default_route_gateway = 0;
1089 * sifproxyarp - Make a proxy ARP entry for the peer.
1092 sifproxyarp(unit, hisaddr)
1096 struct arpreq arpreq;
1098 BZERO(&arpreq, sizeof(arpreq));
1101 * Get the hardware address of an interface on the same subnet
1102 * as our local address.
1104 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1105 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
1109 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1110 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1111 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1112 if (ioctl(sockfd, (int)SIOCSARP, (caddr_t)&arpreq) < 0) {
1113 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1117 proxy_arp_addr = hisaddr;
1122 * cifproxyarp - Delete the proxy ARP entry for the peer.
1125 cifproxyarp(unit, hisaddr)
1129 struct arpreq arpreq;
1131 BZERO(&arpreq, sizeof(arpreq));
1132 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1133 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1134 if (ioctl(sockfd, (int)SIOCDARP, (caddr_t)&arpreq) < 0) {
1135 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1143 * get_ether_addr - get the hardware address of an interface on the
1144 * the same subnet as ipaddr.
1151 get_ether_addr(ipaddr, hwaddr)
1153 struct sockaddr *hwaddr;
1155 struct ifreq *ifr, *ifend, *ifp;
1157 struct sockaddr_dl *dla;
1160 struct ifreq ifs[MAX_IFS];
1161 struct ifdevea ifdevreq;
1163 ifc.ifc_len = sizeof(ifs);
1165 if (ioctl(sockfd, (int)SIOCGIFCONF, &ifc) < 0) {
1166 perror("ioctl(SIOCGIFCONF)");
1171 * Scan through looking for an interface with an Internet
1172 * address on the same subnet as `ipaddr'.
1174 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1175 for (ifr = ifc.ifc_req; ifr < ifend; +ifr = (struct ifreq *)
1176 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len) {
1177 if (ifr->ifr_addr.sa_family == AF_INET) {
1178 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1179 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1181 * Check that the interface is up, and not point-to-point
1184 if (ioctl(sockfd, (int)SIOCGIFFLAGS, &ifreq) < 0)
1186 if ((ifreq.ifr_flags &
1187 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1188 != (IFF_UP|IFF_BROADCAST))
1191 * Get its netmask and check that it's on the right subnet.
1193 if (ioctl(sockfd, (int)SIOCGIFNETMASK, &ifreq) < 0)
1195 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1196 if ((ipaddr & mask) != (ina & mask))
1205 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1207 strncpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name));
1209 if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) {
1210 perror("ioctl(SIOCRPHYSADDR)");
1214 hwaddr->sa_family = AF_UNSPEC;
1215 memcpy(hwaddr->sa_data, ifdevreq.current_pa, sizeof(ifdevreq.current_pa));
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 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1256 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1257 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1259 * Check the interface's internet address.
1261 if (ifr->ifr_addr.sa_family != AF_INET)
1263 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1264 if ((ntohl(ina) & nmask) != (addr & nmask))
1267 * Check that the interface is up, and not point-to-point or loopback.
1269 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1270 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1272 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1276 * Get its netmask and OR it into our mask.
1278 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1280 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1287 #define WTMPFILE "/usr/adm/wtmp"
1288 #include <sys/stat.h>
1291 logwtmp(line, name, host)
1292 char *line, *name, *host;
1298 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1300 if (!fstat(fd, &buf)) {
1301 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1302 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1303 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1304 (void)time(&ut.ut_time);
1305 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1306 (void)ftruncate(fd, buf.st_size);
1312 * Code for locking/unlocking the serial device.
1313 * This code is derived from chat.c.
1319 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1321 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1323 #endif /* LOCK_DIR */
1325 static char *lock_file;
1328 * lock - create a lock file for the named device
1334 char hdb_lock_buffer[12];
1338 if ((p = strrchr(dev, '/')) != NULL)
1340 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1341 if (lock_file == NULL)
1342 novm("lock file name");
1343 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1345 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1347 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1348 /* Read the lock file to find out who has the device locked */
1350 n = read(fd, hdb_lock_buffer, 11);
1352 hdb_lock_buffer[n] = 0;
1353 pid = atoi(hdb_lock_buffer);
1356 n = read(fd, &pid, sizeof(pid));
1359 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1362 if (kill(pid, 0) == -1 && errno == ESRCH) {
1363 /* pid no longer exists - remove the lock file */
1364 if (unlink(lock_file) == 0) {
1366 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1370 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1373 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1378 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1385 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1386 write(fd, hdb_lock_buffer, 11);
1389 write(fd, &pid, sizeof pid);
1397 * unlock - remove our lockfile