2 * System-dependent procedures for pppd under Digital UNIX (OSF/1).
4 * Copyright (c) 1994 The Australian National University.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29 static char rcsid[] = "$Id: sys-osf.c,v 1.21 1999/03/19 01:29:46 paulus Exp $";
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/stream.h>
48 #include <sys/stropts.h>
52 #include <sys/ioctl.h>
54 #include <net/if_dl.h>
55 #include <net/if_arp.h>
56 #include <net/route.h>
57 #include <net/ppp_defs.h>
58 #include <net/pppio.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
65 static int fdmuxid = -1;
69 static int restore_term;
70 static struct termios inittermios;
71 static struct winsize wsinfo; /* Initial window size info */
72 static pid_t tty_sid; /* PID of our session leader */
74 extern u_char inpacket_buf[]; /* borrowed from main.c */
76 static int link_mtu, link_mru;
79 static int tty_nmodules;
80 static char tty_modules[NMODULES][FMNAMESZ+1];
82 static int closed_stdio;
83 static int initfdflags = -1;
84 static int orig_ttyfd = -1;
86 static int if_is_up; /* Interface has been marked up */
87 static u_int32_t ifaddrs[2]; /* local and remote addresses */
88 static u_int32_t default_route_gateway; /* Gateway for default route added */
89 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
91 #define MAX_POLLFDS 32
92 static struct pollfd pollfds[MAX_POLLFDS];
95 /* Prototypes for procedures local to this file. */
96 static int translate_speed __P((int));
97 static int baud_rate_of __P((int));
98 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
99 static int strioctl __P((int, int, void *, int, int));
103 * sys_init - System-dependent initialization.
110 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
111 setlogmask(LOG_UPTO(LOG_INFO));
113 setlogmask(LOG_UPTO(LOG_DEBUG));
115 /* Get an internet socket for doing socket ioctl's on. */
116 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
117 fatal("Couldn't create IP socket: %m");
120 tty_sid = getsid((pid_t)0);
123 * Open the ppp device.
125 pppfd = open("/dev/streams/ppp", O_RDWR | O_NONBLOCK, 0);
127 fatal("Can't open /dev/streams/ppp: %m");
129 x = PPPDBG_LOG + PPPDBG_DRIVER;
130 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
133 /* Assign a new PPA and get its unit number. */
134 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
135 fatal("Can't create new PPP interface: %m");
138 * Open the ppp device again and push the if_ppp module on it.
140 iffd = open("/dev/streams/ppp", O_RDWR, 0);
142 fatal("Can't open /dev/streams/ppp (2): %m");
144 x = PPPDBG_LOG + PPPDBG_DRIVER;
145 strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
147 if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0)
148 fatal("Couldn't attach ppp interface to device: %m");
149 if (ioctl(iffd, I_PUSH, "if_ppp") < 0)
150 fatal("Can't push ppp interface module: %m");
152 x = PPPDBG_LOG + PPPDBG_IF;
153 strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
155 if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0)
156 fatal("Couldn't create ppp interface unit: %m");
158 if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0)
159 fatal("Couldn't bind ppp interface to IP SAP: %m");
165 * sys_cleanup - restore any system state we modified before exiting:
166 * mark the interface down, delete default route and/or proxy arp entry.
167 * This shouldn't call die() because it's called from die().
175 cifaddr(0, ifaddrs[0], ifaddrs[1]);
176 if (default_route_gateway)
177 cifdefaultroute(0, 0, default_route_gateway);
179 cifproxyarp(0, proxy_arp_addr);
183 * sys_close - Clean up in a child process before execing.
195 * sys_check_options - check the options that the user specified
205 * daemon - Detach us from controlling terminal session.
208 daemon(nochdir, noclose)
209 int nochdir, noclose;
213 if ((pid = fork()) < 0)
216 exit(0); /* parent dies */
221 fclose(stdin); /* don't need stdin, stdout, stderr */
229 * ppp_available - check whether the system has any ppp interfaces
236 return stat("/dev/streams/ppp", &buf) >= 0;
239 char pipename[] = "/dev/streams/pipe";
242 * streampipe -- Opens a STREAMS based pipe. Used by streamify().
246 streampipe(int fd[2])
248 if ((fd[0]=open(pipename, O_RDWR)) == -1)
250 else if ((fd[1]=open(pipename, O_RDWR)) == -1) {
253 } else if (ioctl(fd[0], I_PIPE, fd[1]) != 0) {
258 return(ioctl(fd[0], I_PUSH, "pipemod"));
263 * streamify -- Needed for Digital UNIX, since some tty devices are not STREAMS
264 * modules (but ptys are, and pipes can be).
267 #define BUFFSIZE 1000 /* Size of buffer for streamify() */
274 int ret, fret, rret, maxfd;
275 static char buffer[BUFFSIZE];
278 if (streampipe(fdes) != 0)
279 error("streampipe(): %m\n");
280 else if (isastream(fdes[0]) == 1) {
281 if ((fret=fork()) < 0) {
282 error("fork(): %m\n");
283 } else if (fret == 0) {
284 /* Process to forward things from pipe to tty */
285 sigemptyset(&(sa.sa_mask));
286 sa.sa_handler = SIG_DFL;
288 sigaction(SIGHUP, &sa, NULL); /* Go back to default actions */
289 sigaction(SIGINT, &sa, NULL); /* for changed signals. */
290 sigaction(SIGTERM, &sa, NULL);
291 sigaction(SIGCHLD, &sa, NULL);
292 sigaction(SIGUSR1, &sa, NULL);
293 sigaction(SIGUSR2, &sa, NULL);
296 maxfd = (fdes[1]>fd)?fdes[1]:fd;
299 FD_SET(fdes[1], &readfds);
300 FD_SET(fd, &readfds);
301 ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
302 if (FD_ISSET(fd, &readfds)) {
303 rret = read(fd, buffer, BUFFSIZE);
305 SYSDEBUG(("slave died: EOF on tty."));
308 write(fdes[1], buffer, rret);
311 if (FD_ISSET(fdes[1], &readfds)) {
312 rret = read(fdes[1], buffer, BUFFSIZE);
314 SYSDEBUG(("slave died: EOF on pipe."));
317 write(fd, buffer, rret);
332 * establish_ppp - Turn the serial port into a ppp interface.
340 if (isastream(fd) != 1) {
341 if ((ttyfd = fd = streamify(fd)) < 0)
342 fatal("Couldn't get a STREAMS module!\n");
345 /* Pop any existing modules off the tty stream. */
347 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
348 || ioctl(fd, I_POP, 0) < 0)
350 error("popping module %s\n", tty_modules[i]);
355 /* Push the async hdlc module and the compressor module. */
356 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0)
357 fatal("Couldn't push PPP Async HDLC module: %m");
358 if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
359 error("Couldn't push PPP compression module: %m");
361 /* read mode, message non-discard mode */
362 if (ioctl(fd, I_SRDOPT, RMSGN|RPROTNORM) < 0)
363 fatal("ioctl(I_SRDOPT, RMSGN): %m");
365 /* Link the serial port under the PPP multiplexor. */
366 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0)
367 fatal("Can't link tty to PPP mux: %m");
369 /* close stdin, stdout, stderr if they might refer to the device */
370 if (default_device && !closed_stdio) {
373 for (i = 0; i <= 2; ++i)
374 if (i != fd && i != sockfd)
380 * Set device for non-blocking reads.
381 * XXX why do we need to do this? don't we use pppfd not fd?
383 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
384 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
385 warn("Couldn't set device to non-blocking mode: %m");
392 * restore_loop - reattach the ppp unit to the loopback.
393 * This doesn't need to do anything because disestablish_ppp does it.
401 * disestablish_ppp - Restore the serial port to normal operation.
402 * It attempts to reconstruct the stream with the previously popped
403 * modules. This shouldn't call die() because it's called from die().
412 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
414 error("Can't unlink tty from PPP mux: %m");
418 /* Reset non-blocking mode on the file descriptor. */
419 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
420 warn("Couldn't restore device fd flags: %m");
424 while (ioctl(fd, I_POP, 0) >= 0)
426 for (i = tty_nmodules - 1; i >= 0; --i)
427 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
428 error("Couldn't restore tty module %s: %m",
432 if (hungup && default_device && tty_sid > 0) {
434 * If we have received a hangup, we need to send a SIGHUP
435 * to the terminal's controlling process. The reason is
436 * that the original stream head for the terminal hasn't
437 * seen the M_HANGUP message (it went up through the ppp
438 * driver to the stream head for our fd to /dev/ppp).
440 dbglog("sending hangup to %d", tty_sid);
441 if (kill(tty_sid, SIGHUP) < 0)
442 error("couldn't kill pgrp: %m");
444 if (orig_ttyfd >= 0) {
446 (void)wait((void *)0);
454 * Check whether the link seems not to be 8-bit clean.
462 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
467 s = "bit 7 set to 1";
470 s = "bit 7 set to 0";
480 warn("Serial link is not 8-bit clean:");
481 warn("All received characters had %s", s);
486 * List of valid speeds.
489 int speed_int, speed_val;
561 * Translate from bits/second to a speed_t.
567 struct speed *speedp;
571 for (speedp = speeds; speedp->speed_int; speedp++)
572 if (bps == speedp->speed_int)
573 return speedp->speed_val;
574 warn("speed %d not supported", bps);
579 * Translate from a speed_t to bits/second.
585 struct speed *speedp;
589 for (speedp = speeds; speedp->speed_int; speedp++)
590 if (speed == speedp->speed_val)
591 return speedp->speed_int;
596 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
597 * at the requested speed, etc. If `local' is true, set CLOCAL
598 * regardless of whether the modem option was specified.
601 set_up_tty(fd, local)
607 if (tcgetattr(fd, &tios) < 0)
608 fatal("tcgetattr: %m");
612 ioctl(fd, TIOCGWINSZ, &wsinfo);
615 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
617 tios.c_cflag |= CRTSCTS;
618 else if (crtscts < 0)
619 tios.c_cflag &= ~CRTSCTS;
621 tios.c_cflag |= CS8 | CREAD | HUPCL;
623 tios.c_cflag |= CLOCAL;
624 tios.c_iflag = IGNBRK | IGNPAR;
628 tios.c_cc[VTIME] = 0;
631 tios.c_iflag |= IXON | IXOFF;
632 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
633 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
636 speed = translate_speed(inspeed);
638 cfsetospeed(&tios, speed);
639 cfsetispeed(&tios, speed);
641 speed = cfgetospeed(&tios);
643 * We can't proceed if the serial port speed is 0,
644 * since that implies that the serial port is disabled.
647 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
650 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
651 fatal("tcsetattr: %m");
653 baud_rate = inspeed = baud_rate_of(speed);
658 * restore_tty - restore the terminal to the saved settings.
665 if (!default_device) {
667 * Turn off echoing, because otherwise we can get into
668 * a loop with the tty and the modem echoing to each other.
669 * We presume we are the sole user of this tty device, so
670 * when we close it, it will revert to its defaults anyway.
672 inittermios.c_lflag &= ~(ECHO | ECHONL);
674 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
675 if (!hungup && errno != ENXIO)
676 warn("tcsetattr: %m");
677 ioctl(fd, TIOCSWINSZ, &wsinfo);
683 * setdtr - control the DTR line on the serial port.
684 * This is called from die(), so it shouldn't call die().
690 int modembits = TIOCM_DTR;
692 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
696 * open_loopback - open the device we use for getting packets
697 * in demand mode. Under Digital Unix, we use our existing fd
707 * output - Output PPP packet.
720 dbglog("sent %P", p, len);
723 data.buf = (caddr_t) p;
725 while (putmsg(pppfd, NULL, &data, 0) < 0) {
726 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
728 error("Couldn't send packet: %m");
732 pfd.events = POLLOUT;
733 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
739 * wait_input - wait until there is data available on fd,
740 * for the length of time specified by *timo (indefinite
745 struct timeval *timo;
749 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
750 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
755 * add_fd - add an fd to the set that wait_input waits for.
761 for (n = 0; n < n_pollfds; ++n)
762 if (pollfds[n].fd == fd)
764 if (n_pollfds < MAX_POLLFDS) {
765 pollfds[n_pollfds].fd = fd;
766 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
769 error("Too many inputs!");
773 * remove_fd - remove an fd from the set that wait_input waits for.
775 void remove_fd(int fd)
779 for (n = 0; n < n_pollfds; ++n) {
780 if (pollfds[n].fd == fd) {
781 while (++n < n_pollfds)
782 pollfds[n-1] = pollfds[n];
791 * wait_loop_output - wait until there is data available on the
792 * loopback, for the length of time specified by *timo (indefinite
796 wait_loop_output(timo)
797 struct timeval *timo;
803 * wait_time - wait for a given length of time or until a
804 * signal is received.
808 struct timeval *timo;
812 n = select(0, NULL, NULL, NULL, timo);
813 if (n < 0 && errno != EINTR)
819 * read_packet - get a PPP packet from the serial device.
825 struct strbuf ctrl, data;
827 unsigned char ctrlbuf[64];
830 data.maxlen = PPP_MRU + PPP_HDRLEN;
831 data.buf = (caddr_t) buf;
832 ctrl.maxlen = sizeof(ctrlbuf);
833 ctrl.buf = (caddr_t) ctrlbuf;
835 len = getmsg(pppfd, &ctrl, &data, &flags);
837 if (errno = EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
839 fatal("Error reading packet: %m");
846 * Got a M_PROTO or M_PCPROTO message. Huh?
849 dbglog("got ctrl msg len=%d", ctrl.len);
855 * get_loop_output - get outgoing packets from the ppp device,
856 * and detect when we want to bring the real link up.
857 * Return value is 1 if we need to bring up the link, 0 otherwise.
865 while ((len = read_packet(inpacket_buf)) > 0) {
866 if (loop_frame(inpacket_buf, len))
873 * ppp_send_config - configure the transmit characteristics of
877 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
885 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
886 if (hungup && errno == ENXIO)
888 error("Couldn't set MTU: %m");
890 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
891 error("Couldn't set transmit ACCM: %m");
893 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
894 cf[1] = COMP_PROT | COMP_AC;
895 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
896 error("Couldn't set prot/AC compression: %m");
901 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
904 ppp_set_xaccm(unit, accm)
908 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
909 if (!hungup || errno != ENXIO)
910 warn("Couldn't set extended ACCM: %m");
915 * ppp_recv_config - configure the receive-side characteristics of
919 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
927 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
928 if (hungup && errno == ENXIO)
930 error("Couldn't set MRU: %m");
932 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
933 error("Couldn't set receive ACCM: %m");
935 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
936 cf[1] = DECOMP_PROT | DECOMP_AC;
937 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
938 error("Couldn't set prot/AC decompression: %m");
943 * ccp_test - ask kernel whether a given compression method
944 * is acceptable for use.
946 * In Digital UNIX the memory buckets for chunks >16K are not
947 * primed when the system comes up. That means we're not
948 * likely to get the memory needed for the compressor on
949 * the first try. The way we work around this is to have
950 * the driver spin off a thread to go get the memory for us
951 * (we can't block at that point in a streams context.)
953 * This code synchronizes with the thread when it has returned
954 * with the memory we need. The driver will continue to return
955 * with EAGAIN until the thread comes back. We give up here
956 * if after 10 attempts in one second we still don't have memory.
957 * It's up to the driver to not lose track of that memory if
958 * thread takes too long to return.
961 ccp_test(unit, opt_ptr, opt_len, for_transmit)
962 int unit, opt_len, for_transmit;
969 tval.tv_usec = 100000;
970 for (i = 0; i < 10; ++i) {
971 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
972 opt_ptr, opt_len, 0) >= 0) {
980 error("hard failure trying to get memory for a compressor: %m");
981 return (errno == ENOSR)? 0: -1;
985 * ccp_flags_set - inform kernel about the current state of CCP.
988 ccp_flags_set(unit, isopen, isup)
989 int unit, isopen, isup;
993 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
994 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
995 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
996 if (!hungup || errno != ENXIO)
997 error("Couldn't set kernel CCP state: %m");
1002 * get_idle_time - return how long the link has been idle.
1005 get_idle_time(u, ip)
1007 struct ppp_idle *ip;
1009 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
1014 * ccp_fatal_error - returns 1 if decompression was disabled as a
1015 * result of an error detected after decompression of a packet,
1016 * 0 otherwise. This is necessary because of patent nonsense.
1019 ccp_fatal_error(unit)
1025 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1026 if (errno != ENXIO && errno != EINVAL)
1027 error("Couldn't get compression flags: %m");
1030 return cf[0] & CCP_FATALERROR;
1034 * sifvjcomp - config tcp header compression
1037 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
1038 int u, vjcomp, xcidcomp, xmaxcid;
1044 maxcid[0] = xcidcomp;
1045 maxcid[1] = 15; /* XXX should be rmaxcid */
1046 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
1047 error("Couldn't initialize VJ compression: %m");
1051 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
1052 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1053 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1054 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1056 error("Couldn't enable VJ compression: %m");
1063 * sifup - Config the interface up and enable IP packets to pass.
1071 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1072 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1073 error("Couldn't mark interface up (get): %m");
1076 ifr.ifr_flags |= IFF_UP;
1077 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1078 error("Couldn't mark interface up (set): %m");
1086 * sifdown - Config the interface down and disable IP.
1094 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1095 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1096 error("Couldn't mark interface down (get): %m");
1099 if ((ifr.ifr_flags & IFF_UP) != 0) {
1100 ifr.ifr_flags &= ~IFF_UP;
1101 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1102 error("Couldn't mark interface down (set): %m");
1111 * sifnpmode - Set the mode for handling packets for a given NP.
1114 sifnpmode(u, proto, mode)
1122 npi[1] = (int) mode;
1123 if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
1124 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1130 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1133 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
1135 #define SET_SA_FAMILY(addr, family) \
1136 BZERO((char *) &(addr), sizeof(addr)); \
1137 addr.sa_family = (family); \
1138 addr.sa_len = sizeof ((addr))
1141 * sifaddr - Config the interface IP addresses and netmask.
1149 struct ifaliasreq addreq;
1154 /* flush old address, if any
1156 bzero(&ifr, sizeof (ifr));
1157 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1158 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1159 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1160 if ((ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0)
1161 && errno != EADDRNOTAVAIL) {
1162 error("ioctl(SIOCDIFADDR): %m");
1166 bzero(&addreq, sizeof (addreq));
1167 strlcpy(addreq.ifra_name, ifname, sizeof (addreq.ifra_name));
1168 SET_SA_FAMILY(addreq.ifra_addr, AF_INET);
1169 SET_SA_FAMILY(addreq.ifra_broadaddr, AF_INET);
1170 ((struct sockaddr_in *)&addreq.ifra_addr)->sin_addr.s_addr = o;
1171 ((struct sockaddr_in *)&addreq.ifra_broadaddr)->sin_addr.s_addr = h;
1174 ((struct sockaddr_in *)&addreq.ifra_mask)->sin_addr.s_addr = m;
1175 addreq.ifra_mask.sa_len = sizeof (struct sockaddr);
1176 info("Setting interface mask to %s\n", ip_ntoa(m));
1179 /* install new src/dst and (possibly) netmask
1181 if (ioctl(sockfd, SIOCPIFADDR, &addreq) < 0) {
1182 error("ioctl(SIOCPIFADDR): %m");
1186 ifr.ifr_metric = link_mtu;
1187 if (ioctl(sockfd, SIOCSIPMTU, &ifr) < 0) {
1188 error("Couldn't set IP MTU: %m");
1199 * cifaddr - Clear the interface IP addresses, and delete routes
1200 * through the interface if possible.
1211 bzero(&ifr, sizeof (ifr));
1212 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1213 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1214 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1215 if (ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) {
1216 error("ioctl(SIOCDIFADDR): %m");
1224 * sifdefaultroute - assign a default route through the address given.
1227 sifdefaultroute(u, l, g)
1233 BZERO(&rt, sizeof(rt));
1234 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1235 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1236 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1237 rt.rt_flags = RTF_GATEWAY;
1238 if (ioctl(sockfd, (int)SIOCADDRT, &rt) < 0) {
1239 error("default route ioctl(SIOCADDRT): %m");
1242 default_route_gateway = g;
1248 * cifdefaultroute - delete a default route through the address given.
1251 cifdefaultroute(u, l, g)
1257 BZERO(&rt, sizeof(rt));
1258 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1259 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1260 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1261 rt.rt_flags = RTF_GATEWAY;
1262 if (ioctl(sockfd, (int)SIOCDELRT, &rt) < 0) {
1263 error("default route ioctl(SIOCDELRT): %m");
1266 default_route_gateway = 0;
1271 * sifproxyarp - Make a proxy ARP entry for the peer.
1274 sifproxyarp(unit, hisaddr)
1278 struct arpreq arpreq;
1280 BZERO(&arpreq, sizeof(arpreq));
1283 * Get the hardware address of an interface on the same subnet
1284 * as our local address.
1286 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1287 warn("Cannot determine ethernet address for proxy ARP");
1291 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1292 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1293 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1294 if (ioctl(sockfd, (int)SIOCSARP, (caddr_t)&arpreq) < 0) {
1295 error("ioctl(SIOCSARP): %m");
1299 proxy_arp_addr = hisaddr;
1305 * cifproxyarp - Delete the proxy ARP entry for the peer.
1308 cifproxyarp(unit, hisaddr)
1312 struct arpreq arpreq;
1314 BZERO(&arpreq, sizeof(arpreq));
1315 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1316 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1317 if (ioctl(sockfd, (int)SIOCDARP, (caddr_t)&arpreq) < 0) {
1318 error("ioctl(SIOCDARP): %m");
1326 * get_ether_addr - get the hardware address of an interface on the
1327 * the same subnet as ipaddr.
1332 get_ether_addr(ipaddr, hwaddr)
1334 struct sockaddr *hwaddr;
1336 struct ifreq *ifr, *ifend;
1337 u_int32_t ina, mask;
1340 struct ifreq ifs[MAX_IFS];
1341 struct ifdevea ifdevreq;
1343 ifc.ifc_len = sizeof(ifs);
1345 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1346 error("ioctl(SIOCGIFCONF): %m");
1351 * Scan through looking for an interface with an Internet
1352 * address on the same subnet as `ipaddr'.
1354 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1355 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1356 if (ifr->ifr_addr.sa_family == AF_INET) {
1359 * Check that the interface is up, and not point-to-point
1362 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1363 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1365 if ((ifreq.ifr_flags &
1366 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1367 != (IFF_UP|IFF_BROADCAST))
1371 * Get its netmask and check that it's on the right subnet.
1373 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1375 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1376 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1377 if ((ipaddr & mask) != (ina & mask))
1382 if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
1383 ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
1389 info("found interface %s for proxy arp", ifr->ifr_name);
1391 strlcpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name));
1393 if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) {
1394 perror("ioctl(SIOCRPHYSADDR)");
1398 hwaddr->sa_family = AF_UNSPEC;
1399 memcpy(hwaddr->sa_data, ifdevreq.current_pa, sizeof(ifdevreq.current_pa));
1403 #define WTMPFILE "/usr/adm/wtmp"
1406 logwtmp(line, name, host)
1407 const char *line, *name, *host;
1413 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1415 if (!fstat(fd, &buf)) {
1416 strlcpy(ut.ut_line, line, sizeof(ut.ut_line));
1417 strlcpy(ut.ut_name, name, sizeof(ut.ut_name));
1418 strlcpy(ut.ut_host, host, sizeof(ut.ut_host));
1419 (void)time(&ut.ut_time);
1420 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1421 (void)ftruncate(fd, buf.st_size);
1427 * Return user specified netmask, modified by any mask we might determine
1428 * for address `addr' (in network byte order).
1429 * Here we scan through the system's list of interfaces, looking for
1430 * any non-point-to-point interfaces which might appear to be on the same
1431 * network as `addr'. If we find any, we OR in their netmask to the
1432 * user-specified netmask.
1438 u_int32_t mask, nmask, ina;
1439 struct ifreq *ifr, *ifend, ifreq;
1443 if (IN_CLASSA(addr)) /* determine network mask for address class */
1444 nmask = IN_CLASSA_NET;
1445 else if (IN_CLASSB(addr))
1446 nmask = IN_CLASSB_NET;
1448 nmask = IN_CLASSC_NET;
1449 /* class D nets are disallowed by bad_ip_adrs */
1450 mask = netmask | htonl(nmask);
1453 * Scan through the system's network interfaces.
1455 ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1456 ifc.ifc_req = (struct ifreq *)alloca(ifc.ifc_len);
1457 if (ifc.ifc_req == 0)
1459 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1460 warn("Couldn't get system interface list: %m");
1463 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1464 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1466 * Check the interface's internet address.
1468 if (ifr->ifr_addr.sa_family == AF_INET) {
1469 ina = INET_ADDR(ifr->ifr_addr);
1470 if ((ntohl(ina) & nmask) != (addr & nmask))
1473 * Check that the interface is up, and not point-to-point or loopback.
1475 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1476 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1478 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1482 * Get its netmask and OR it into our mask.
1484 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1486 mask |= INET_ADDR(ifreq.ifr_addr);
1489 if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
1490 ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
1498 * have_route_to - determine if the system has any route to
1499 * a given IP address. `addr' is in network byte order.
1500 * For demand mode to work properly, we have to ignore routes
1501 * through our own interface.
1503 int have_route_to(u_int32_t addr)
1505 char buf[sizeof(struct rt_msghdr) + (sizeof(struct sockaddr_in))];
1508 struct rt_msghdr *rtm;
1509 struct sockaddr_in *sin;
1510 int msglen = sizeof(*rtm) + (sizeof(*sin));
1514 rtm = (struct rt_msghdr *)buf;
1515 memset(rtm, 0, msglen);
1516 rtm->rtm_msglen = msglen;
1517 rtm->rtm_version = RTM_VERSION;
1518 rtm->rtm_type = RTM_GET;
1519 rtm->rtm_addrs = RTA_DST;
1520 /* rtm->rtm_addrs, rtm_flags should be set on output */
1522 sin = (struct sockaddr_in *)((u_char *)rtm + sizeof(*rtm));
1523 sin->sin_len = sizeof(*sin);
1524 sin->sin_family = AF_INET;
1525 sin->sin_addr.s_addr = addr;
1529 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
1531 if (write(s, (char *)rtm, msglen) != msglen) {
1533 return status == ESRCH? 0: -1;
1536 n = read(s, msg, 2048);
1541 rtm = (struct rt_msghdr *) msg;
1542 if (rtm->rtm_version != RTM_VERSION)
1545 /* here we try to work out if the route is through our own interface */
1546 cp = (char *)(rtm + 1);
1547 if (rtm->rtm_addrs & RTA_DST) {
1548 struct sockaddr *sa = (struct sockaddr *) cp;
1549 cp = (char *)(((unsigned long)cp + sa->sa_len
1550 + sizeof(long) - 1) & ~(sizeof(long) - 1));
1552 if (rtm->rtm_addrs & RTA_GATEWAY) {
1553 sin = (struct sockaddr_in *) cp;
1554 if (sin->sin_addr.s_addr == ifaddrs[0]
1555 || sin->sin_addr.s_addr == ifaddrs[1])
1556 return 0; /* route is through our interface */
1563 strioctl(fd, cmd, ptr, ilen, olen)
1564 int fd, cmd, ilen, olen;
1567 struct strioctl str;
1573 if (ioctl(fd, I_STR, &str) == -1)
1575 if (str.ic_len != olen)
1576 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1577 olen, str.ic_len, cmd);
1582 * Use the hostid as part of the random number seed.
1591 * Code for locking/unlocking the serial device.
1592 * This code is derived from chat.c.
1595 #if !defined(HDB) && !defined(SUNOS3)
1596 #define HDB 1 /* ascii lock files are the default */
1602 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1604 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1606 #endif /* LOCK_DIR */
1608 static char *lock_file; /* name of lock file created */
1611 * lock - create a lock file for the named device.
1617 char hdb_lock_buffer[12];
1622 if ((p = strrchr(dev, '/')) != NULL)
1624 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1625 lock_file = malloc(l);
1626 if (lock_file == NULL)
1627 novm("lock file name");
1628 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1630 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1632 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1633 /* Read the lock file to find out who has the device locked */
1635 n = read(fd, hdb_lock_buffer, 11);
1637 hdb_lock_buffer[n] = 0;
1638 pid = atoi(hdb_lock_buffer);
1641 n = read(fd, &pid, sizeof(pid));
1644 error("Can't read pid from lock file %s", lock_file);
1647 if (kill(pid, 0) == -1 && errno == ESRCH) {
1648 /* pid no longer exists - remove the lock file */
1649 if (unlink(lock_file) == 0) {
1651 notice("Removed stale lock on %s (pid %d)",
1655 warn("Couldn't remove stale lock on %s",
1658 notice("Device %s is locked by pid %d",
1663 error("Can't create lock file %s: %m", lock_file);
1670 slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%10d\n", getpid());
1671 write(fd, hdb_lock_buffer, 11);
1674 write(fd, &pid, sizeof pid);
1682 * unlock - remove our lockfile
1695 set_filters(pass, active)
1696 struct bpf_program *pass, *active;
1702 bpf_compile(program, buf, optimize)
1703 struct bpf_program *program;
1717 bpf_filter(pc, p, wirelen, buflen)
1718 struct bpf_insn *pc;