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.27 1999/05/12 06:16:16 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 */
230 * ppp_available - check whether the system has any ppp interfaces
237 return stat("/dev/streams/ppp", &buf) >= 0;
240 char pipename[] = "/dev/streams/pipe";
243 * streampipe -- Opens a STREAMS based pipe. Used by streamify().
247 streampipe(int fd[2])
249 if ((fd[0]=open(pipename, O_RDWR)) == -1)
251 else if ((fd[1]=open(pipename, O_RDWR)) == -1) {
254 } else if (ioctl(fd[0], I_PIPE, fd[1]) != 0) {
259 return(ioctl(fd[0], I_PUSH, "pipemod"));
264 * streamify -- Needed for Digital UNIX, since some tty devices are not STREAMS
265 * modules (but ptys are, and pipes can be).
268 #define BUFFSIZE 1000 /* Size of buffer for streamify() */
275 int ret, fret, rret, maxfd;
276 static char buffer[BUFFSIZE];
279 if (streampipe(fdes) != 0)
280 error("streampipe(): %m\n");
281 else if (isastream(fdes[0]) == 1) {
282 if ((fret=fork()) < 0) {
283 error("fork(): %m\n");
284 } else if (fret == 0) {
285 /* Process to forward things from pipe to tty */
286 sigemptyset(&(sa.sa_mask));
287 sa.sa_handler = SIG_DFL;
289 sigaction(SIGHUP, &sa, NULL); /* Go back to default actions */
290 sigaction(SIGINT, &sa, NULL); /* for changed signals. */
291 sigaction(SIGTERM, &sa, NULL);
292 sigaction(SIGCHLD, &sa, NULL);
293 sigaction(SIGUSR1, &sa, NULL);
294 sigaction(SIGUSR2, &sa, NULL);
297 maxfd = (fdes[1]>fd)?fdes[1]:fd;
300 FD_SET(fdes[1], &readfds);
301 FD_SET(fd, &readfds);
302 ret = select(maxfd+1, &readfds, NULL, NULL, NULL);
303 if (FD_ISSET(fd, &readfds)) {
304 rret = read(fd, buffer, BUFFSIZE);
306 SYSDEBUG(("slave died: EOF on tty."));
309 write(fdes[1], buffer, rret);
312 if (FD_ISSET(fdes[1], &readfds)) {
313 rret = read(fdes[1], buffer, BUFFSIZE);
315 SYSDEBUG(("slave died: EOF on pipe."));
318 write(fd, buffer, rret);
333 * establish_ppp - Turn the serial port into a ppp interface.
341 if (isastream(fd) != 1) {
342 if ((ttyfd = fd = streamify(fd)) < 0)
343 fatal("Couldn't get a STREAMS module!\n");
346 /* Pop any existing modules off the tty stream. */
348 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
349 || ioctl(fd, I_POP, 0) < 0)
351 error("popping module %s\n", tty_modules[i]);
356 /* Push the async hdlc module and the compressor module. */
357 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0)
358 fatal("Couldn't push PPP Async HDLC module: %m");
359 if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
360 error("Couldn't push PPP compression module: %m");
362 /* read mode, message non-discard mode */
363 if (ioctl(fd, I_SRDOPT, RMSGN|RPROTNORM) < 0)
364 fatal("ioctl(I_SRDOPT, RMSGN): %m");
366 /* Link the serial port under the PPP multiplexor. */
367 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0)
368 fatal("Can't link tty to PPP mux: %m");
370 /* close stdin, stdout, stderr if they might refer to the device */
371 if (default_device && !closed_stdio) {
374 for (i = 0; i <= 2; ++i)
375 if (i != fd && i != sockfd)
378 /* make sure 0, 1, 2 are open to /dev/null */
379 while ((i = open("/dev/null", O_RDWR)) >= 0) {
388 * Set device for non-blocking reads.
389 * XXX why do we need to do this? don't we use pppfd not fd?
391 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
392 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
393 warn("Couldn't set device to non-blocking mode: %m");
400 * restore_loop - reattach the ppp unit to the loopback.
401 * This doesn't need to do anything because disestablish_ppp does it.
409 * disestablish_ppp - Restore the serial port to normal operation.
410 * It attempts to reconstruct the stream with the previously popped
411 * modules. This shouldn't call die() because it's called from die().
420 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
422 error("Can't unlink tty from PPP mux: %m");
426 /* Reset non-blocking mode on the file descriptor. */
427 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
428 warn("Couldn't restore device fd flags: %m");
432 while (ioctl(fd, I_POP, 0) >= 0)
434 for (i = tty_nmodules - 1; i >= 0; --i)
435 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
436 error("Couldn't restore tty module %s: %m",
440 if (hungup && default_device && tty_sid > 0) {
442 * If we have received a hangup, we need to send a SIGHUP
443 * to the terminal's controlling process. The reason is
444 * that the original stream head for the terminal hasn't
445 * seen the M_HANGUP message (it went up through the ppp
446 * driver to the stream head for our fd to /dev/ppp).
448 dbglog("sending hangup to %d", tty_sid);
449 if (kill(tty_sid, SIGHUP) < 0)
450 error("couldn't kill pgrp: %m");
452 if (orig_ttyfd >= 0) {
454 (void)wait((void *)0);
462 * Check whether the link seems not to be 8-bit clean.
470 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
475 s = "bit 7 set to 1";
478 s = "bit 7 set to 0";
488 warn("Serial link is not 8-bit clean:");
489 warn("All received characters had %s", s);
494 * List of valid speeds.
497 int speed_int, speed_val;
569 * Translate from bits/second to a speed_t.
575 struct speed *speedp;
579 for (speedp = speeds; speedp->speed_int; speedp++)
580 if (bps == speedp->speed_int)
581 return speedp->speed_val;
582 warn("speed %d not supported", bps);
587 * Translate from a speed_t to bits/second.
593 struct speed *speedp;
597 for (speedp = speeds; speedp->speed_int; speedp++)
598 if (speed == speedp->speed_val)
599 return speedp->speed_int;
604 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
605 * at the requested speed, etc. If `local' is true, set CLOCAL
606 * regardless of whether the modem option was specified.
609 set_up_tty(fd, local)
615 if (tcgetattr(fd, &tios) < 0)
616 fatal("tcgetattr: %m");
620 ioctl(fd, TIOCGWINSZ, &wsinfo);
623 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
625 tios.c_cflag |= CRTSCTS;
626 else if (crtscts < 0)
627 tios.c_cflag &= ~CRTSCTS;
629 tios.c_cflag |= CS8 | CREAD | HUPCL;
631 tios.c_cflag |= CLOCAL;
632 tios.c_iflag = IGNBRK | IGNPAR;
636 tios.c_cc[VTIME] = 0;
639 tios.c_iflag |= IXON | IXOFF;
640 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
641 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
644 speed = translate_speed(inspeed);
646 cfsetospeed(&tios, speed);
647 cfsetispeed(&tios, speed);
649 speed = cfgetospeed(&tios);
651 * We can't proceed if the serial port speed is 0,
652 * since that implies that the serial port is disabled.
655 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
658 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
659 fatal("tcsetattr: %m");
661 baud_rate = inspeed = baud_rate_of(speed);
666 * restore_tty - restore the terminal to the saved settings.
673 if (!default_device) {
675 * Turn off echoing, because otherwise we can get into
676 * a loop with the tty and the modem echoing to each other.
677 * We presume we are the sole user of this tty device, so
678 * when we close it, it will revert to its defaults anyway.
680 inittermios.c_lflag &= ~(ECHO | ECHONL);
682 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
683 if (!hungup && errno != ENXIO)
684 warn("tcsetattr: %m");
685 ioctl(fd, TIOCSWINSZ, &wsinfo);
691 * setdtr - control the DTR line on the serial port.
692 * This is called from die(), so it shouldn't call die().
698 int modembits = TIOCM_DTR;
700 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
704 * open_loopback - open the device we use for getting packets
705 * in demand mode. Under Digital Unix, we use our existing fd
715 * output - Output PPP packet.
728 dbglog("sent %P", p, len);
731 data.buf = (caddr_t) p;
733 while (putmsg(pppfd, NULL, &data, 0) < 0) {
734 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
736 error("Couldn't send packet: %m");
740 pfd.events = POLLOUT;
741 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
747 * wait_input - wait until there is data available on fd,
748 * for the length of time specified by *timo (indefinite
753 struct timeval *timo;
757 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
758 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
763 * add_fd - add an fd to the set that wait_input waits for.
770 for (n = 0; n < n_pollfds; ++n)
771 if (pollfds[n].fd == fd)
773 if (n_pollfds < MAX_POLLFDS) {
774 pollfds[n_pollfds].fd = fd;
775 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
778 error("Too many inputs!");
782 * remove_fd - remove an fd from the set that wait_input waits for.
789 for (n = 0; n < n_pollfds; ++n) {
790 if (pollfds[n].fd == fd) {
791 while (++n < n_pollfds)
792 pollfds[n-1] = pollfds[n];
801 * wait_loop_output - wait until there is data available on the
802 * loopback, for the length of time specified by *timo (indefinite
806 wait_loop_output(timo)
807 struct timeval *timo;
813 * wait_time - wait for a given length of time or until a
814 * signal is received.
818 struct timeval *timo;
822 n = select(0, NULL, NULL, NULL, timo);
823 if (n < 0 && errno != EINTR)
829 * read_packet - get a PPP packet from the serial device.
835 struct strbuf ctrl, data;
837 unsigned char ctrlbuf[64];
840 data.maxlen = PPP_MRU + PPP_HDRLEN;
841 data.buf = (caddr_t) buf;
842 ctrl.maxlen = sizeof(ctrlbuf);
843 ctrl.buf = (caddr_t) ctrlbuf;
845 len = getmsg(pppfd, &ctrl, &data, &flags);
847 if (errno = EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
849 fatal("Error reading packet: %m");
856 * Got a M_PROTO or M_PCPROTO message. Huh?
859 dbglog("got ctrl msg len=%d", ctrl.len);
865 * get_loop_output - get outgoing packets from the ppp device,
866 * and detect when we want to bring the real link up.
867 * Return value is 1 if we need to bring up the link, 0 otherwise.
875 while ((len = read_packet(inpacket_buf)) > 0) {
876 if (loop_frame(inpacket_buf, len))
883 * ppp_send_config - configure the transmit characteristics of
887 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
895 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
896 if (hungup && errno == ENXIO)
898 error("Couldn't set MTU: %m");
900 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
901 error("Couldn't set transmit ACCM: %m");
903 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
904 cf[1] = COMP_PROT | COMP_AC;
905 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
906 error("Couldn't set prot/AC compression: %m");
911 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
914 ppp_set_xaccm(unit, accm)
918 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
919 if (!hungup || errno != ENXIO)
920 warn("Couldn't set extended ACCM: %m");
925 * ppp_recv_config - configure the receive-side characteristics of
929 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
937 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
938 if (hungup && errno == ENXIO)
940 error("Couldn't set MRU: %m");
942 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
943 error("Couldn't set receive ACCM: %m");
945 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
946 cf[1] = DECOMP_PROT | DECOMP_AC;
947 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
948 error("Couldn't set prot/AC decompression: %m");
953 * ccp_test - ask kernel whether a given compression method
954 * is acceptable for use.
956 * In Digital UNIX the memory buckets for chunks >16K are not
957 * primed when the system comes up. That means we're not
958 * likely to get the memory needed for the compressor on
959 * the first try. The way we work around this is to have
960 * the driver spin off a thread to go get the memory for us
961 * (we can't block at that point in a streams context.)
963 * This code synchronizes with the thread when it has returned
964 * with the memory we need. The driver will continue to return
965 * with EAGAIN until the thread comes back. We give up here
966 * if after 10 attempts in one second we still don't have memory.
967 * It's up to the driver to not lose track of that memory if
968 * thread takes too long to return.
971 ccp_test(unit, opt_ptr, opt_len, for_transmit)
972 int unit, opt_len, for_transmit;
979 tval.tv_usec = 100000;
980 for (i = 0; i < 10; ++i) {
981 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
982 opt_ptr, opt_len, 0) >= 0) {
990 error("hard failure trying to get memory for a compressor: %m");
991 return (errno == ENOSR)? 0: -1;
995 * ccp_flags_set - inform kernel about the current state of CCP.
998 ccp_flags_set(unit, isopen, isup)
999 int unit, isopen, isup;
1003 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
1004 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
1005 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1006 if (!hungup || errno != ENXIO)
1007 error("Couldn't set kernel CCP state: %m");
1012 * get_idle_time - return how long the link has been idle.
1015 get_idle_time(u, ip)
1017 struct ppp_idle *ip;
1019 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
1023 * get_ppp_stats - return statistics for the link.
1026 get_ppp_stats(u, stats)
1028 struct pppd_stats *stats;
1032 if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
1033 error("Couldn't get link statistics: %m");
1036 stats->bytes_in = s.p.ppp_ibytes;
1037 stats->bytes_out = s.p.ppp_obytes;
1043 * ccp_fatal_error - returns 1 if decompression was disabled as a
1044 * result of an error detected after decompression of a packet,
1045 * 0 otherwise. This is necessary because of patent nonsense.
1048 ccp_fatal_error(unit)
1054 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1055 if (errno != ENXIO && errno != EINVAL)
1056 error("Couldn't get compression flags: %m");
1059 return cf[0] & CCP_FATALERROR;
1063 * sifvjcomp - config tcp header compression
1066 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
1067 int u, vjcomp, xcidcomp, xmaxcid;
1073 maxcid[0] = xcidcomp;
1074 maxcid[1] = 15; /* XXX should be rmaxcid */
1075 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
1076 error("Couldn't initialize VJ compression: %m");
1080 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
1081 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
1082 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
1083 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
1085 error("Couldn't enable VJ compression: %m");
1092 * sifup - Config the interface up and enable IP packets to pass.
1100 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1101 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1102 error("Couldn't mark interface up (get): %m");
1105 ifr.ifr_flags |= IFF_UP;
1106 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1107 error("Couldn't mark interface up (set): %m");
1115 * sifdown - Config the interface down and disable IP.
1123 bzero(&ifr, sizeof(ifr));
1124 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1125 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1126 error("Couldn't mark interface down (get): %m");
1129 if ((ifr.ifr_flags & IFF_UP) != 0) {
1130 ifr.ifr_flags &= ~IFF_UP;
1131 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1132 error("Couldn't mark interface down (set): %m");
1141 * sifnpmode - Set the mode for handling packets for a given NP.
1144 sifnpmode(u, proto, mode)
1152 npi[1] = (int) mode;
1153 if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
1154 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1160 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1163 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
1165 #define SET_SA_FAMILY(addr, family) \
1166 BZERO((char *) &(addr), sizeof(addr)); \
1167 addr.sa_family = (family); \
1168 addr.sa_len = sizeof ((addr))
1171 * sifaddr - Config the interface IP addresses and netmask.
1179 struct ifaliasreq addreq;
1184 /* flush old address, if any
1186 bzero(&ifr, sizeof (ifr));
1187 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1188 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1189 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1190 if ((ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0)
1191 && errno != EADDRNOTAVAIL) {
1192 error("ioctl(SIOCDIFADDR): %m");
1196 bzero(&addreq, sizeof (addreq));
1197 strlcpy(addreq.ifra_name, ifname, sizeof (addreq.ifra_name));
1198 SET_SA_FAMILY(addreq.ifra_addr, AF_INET);
1199 SET_SA_FAMILY(addreq.ifra_broadaddr, AF_INET);
1200 ((struct sockaddr_in *)&addreq.ifra_addr)->sin_addr.s_addr = o;
1201 ((struct sockaddr_in *)&addreq.ifra_broadaddr)->sin_addr.s_addr = h;
1204 ((struct sockaddr_in *)&addreq.ifra_mask)->sin_addr.s_addr = m;
1205 addreq.ifra_mask.sa_len = sizeof (struct sockaddr);
1206 info("Setting interface mask to %s\n", ip_ntoa(m));
1209 /* install new src/dst and (possibly) netmask
1211 if (ioctl(sockfd, SIOCPIFADDR, &addreq) < 0) {
1212 error("ioctl(SIOCPIFADDR): %m");
1216 ifr.ifr_data = (caddr_t)&link_mtu;
1218 if (ioctl(sockfd, SIOCSIPMTU, &ifr) < 0) {
1219 error("Couldn't set IP MTU: %m");
1230 * cifaddr - Clear the interface IP addresses, and delete routes
1231 * through the interface if possible.
1242 bzero(&ifr, sizeof (ifr));
1243 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1244 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1245 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1246 if (ioctl(sockfd, (int)SIOCDIFADDR, (caddr_t) &ifr) < 0) {
1247 error("ioctl(SIOCDIFADDR): %m");
1255 * sifdefaultroute - assign a default route through the address given.
1258 sifdefaultroute(u, l, g)
1264 BZERO(&rt, sizeof(rt));
1265 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1266 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1267 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1268 rt.rt_flags = RTF_GATEWAY;
1269 if (ioctl(sockfd, (int)SIOCADDRT, &rt) < 0) {
1270 error("default route ioctl(SIOCADDRT): %m");
1273 default_route_gateway = g;
1279 * cifdefaultroute - delete a default route through the address given.
1282 cifdefaultroute(u, l, g)
1288 BZERO(&rt, sizeof(rt));
1289 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1290 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1291 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1292 rt.rt_flags = RTF_GATEWAY;
1293 if (ioctl(sockfd, (int)SIOCDELRT, &rt) < 0) {
1294 error("default route ioctl(SIOCDELRT): %m");
1297 default_route_gateway = 0;
1302 * sifproxyarp - Make a proxy ARP entry for the peer.
1305 sifproxyarp(unit, hisaddr)
1309 struct arpreq arpreq;
1311 BZERO(&arpreq, sizeof(arpreq));
1314 * Get the hardware address of an interface on the same subnet
1315 * as our local address.
1317 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1318 warn("Cannot determine ethernet address for proxy ARP");
1322 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1323 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1324 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1325 if (ioctl(sockfd, (int)SIOCSARP, (caddr_t)&arpreq) < 0) {
1326 error("ioctl(SIOCSARP): %m");
1330 proxy_arp_addr = hisaddr;
1336 * cifproxyarp - Delete the proxy ARP entry for the peer.
1339 cifproxyarp(unit, hisaddr)
1343 struct arpreq arpreq;
1345 BZERO(&arpreq, sizeof(arpreq));
1346 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1347 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1348 if (ioctl(sockfd, (int)SIOCDARP, (caddr_t)&arpreq) < 0) {
1349 error("ioctl(SIOCDARP): %m");
1357 * get_ether_addr - get the hardware address of an interface on the
1358 * the same subnet as ipaddr.
1363 get_ether_addr(ipaddr, hwaddr)
1365 struct sockaddr *hwaddr;
1367 struct ifreq *ifr, *ifend;
1368 u_int32_t ina, mask;
1371 struct ifreq ifs[MAX_IFS];
1372 struct ifdevea ifdevreq;
1374 ifc.ifc_len = sizeof(ifs);
1376 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1377 error("ioctl(SIOCGIFCONF): %m");
1382 * Scan through looking for an interface with an Internet
1383 * address on the same subnet as `ipaddr'.
1385 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1386 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1387 if (ifr->ifr_addr.sa_family == AF_INET) {
1390 * Check that the interface is up, and not point-to-point
1393 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1394 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1396 if ((ifreq.ifr_flags &
1397 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1398 != (IFF_UP|IFF_BROADCAST))
1402 * Get its netmask and check that it's on the right subnet.
1404 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1406 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1407 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1408 if ((ipaddr & mask) != (ina & mask))
1413 if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
1414 ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
1420 info("found interface %s for proxy arp", ifr->ifr_name);
1422 strlcpy(ifdevreq.ifr_name, ifr->ifr_name, sizeof(ifdevreq.ifr_name));
1424 if (ioctl(sockfd, (int)SIOCRPHYSADDR, &ifdevreq) < 0) {
1425 perror("ioctl(SIOCRPHYSADDR)");
1429 hwaddr->sa_family = AF_UNSPEC;
1430 memcpy(hwaddr->sa_data, ifdevreq.current_pa, sizeof(ifdevreq.current_pa));
1434 #define WTMPFILE "/usr/adm/wtmp"
1437 logwtmp(line, name, host)
1438 const char *line, *name, *host;
1444 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1446 if (!fstat(fd, &buf)) {
1447 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1448 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1449 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1450 (void)time(&ut.ut_time);
1451 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1452 (void)ftruncate(fd, buf.st_size);
1458 * Return user specified netmask, modified by any mask we might determine
1459 * for address `addr' (in network byte order).
1460 * Here we scan through the system's list of interfaces, looking for
1461 * any non-point-to-point interfaces which might appear to be on the same
1462 * network as `addr'. If we find any, we OR in their netmask to the
1463 * user-specified netmask.
1469 u_int32_t mask, nmask, ina;
1470 struct ifreq *ifr, *ifend, ifreq;
1474 if (IN_CLASSA(addr)) /* determine network mask for address class */
1475 nmask = IN_CLASSA_NET;
1476 else if (IN_CLASSB(addr))
1477 nmask = IN_CLASSB_NET;
1479 nmask = IN_CLASSC_NET;
1480 /* class D nets are disallowed by bad_ip_adrs */
1481 mask = netmask | htonl(nmask);
1484 * Scan through the system's network interfaces.
1486 ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1487 ifc.ifc_req = (struct ifreq *)alloca(ifc.ifc_len);
1488 if (ifc.ifc_req == 0)
1490 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1491 warn("Couldn't get system interface list: %m");
1494 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1495 for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
1497 * Check the interface's internet address.
1499 if (ifr->ifr_addr.sa_family == AF_INET) {
1500 ina = INET_ADDR(ifr->ifr_addr);
1501 if ((ntohl(ina) & nmask) != (addr & nmask))
1504 * Check that the interface is up, and not point-to-point or loopback.
1506 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1507 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1509 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1513 * Get its netmask and OR it into our mask.
1515 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1517 mask |= INET_ADDR(ifreq.ifr_addr);
1520 if (ifr->ifr_addr.sa_len > sizeof (ifr->ifr_addr))
1521 ifr = (struct ifreq *)((caddr_t)ifr + (ifr->ifr_addr.sa_len - sizeof (ifr->ifr_addr)));
1529 * have_route_to - determine if the system has any route to
1530 * a given IP address. `addr' is in network byte order.
1531 * For demand mode to work properly, we have to ignore routes
1532 * through our own interface.
1534 int have_route_to(u_int32_t addr)
1536 char buf[sizeof(struct rt_msghdr) + (sizeof(struct sockaddr_in))];
1539 struct rt_msghdr *rtm;
1540 struct sockaddr_in *sin;
1541 int msglen = sizeof(*rtm) + (sizeof(*sin));
1545 rtm = (struct rt_msghdr *)buf;
1546 memset(rtm, 0, msglen);
1547 rtm->rtm_msglen = msglen;
1548 rtm->rtm_version = RTM_VERSION;
1549 rtm->rtm_type = RTM_GET;
1550 rtm->rtm_addrs = RTA_DST;
1551 /* rtm->rtm_addrs, rtm_flags should be set on output */
1553 sin = (struct sockaddr_in *)((u_char *)rtm + sizeof(*rtm));
1554 sin->sin_len = sizeof(*sin);
1555 sin->sin_family = AF_INET;
1556 sin->sin_addr.s_addr = addr;
1560 if ((s = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
1562 if (write(s, (char *)rtm, msglen) != msglen) {
1564 return status == ESRCH? 0: -1;
1567 n = read(s, msg, 2048);
1572 rtm = (struct rt_msghdr *) msg;
1573 if (rtm->rtm_version != RTM_VERSION)
1576 /* here we try to work out if the route is through our own interface */
1577 cp = (char *)(rtm + 1);
1578 if (rtm->rtm_addrs & RTA_DST) {
1579 struct sockaddr *sa = (struct sockaddr *) cp;
1580 cp = (char *)(((unsigned long)cp + sa->sa_len
1581 + sizeof(long) - 1) & ~(sizeof(long) - 1));
1583 if (rtm->rtm_addrs & RTA_GATEWAY) {
1584 sin = (struct sockaddr_in *) cp;
1585 if (sin->sin_addr.s_addr == ifaddrs[0]
1586 || sin->sin_addr.s_addr == ifaddrs[1])
1587 return 0; /* route is through our interface */
1594 strioctl(fd, cmd, ptr, ilen, olen)
1595 int fd, cmd, ilen, olen;
1598 struct strioctl str;
1604 if (ioctl(fd, I_STR, &str) == -1)
1606 if (str.ic_len != olen)
1607 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1608 olen, str.ic_len, cmd);
1613 * Use the hostid as part of the random number seed.
1622 * get_pty - get a pty master/slave pair and chown the slave side
1623 * to the uid given. Assumes slave_name points to >= 12 bytes of space.
1626 get_pty(master_fdp, slave_fdp, slave_name, uid)
1634 struct termios tios;
1637 for (i = 0; i < 64; ++i) {
1638 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
1639 'p' + i / 16, i % 16);
1640 mfd = open(pty_name, O_RDWR, 0);
1643 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
1652 strlcpy(slave_name, pty_name, 12);
1655 fchown(sfd, uid, -1);
1656 fchmod(sfd, S_IRUSR | S_IWUSR);
1657 if (tcgetattr(sfd, &tios) == 0) {
1658 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1659 tios.c_cflag |= CS8 | CREAD;
1660 tios.c_iflag = IGNPAR | CLOCAL;
1663 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
1664 warn("couldn't set attributes on pty: %m");
1666 warn("couldn't get attributes on pty: %m");
1673 * Code for locking/unlocking the serial device.
1674 * This code is derived from chat.c.
1677 #if !defined(HDB) && !defined(SUNOS3)
1678 #define HDB 1 /* ascii lock files are the default */
1684 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1686 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1688 #endif /* LOCK_DIR */
1690 static char *lock_file; /* name of lock file created */
1693 * lock - create a lock file for the named device.
1699 char hdb_lock_buffer[12];
1704 if ((p = strrchr(dev, '/')) != NULL)
1706 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1707 lock_file = malloc(l);
1708 if (lock_file == NULL)
1709 novm("lock file name");
1710 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1712 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1714 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1715 /* Read the lock file to find out who has the device locked */
1717 n = read(fd, hdb_lock_buffer, 11);
1719 hdb_lock_buffer[n] = 0;
1720 pid = atoi(hdb_lock_buffer);
1723 n = read(fd, &pid, sizeof(pid));
1726 error("Can't read pid from lock file %s", lock_file);
1729 if (kill(pid, 0) == -1 && errno == ESRCH) {
1730 /* pid no longer exists - remove the lock file */
1731 if (unlink(lock_file) == 0) {
1733 notice("Removed stale lock on %s (pid %d)",
1737 warn("Couldn't remove stale lock on %s",
1740 notice("Device %s is locked by pid %d",
1745 error("Can't create lock file %s: %m", lock_file);
1752 slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%10d\n", getpid());
1753 write(fd, hdb_lock_buffer, 11);
1756 write(fd, &pid, sizeof pid);
1764 * unlock - remove our lockfile
1778 set_filters(pass, active)
1779 struct bpf_program *pass, *active;
1785 bpf_compile(program, buf, optimize)
1786 struct bpf_program *program;
1800 bpf_filter(pc, p, wirelen, buflen)
1801 struct bpf_insn *pc;