2 * System-dependent procedures for pppd under SunOS 4.
4 * Copyright (c) 1994-2002 Paul Mackerras. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 * Derived from main.c and pppd.h, which are:
37 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
51 * 3. The name "Carnegie Mellon University" must not be used to
52 * endorse or promote products derived from this software without
53 * prior written permission. For permission or any legal
54 * details, please contact
55 * Office of Technology Transfer
56 * Carnegie Mellon University
58 * Pittsburgh, PA 15213-3890
59 * (412) 268-4387, fax: (412) 268-7395
60 * tech-transfer@andrew.cmu.edu
62 * 4. Redistributions of any form whatsoever must retain the following
64 * "This product includes software developed by Computing Services
65 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
67 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
68 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
69 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
70 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
71 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
72 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
73 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
76 #define RCSID "$Id: sys-sunos4.c,v 1.32 2003/04/07 00:01:46 paulus Exp $"
90 #include <sys/types.h>
91 #include <sys/param.h>
92 #include <sys/socket.h>
93 #include <sys/sockio.h>
94 #include <sys/stream.h>
95 #include <sys/stropts.h>
100 #include <net/if_arp.h>
101 #include <net/nit_if.h>
102 #include <net/route.h>
103 #include <net/ppp_defs.h>
104 #include <net/pppio.h>
105 #include <netinet/in.h>
109 #if defined(sun) && defined(sparc)
112 extern void *alloca();
116 static const char rcsid[] = RCSID;
119 static int fdmuxid = -1;
123 static int restore_term;
124 static struct termios inittermios;
125 static struct winsize wsinfo; /* Initial window size info */
126 static pid_t parent_pid; /* PID of our parent */
128 extern u_char inpacket_buf[]; /* borrowed from main.c */
130 #define MAX_POLLFDS 32
131 static struct pollfd pollfds[MAX_POLLFDS];
132 static int n_pollfds;
134 static int link_mtu, link_mru;
137 static int tty_nmodules;
138 static char tty_modules[NMODULES][FMNAMESZ+1];
140 static int if_is_up; /* Interface has been marked up */
141 static u_int32_t ifaddrs[2]; /* local and remote addresses */
142 static u_int32_t default_route_gateway; /* Gateway for default route added */
143 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
145 /* Prototypes for procedures local to this file. */
146 static int translate_speed __P((int));
147 static int baud_rate_of __P((int));
148 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
149 static int strioctl __P((int, int, void *, int, int));
153 * sys_init - System-dependent initialization.
160 /* Get an internet socket for doing socket ioctl's on. */
161 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
162 fatal("Couldn't create IP socket: %m");
165 * We may want to send a SIGHUP to the session leader associated
166 * with our controlling terminal later. Because SunOS doesn't
167 * have getsid(), we make do with sending the signal to our
170 parent_pid = getppid();
173 * Open the ppp device.
175 pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
177 fatal("Can't open /dev/ppp: %m");
179 x = PPPDBG_LOG + PPPDBG_DRIVER;
180 strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
183 /* Assign a new PPA and get its unit number. */
184 if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0)
185 fatal("Can't create new PPP interface: %m");
188 * Open the ppp device again and push the if_ppp module on it.
190 iffd = open("/dev/ppp", O_RDWR, 0);
192 fatal("Can't open /dev/ppp (2): %m");
194 x = PPPDBG_LOG + PPPDBG_DRIVER;
195 strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
197 if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0)
198 fatal("Couldn't attach ppp interface to device: %m");
199 if (ioctl(iffd, I_PUSH, "if_ppp") < 0)
200 fatal("Can't push ppp interface module: %m");
202 x = PPPDBG_LOG + PPPDBG_IF;
203 strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
205 if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0)
206 fatal("Couldn't create ppp interface unit: %m");
208 if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0)
209 fatal("Couldn't bind ppp interface to IP SAP: %m");
215 * sys_cleanup - restore any system state we modified before exiting:
216 * mark the interface down, delete default route and/or proxy arp entry.
217 * This shouldn't call die() because it's called from die().
225 cifaddr(0, ifaddrs[0], ifaddrs[1]);
226 if (default_route_gateway)
227 cifdefaultroute(0, 0, default_route_gateway);
229 cifproxyarp(0, proxy_arp_addr);
233 * sys_close - Clean up in a child process before execing.
244 * sys_check_options - check the options that the user specified
254 * daemon - Detach us from controlling terminal session.
257 daemon(nochdir, noclose)
258 int nochdir, noclose;
262 if ((pid = fork()) < 0)
265 exit(0); /* parent dies */
270 fclose(stdin); /* don't need stdin, stdout, stderr */
279 * ppp_available - check whether the system has any ppp interfaces
286 return stat("/dev/ppp", &buf) >= 0;
290 * tty_establish_ppp - Turn the serial port into a ppp interface.
293 tty_establish_ppp(fd)
298 /* Pop any existing modules off the tty stream. */
300 if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
301 || ioctl(fd, I_POP, 0) < 0)
305 /* Push the async hdlc module and the compressor module. */
306 if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0)
307 fatal("Couldn't push PPP Async HDLC module: %m");
308 if (ioctl(fd, I_PUSH, "ppp_comp") < 0)
309 error("Couldn't push PPP compression module: %m");
311 /* Link the serial port under the PPP multiplexor. */
312 if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0)
313 fatal("Can't link tty to PPP mux: %m");
319 * disestablish_ppp - Restore the serial port to normal operation.
320 * It attempts to reconstruct the stream with the previously popped
321 * modules. This shouldn't call die() because it's called from die().
324 tty_disestablish_ppp(fd)
330 if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
332 error("Can't unlink tty from PPP mux: %m");
337 while (ioctl(fd, I_POP, 0) >= 0)
339 for (i = tty_nmodules - 1; i >= 0; --i)
340 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
341 error("Couldn't restore tty module %s: %m",
344 if (hungup && default_device && parent_pid > 0) {
346 * If we have received a hangup, we need to send a SIGHUP
347 * to the terminal's controlling process. The reason is
348 * that the original stream head for the terminal hasn't
349 * seen the M_HANGUP message (it went up through the ppp
350 * driver to the stream head for our fd to /dev/ppp).
351 * Actually we send the signal to the process that invoked
352 * pppd, since SunOS doesn't have getsid().
354 kill(parent_pid, SIGHUP);
360 * Check whether the link seems not to be 8-bit clean.
368 if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
373 s = "bit 7 set to 1";
376 s = "bit 7 set to 0";
386 warn("Serial link is not 8-bit clean:");
387 warn("All received characters had %s", s);
392 * List of valid speeds.
395 int speed_int, speed_val;
467 * Translate from bits/second to a speed_t.
473 struct speed *speedp;
477 for (speedp = speeds; speedp->speed_int; speedp++)
478 if (bps == speedp->speed_int)
479 return speedp->speed_val;
480 warn("speed %d not supported", bps);
485 * Translate from a speed_t to bits/second.
491 struct speed *speedp;
495 for (speedp = speeds; speedp->speed_int; speedp++)
496 if (speed == speedp->speed_val)
497 return speedp->speed_int;
502 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
503 * at the requested speed, etc. If `local' is true, set CLOCAL
504 * regardless of whether the modem option was specified.
507 set_up_tty(fd, local)
513 if (tcgetattr(fd, &tios) < 0)
514 fatal("tcgetattr: %m");
518 ioctl(fd, TIOCGWINSZ, &wsinfo);
521 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
523 tios.c_cflag |= CRTSCTS;
524 else if (crtscts < 0)
525 tios.c_cflag &= ~CRTSCTS;
527 tios.c_cflag |= CS8 | CREAD | HUPCL;
529 tios.c_cflag |= CLOCAL;
530 tios.c_iflag = IGNBRK | IGNPAR;
534 tios.c_cc[VTIME] = 0;
537 tios.c_iflag |= IXON | IXOFF;
538 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
539 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
542 speed = translate_speed(inspeed);
544 cfsetospeed(&tios, speed);
545 cfsetispeed(&tios, speed);
547 speed = cfgetospeed(&tios);
549 * We can't proceed if the serial port speed is 0,
550 * since that implies that the serial port is disabled.
553 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
556 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
557 fatal("tcsetattr: %m");
559 baud_rate = inspeed = baud_rate_of(speed);
564 * restore_tty - restore the terminal to the saved settings.
571 if (!default_device) {
573 * Turn off echoing, because otherwise we can get into
574 * a loop with the tty and the modem echoing to each other.
575 * We presume we are the sole user of this tty device, so
576 * when we close it, it will revert to its defaults anyway.
578 inittermios.c_lflag &= ~(ECHO | ECHONL);
580 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
581 if (!hungup && errno != ENXIO)
582 warn("tcsetattr: %m");
583 ioctl(fd, TIOCSWINSZ, &wsinfo);
589 * setdtr - control the DTR line on the serial port.
590 * This is called from die(), so it shouldn't call die().
596 int modembits = TIOCM_DTR;
598 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
602 * open_loopback - open the device we use for getting packets
603 * in demand mode. Under SunOS, we use our existing fd
613 * output - Output PPP packet.
625 dump_packet("sent", p, len);
626 if (snoop_send_hook) snoop_send_hook(p, len);
629 data.buf = (caddr_t) p;
631 while (putmsg(pppfd, NULL, &data, 0) < 0) {
632 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
634 error("Couldn't send packet: %m");
638 pfd.events = POLLOUT;
639 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
645 * wait_input - wait until there is data available,
646 * for the length of time specified by *timo (indefinite
651 struct timeval *timo;
655 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
656 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR) {
659 /* we can get EAGAIN on a heavily loaded system,
660 * just wait a short time and try again. */
666 * add_fd - add an fd to the set that wait_input waits for.
673 for (n = 0; n < n_pollfds; ++n)
674 if (pollfds[n].fd == fd)
676 if (n_pollfds < MAX_POLLFDS) {
677 pollfds[n_pollfds].fd = fd;
678 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
681 error("Too many inputs!");
685 * remove_fd - remove an fd from the set that wait_input waits for.
692 for (n = 0; n < n_pollfds; ++n) {
693 if (pollfds[n].fd == fd) {
694 while (++n < n_pollfds)
695 pollfds[n-1] = pollfds[n];
704 * wait_loop_output - wait until there is data available on the
705 * loopback, for the length of time specified by *timo (indefinite
709 wait_loop_output(timo)
710 struct timeval *timo;
716 * wait_time - wait for a given length of time or until a
717 * signal is received.
721 struct timeval *timo;
725 n = select(0, NULL, NULL, NULL, timo);
726 if (n < 0 && errno != EINTR)
732 * read_packet - get a PPP packet from the serial device.
738 struct strbuf ctrl, data;
740 unsigned char ctrlbuf[64];
743 data.maxlen = PPP_MRU + PPP_HDRLEN;
744 data.buf = (caddr_t) buf;
745 ctrl.maxlen = sizeof(ctrlbuf);
746 ctrl.buf = (caddr_t) ctrlbuf;
748 len = getmsg(pppfd, &ctrl, &data, &flags);
750 if (errno == EAGAIN || errno == EINTR)
752 fatal("Error reading packet: %m");
759 * Got a M_PROTO or M_PCPROTO message. Huh?
762 dbglog("got ctrl msg len=%d", ctrl.len);
768 * get_loop_output - get outgoing packets from the ppp device,
769 * and detect when we want to bring the real link up.
770 * Return value is 1 if we need to bring up the link, 0 otherwise.
778 while ((len = read_packet(inpacket_buf)) > 0) {
779 if (loop_frame(inpacket_buf, len))
786 * netif_set_mtu - set the MTU on the PPP network interface.
789 netif_set_mtu(unit, mtu)
794 memset(&ifr, 0, sizeof(ifr));
795 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
796 ifr.ifr_metric = link_mtu;
797 if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
798 error("Couldn't set IP MTU: %m");
803 * tty_send_config - configure the transmit characteristics of
807 tty_send_config(mtu, asyncmap, pcomp, accomp)
815 if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
816 if (hungup && errno == ENXIO) {
820 error("Couldn't set MTU: %m");
822 if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0)
823 error("Couldn't set transmit ACCM: %m");
824 cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
825 cf[1] = COMP_PROT | COMP_AC;
826 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0)
827 error("Couldn't set prot/AC compression: %m");
831 * tty_set_xaccm - set the extended transmit ACCM for the interface.
834 tty_set_xaccm(unit, accm)
838 if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
839 if (!hungup || errno != ENXIO)
840 warn("Couldn't set extended ACCM: %m");
845 * tty_recv_config - configure the receive-side characteristics of
849 tty_recv_config(mru, asyncmap, pcomp, accomp)
857 if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
858 if (hungup && errno == ENXIO) {
862 error("Couldn't set MRU: %m");
864 if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0)
865 error("Couldn't set receive ACCM: %m");
866 cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
867 cf[1] = DECOMP_PROT | DECOMP_AC;
868 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0)
869 error("Couldn't set prot/AC decompression: %m");
873 * ccp_test - ask kernel whether a given compression method
874 * is acceptable for use.
877 ccp_test(unit, opt_ptr, opt_len, for_transmit)
878 int unit, opt_len, for_transmit;
881 if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
882 opt_ptr, opt_len, 0) >= 0)
884 return (errno == ENOSR)? 0: -1;
888 * ccp_flags_set - inform kernel about the current state of CCP.
891 ccp_flags_set(unit, isopen, isup)
892 int unit, isopen, isup;
896 cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
897 cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
898 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
899 if (!hungup || errno != ENXIO)
900 error("Couldn't set kernel CCP state: %m");
905 * get_idle_time - return how long the link has been idle.
912 return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
916 * get_ppp_stats - return statistics for the link.
919 get_ppp_stats(u, stats)
921 struct pppd_stats *stats;
925 if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
926 error("Couldn't get link statistics: %m");
929 stats->bytes_in = s.p.ppp_ibytes;
930 stats->bytes_out = s.p.ppp_obytes;
931 stats->pkts_in = s.p.ppp_ipackets;
932 stats->pkts_out = s.p.ppp_opackets;
938 * ccp_fatal_error - returns 1 if decompression was disabled as a
939 * result of an error detected after decompression of a packet,
940 * 0 otherwise. This is necessary because of patent nonsense.
943 ccp_fatal_error(unit)
949 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
950 if (errno != ENXIO && errno != EINVAL)
951 error("Couldn't get compression flags: %m");
954 return cf[0] & CCP_FATALERROR;
958 * sifvjcomp - config tcp header compression
961 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
962 int u, vjcomp, xcidcomp, xmaxcid;
968 maxcid[0] = xcidcomp;
969 maxcid[1] = 15; /* XXX should be rmaxcid */
970 if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
971 error("Couldn't initialize VJ compression: %m");
975 cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0) /* XXX this is wrong */
976 + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
977 cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
978 if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
980 error("Couldn't enable VJ compression: %m");
987 * sifup - Config the interface up and enable IP packets to pass.
995 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
996 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
997 error("Couldn't mark interface up (get): %m");
1000 ifr.ifr_flags |= IFF_UP;
1001 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1002 error("Couldn't mark interface up (set): %m");
1010 * sifdown - Config the interface down and disable IP.
1018 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1019 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
1020 error("Couldn't mark interface down (get): %m");
1023 if ((ifr.ifr_flags & IFF_UP) != 0) {
1024 ifr.ifr_flags &= ~IFF_UP;
1025 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
1026 error("Couldn't mark interface down (set): %m");
1035 * sifnpmode - Set the mode for handling packets for a given NP.
1038 sifnpmode(u, proto, mode)
1046 npi[1] = (int) mode;
1047 if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
1048 error("ioctl(set NP %d mode to %d): %m", proto, mode);
1054 #define INET_ADDR(x) (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1057 * sifaddr - Config the interface IP addresses and netmask.
1066 memset(&ifr, 0, sizeof(ifr));
1067 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1068 ifr.ifr_addr.sa_family = AF_INET;
1069 INET_ADDR(ifr.ifr_addr) = m;
1070 if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
1071 error("Couldn't set IP netmask: %m");
1073 ifr.ifr_addr.sa_family = AF_INET;
1074 INET_ADDR(ifr.ifr_addr) = o;
1075 if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
1076 error("Couldn't set local IP address: %m");
1078 ifr.ifr_dstaddr.sa_family = AF_INET;
1079 INET_ADDR(ifr.ifr_dstaddr) = h;
1080 if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
1081 error("Couldn't set remote IP address: %m");
1083 #if 0 /* now done in ppp_send_config */
1084 ifr.ifr_metric = link_mtu;
1085 if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
1086 error("Couldn't set IP MTU: %m");
1096 * cifaddr - Clear the interface IP addresses, and delete routes
1097 * through the interface if possible.
1106 bzero(&rt, sizeof(rt));
1107 rt.rt_dst.sa_family = AF_INET;
1108 INET_ADDR(rt.rt_dst) = h;
1109 rt.rt_gateway.sa_family = AF_INET;
1110 INET_ADDR(rt.rt_gateway) = o;
1111 rt.rt_flags = RTF_HOST;
1112 if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1113 error("Couldn't delete route through interface: %m");
1119 * sifdefaultroute - assign a default route through the address given.
1122 sifdefaultroute(u, l, g)
1128 bzero(&rt, sizeof(rt));
1129 rt.rt_dst.sa_family = AF_INET;
1130 INET_ADDR(rt.rt_dst) = 0;
1131 rt.rt_gateway.sa_family = AF_INET;
1132 INET_ADDR(rt.rt_gateway) = g;
1133 rt.rt_flags = RTF_GATEWAY;
1135 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1136 error("Can't add default route: %m");
1140 default_route_gateway = g;
1145 * cifdefaultroute - delete a default route through the address given.
1148 cifdefaultroute(u, l, g)
1154 bzero(&rt, sizeof(rt));
1155 rt.rt_dst.sa_family = AF_INET;
1156 INET_ADDR(rt.rt_dst) = 0;
1157 rt.rt_gateway.sa_family = AF_INET;
1158 INET_ADDR(rt.rt_gateway) = g;
1159 rt.rt_flags = RTF_GATEWAY;
1161 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1162 error("Can't delete default route: %m");
1166 default_route_gateway = 0;
1171 * sifproxyarp - Make a proxy ARP entry for the peer.
1174 sifproxyarp(unit, hisaddr)
1178 struct arpreq arpreq;
1180 bzero(&arpreq, sizeof(arpreq));
1181 if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1184 arpreq.arp_pa.sa_family = AF_INET;
1185 INET_ADDR(arpreq.arp_pa) = hisaddr;
1186 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1187 if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1188 error("Couldn't set proxy ARP entry: %m");
1192 proxy_arp_addr = hisaddr;
1197 * cifproxyarp - Delete the proxy ARP entry for the peer.
1200 cifproxyarp(unit, hisaddr)
1204 struct arpreq arpreq;
1206 bzero(&arpreq, sizeof(arpreq));
1207 arpreq.arp_pa.sa_family = AF_INET;
1208 INET_ADDR(arpreq.arp_pa) = hisaddr;
1209 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1210 error("Couldn't delete proxy ARP entry: %m");
1219 * get_ether_addr - get the hardware address of an interface on the
1220 * the same subnet as ipaddr.
1225 get_ether_addr(ipaddr, hwaddr)
1227 struct sockaddr *hwaddr;
1229 struct ifreq *ifr, *ifend;
1230 u_int32_t ina, mask;
1233 struct ifreq ifs[MAX_IFS];
1236 ifc.ifc_len = sizeof(ifs);
1238 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1239 error("ioctl(SIOCGIFCONF): %m");
1244 * Scan through looking for an interface with an Internet
1245 * address on the same subnet as `ipaddr'.
1247 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1248 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1249 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1250 if (ifr->ifr_addr.sa_family == AF_INET) {
1253 * Check that the interface is up, and not point-to-point
1256 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1257 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1259 if ((ifreq.ifr_flags &
1260 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1261 != (IFF_UP|IFF_BROADCAST))
1265 * Get its netmask and check that it's on the right subnet.
1267 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1269 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1270 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1271 if ((ipaddr & mask) != (ina & mask))
1280 info("found interface %s for proxy arp", ifr->ifr_name);
1283 * Grab the physical address for this interface.
1285 if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1286 error("Couldn't open /dev/nit: %m");
1289 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1290 if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1291 || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1292 error("Couldn't get hardware address for %s: %m",
1298 hwaddr->sa_family = AF_UNSPEC;
1299 memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1305 * have_route_to - determine if the system has any route to
1306 * a given IP address.
1307 * For demand mode to work properly, we have to ignore routes
1308 * through our own interface.
1310 int have_route_to(addr)
1316 #define WTMPFILE "/usr/adm/wtmp"
1319 logwtmp(line, name, host)
1320 const char *line, *name, *host;
1326 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1328 if (!fstat(fd, &buf)) {
1329 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1330 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1331 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1332 (void)time(&ut.ut_time);
1333 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1334 (void)ftruncate(fd, buf.st_size);
1340 * Return user specified netmask, modified by any mask we might determine
1341 * for address `addr' (in network byte order).
1342 * Here we scan through the system's list of interfaces, looking for
1343 * any non-point-to-point interfaces which might appear to be on the same
1344 * network as `addr'. If we find any, we OR in their netmask to the
1345 * user-specified netmask.
1351 u_int32_t mask, nmask, ina;
1352 struct ifreq *ifr, *ifend, ifreq;
1356 if (IN_CLASSA(addr)) /* determine network mask for address class */
1357 nmask = IN_CLASSA_NET;
1358 else if (IN_CLASSB(addr))
1359 nmask = IN_CLASSB_NET;
1361 nmask = IN_CLASSC_NET;
1362 /* class D nets are disallowed by bad_ip_adrs */
1363 mask = netmask | htonl(nmask);
1366 * Scan through the system's network interfaces.
1368 ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1369 ifc.ifc_req = alloca(ifc.ifc_len);
1370 if (ifc.ifc_req == 0)
1372 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1373 warn("Couldn't get system interface list: %m");
1376 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1377 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1379 * Check the interface's internet address.
1381 if (ifr->ifr_addr.sa_family != AF_INET)
1383 ina = INET_ADDR(ifr->ifr_addr);
1384 if ((ntohl(ina) & nmask) != (addr & nmask))
1387 * Check that the interface is up, and not point-to-point or loopback.
1389 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1390 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1392 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1396 * Get its netmask and OR it into our mask.
1398 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1400 mask |= INET_ADDR(ifreq.ifr_addr);
1407 strioctl(fd, cmd, ptr, ilen, olen)
1408 int fd, cmd, ilen, olen;
1411 struct strioctl str;
1417 if (ioctl(fd, I_STR, &str) == -1)
1419 if (str.ic_len != olen)
1420 dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1421 olen, str.ic_len, cmd);
1426 * Use the hostid as part of the random number seed.
1436 * Code for locking/unlocking the serial device.
1437 * This code is derived from chat.c.
1440 #if !defined(HDB) && !defined(SUNOS3)
1441 #define HDB 1 /* ascii lock files are the default */
1447 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1449 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1451 #endif /* LOCK_DIR */
1453 static char *lock_file; /* name of lock file created */
1456 * lock - create a lock file for the named device.
1462 char hdb_lock_buffer[12];
1467 if ((p = strrchr(dev, '/')) != NULL)
1469 l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1470 lock_file = malloc(l);
1471 if (lock_file == NULL)
1472 novm("lock file name");
1473 slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1475 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1477 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1478 /* Read the lock file to find out who has the device locked */
1480 n = read(fd, hdb_lock_buffer, 11);
1482 hdb_lock_buffer[n] = 0;
1483 pid = atoi(hdb_lock_buffer);
1486 n = read(fd, &pid, sizeof(pid));
1489 error("Can't read pid from lock file %s", lock_file);
1492 if (kill(pid, 0) == -1 && errno == ESRCH) {
1493 /* pid no longer exists - remove the lock file */
1494 if (unlink(lock_file) == 0) {
1496 notice("Removed stale lock on %s (pid %d)",
1500 warn("Couldn't remove stale lock on %s",
1503 notice("Device %s is locked by pid %d",
1508 error("Can't create lock file %s: %m", lock_file);
1515 slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%10d\n", getpid());
1516 write(fd, hdb_lock_buffer, 11);
1519 write(fd, &pid, sizeof pid);
1527 * unlock - remove our lockfile
1538 #endif /* lock stuff removed */
1541 * get_pty - get a pty master/slave pair and chown the slave side
1542 * to the uid given. Assumes slave_name points to >= 12 bytes of space.
1545 get_pty(master_fdp, slave_fdp, slave_name, uid)
1553 struct termios tios;
1556 for (i = 0; i < 64; ++i) {
1557 slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
1558 'p' + i / 16, i % 16);
1559 mfd = open(pty_name, O_RDWR, 0);
1562 sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
1571 strlcpy(slave_name, pty_name, 12);
1574 fchown(sfd, uid, -1);
1575 fchmod(sfd, S_IRUSR | S_IWUSR);
1576 if (tcgetattr(sfd, &tios) == 0) {
1577 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1578 tios.c_cflag |= CS8 | CREAD;
1579 tios.c_iflag = IGNPAR | CLOCAL;
1582 if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
1583 warn("couldn't set attributes on pty: %m");
1585 warn("couldn't get attributes on pty: %m");
1591 * SunOS doesn't have strtoul :-(
1594 strtoul(str, ptr, base)
1598 return (unsigned long) strtol(str, ptr, base);
1604 extern char *sys_errlist[];
1605 extern int sys_nerr;
1611 static char unknown[32];
1613 if (n > 0 && n < sys_nerr)
1614 return sys_errlist[n];
1615 slprintf(unknown, sizeof(unknown), "Error %d", n);