2 * sys-str.c - System-dependent procedures for setting up
3 * PPP interfaces on systems which use the STREAMS ppp interface.
5 * Copyright (c) 1989 Carnegie Mellon University.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 static char rcsid[] = "$Id: sys-str.c,v 1.20.2.1 1995/06/01 07:01:42 paulus Exp $";
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
43 #include <sys/stream.h>
44 #include <sys/stropts.h>
46 #include <arpa/inet.h>
48 #include <net/ppp_defs.h>
49 #include <net/ppp_str.h>
50 #include <net/route.h>
51 #include <net/if_arp.h>
52 #include <netinet/in.h>
57 #define ifr_mtu ifr_metric
60 #define MAXMODULES 10 /* max number of module names to save */
61 static struct modlist {
62 char modname[FMNAMESZ+1];
63 } str_modules[MAXMODULES];
64 static int str_module_count = 0;
65 static int pushed_ppp;
66 static int closed_stdio;
68 static int ppp_fd = -1; /* fd currently attached to ppp unit */
70 static int restore_term; /* 1 => we've munged the terminal */
71 static struct termios inittermios; /* Initial TTY termios */
73 static int loop_slave = -1;
74 static int loop_master;
75 static char loop_name[20];
77 int sockfd; /* socket for doing interface ioctls */
78 static char *lock_file; /* lock file created for serial port */
81 * sys_init - System-dependent initialization.
86 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
87 setlogmask(LOG_UPTO(LOG_INFO));
89 setlogmask(LOG_UPTO(LOG_DEBUG));
91 /* Get an internet socket for doing socket ioctl's on. */
92 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
93 syslog(LOG_ERR, "Couldn't create IP socket: %m");
99 * note_debug_level - note a change in the debug level.
105 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
106 setlogmask(LOG_UPTO(LOG_DEBUG));
108 setlogmask(LOG_UPTO(LOG_WARNING));
113 * daemon - Detach us from the terminal session.
116 daemon(nochdir, noclose)
117 int nochdir, noclose;
121 if ((pid = fork()) < 0)
124 exit(0); /* parent dies */
129 fclose(stdin); /* don't need stdin, stdout, stderr */
138 * ppp_available - check if this kernel supports PPP.
145 fd = open("/dev/tty", O_RDONLY, 0);
147 return 1; /* can't find out - assume we have ppp */
148 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
155 * establish_ppp - Turn the serial port (or loopback) into a ppp interface.
160 if (fd != loop_slave) {
161 str_module_count = 0;
162 /* go through and save the name of all the modules, then pop em */
164 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
165 ioctl(fd, I_POP, 0) < 0)
167 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
168 str_modules[str_module_count].modname));
173 /* now push the async/fcs module */
174 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
175 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
178 /* push the compress module */
179 if (fd != loop_slave && ioctl(fd, I_PUSH, "pppcomp") < 0) {
180 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
182 /* finally, push the ppp_if module that actually handles the */
183 /* network interface */
184 if (ioctl(fd, I_PUSH, "pppif") < 0) {
185 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
190 /* read mode, message non-discard mode */
191 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
192 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
196 * Find out which interface we were given.
197 * (ppp_if handles this ioctl)
199 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
200 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
204 /* Set debug flags in driver */
205 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
206 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
211 /* close stdin, stdout, stderr if they might refer to the device */
212 if (default_device && !closed_stdio) {
215 for (i = 0; i <= 2; ++i)
216 if (i != fd && i != sockfd)
223 * transfer_ppp - make the device on fd `fd' take over the PPP interface
224 * unit that we are using.
231 return; /* we can't get here */
233 if (fd != loop_slave) {
234 /* go through and save the name of all the modules, then pop em */
235 str_module_count = 0;
237 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
238 ioctl(fd, I_POP, 0) < 0)
240 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
241 str_modules[str_module_count].modname));
246 /* now push the async/fcs module */
247 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
248 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
251 /* push the compress module */
252 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
253 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
255 /* finally, push the ppp_if module that actually handles the */
256 /* network interface */
257 if (ioctl(fd, I_PUSH, "pppif") < 0) {
258 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
263 /* read mode, message non-discard mode */
264 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
265 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
269 * Connect to the interface unit we want.
270 * (ppp_if handles this ioctl)
272 if (ioctl(fd, SIOCSETU, &ifunit) < 0) {
273 syslog(LOG_ERR, "ioctl(SIOCSETU): %m");
277 /* Set debug flags in driver */
278 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
279 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
283 * Pop the modules off the old ppp stream, and restore
284 * the modules that were there, if this is the real serial port.
286 while (ioctl(ppp_fd, I_POP, 0) == 0) /* pop any we pushed */
288 if (fd == loop_slave) {
289 for (; str_module_count > 0; str_module_count--) {
290 if (ioctl(ppp_fd, I_PUSH, str_modules[str_module_count-1].modname)) {
293 "Couldn't restore STREAMS module %s: %m",
294 str_modules[str_module_count-1].modname);
302 * disestablish_ppp - Restore the serial port to normal operation.
303 * It attempts to reconstruct the stream with the previously popped
304 * modules. This shouldn't call die() because it's called from die().
311 /* we can't push or pop modules after the stream has hung up */
312 str_module_count = 0;
313 restore_term = 0; /* nor can we fix up terminal settings */
317 if (fd == ppp_fd && fd >= 0 && pushed_ppp) {
318 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
322 for (; str_module_count > 0; str_module_count--) {
323 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
326 "Couldn't restore STREAMS module %s: %m",
327 str_modules[str_module_count-1].modname);
336 * Check whether the link seems not to be 8-bit clean.
344 if (ioctl(ppp_fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
346 switch (~flags & PAI_FLAGS_HIBITS) {
348 s = "bit 7 set to 1";
351 s = "bit 7 set to 0";
353 case PAI_FLAGS_PAR_EVEN:
356 case PAI_FLAGS_PAR_ODD:
361 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
362 syslog(LOG_WARNING, "All received characters had %s", s);
368 * List of valid speeds.
371 int speed_int, speed_val;
443 * Translate from bits/second to a speed_t.
449 struct speed *speedp;
453 for (speedp = speeds; speedp->speed_int; speedp++)
454 if (bps == speedp->speed_int)
455 return speedp->speed_val;
456 syslog(LOG_WARNING, "speed %d not supported", bps);
461 * Translate from a speed_t to bits/second.
467 struct speed *speedp;
471 for (speedp = speeds; speedp->speed_int; speedp++)
472 if (speed == speedp->speed_val)
473 return speedp->speed_int;
478 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
479 * at the requested speed, etc. If `local' is true, set CLOCAL
480 * regardless of whether the modem option was specified.
482 set_up_tty(fd, local)
488 if (tcgetattr(fd, &tios) < 0) {
489 syslog(LOG_ERR, "tcgetattr: %m");
496 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
498 tios.c_cflag |= CRTSCTS;
499 else if (crtscts < 0)
500 tios.c_cflag &= ~CRTSCTS;
502 tios.c_cflag |= CS8 | CREAD | HUPCL;
504 tios.c_cflag |= CLOCAL;
505 tios.c_iflag = IGNBRK | IGNPAR;
509 tios.c_cc[VTIME] = 0;
512 tios.c_iflag |= IXOFF;
513 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
514 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
517 speed = translate_speed(inspeed);
519 cfsetospeed(&tios, speed);
520 cfsetispeed(&tios, speed);
522 speed = cfgetospeed(&tios);
524 * We can't proceed if the serial port speed is B0,
525 * since that implies that the serial port is disabled.
528 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
534 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
535 syslog(LOG_ERR, "tcsetattr: %m");
539 baud_rate = inspeed = baud_rate_of(speed);
544 * restore_tty - restore the terminal to the saved settings.
550 if (!default_device) {
552 * Turn off echoing, because otherwise we can get into
553 * a loop with the tty and the modem echoing to each other.
554 * We presume we are the sole user of this tty device, so
555 * when we close it, it will revert to its defaults anyway.
557 inittermios.c_lflag &= ~(ECHO | ECHONL);
559 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
561 syslog(LOG_WARNING, "tcsetattr: %m");
567 * setdtr - control the DTR line on the serial port.
568 * This is called from die(), so it shouldn't call die().
573 int modembits = TIOCM_DTR;
575 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
580 * open_loopback - open the device we use for getting packets
581 * in demand mode. Here we use a pty.
588 static char c1[] = "pqrstuvwxyzPQRST";
589 static char c2[] = "0123456789abcdef";
594 strcpy(loop_name, "/dev/ptyxx");
599 if ((loop_master = open(loop_name, O_RDWR, 0)) >= 0) {
601 chown(loop_name, geteuid(), getegid());
602 chmod(loop_name, S_IRUSR|S_IWUSR);
603 if ((loop_slave = open(loop_name, O_RDWR, 0)) >= 0)
607 } else if (errno == ENOENT)
615 if (loop_slave < 0) {
616 syslog(LOG_ERR, "No free pty for loopback");
619 SYSDEBUG((LOG_DEBUG, "using %s for loopback", loop_name));
620 if (tcgetattr(loop_slave, &tios) == 0) {
621 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
622 tios.c_cflag |= CS8 | CREAD;
623 tios.c_iflag = IGNPAR;
626 if (tcsetattr(loop_slave, TCSAFLUSH, &tios) < 0)
627 syslog(LOG_WARNING, "couldn't set attributes on loopback: %m");
629 if ((flags = fcntl(loop_slave, F_GETFL)) != -1)
630 if (fcntl(loop_slave, F_SETFL, flags | O_NONBLOCK) == -1)
631 syslog(LOG_WARNING, "couldn't set loopback to nonblock: %m");
632 if ((flags = fcntl(loop_master, F_GETFL)) != -1)
633 if (fcntl(loop_master, F_SETFL, flags | O_NONBLOCK) == -1)
634 syslog(LOG_WARNING, "couldn't set loopback(m) to nonblock: %m");
639 * output - Output PPP packet.
650 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
652 log_packet(p, len, "sent ");
655 str.buf = (caddr_t) p;
656 if (putmsg(fd, NULL, &str, 0) < 0) {
657 if (errno != ENXIO) {
658 syslog(LOG_ERR, "putmsg: %m");
665 * wait_input - wait for input, for a length of time specified in *timo.
668 struct timeval *timo;
673 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
675 pfd.events = POLLIN | POLLPRI | POLLHUP;
676 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
677 syslog(LOG_ERR, "poll: %m");
683 * wait_loop_output - wait until there is data available on the,
684 * loopback, for the length of time specified by *timo (indefinite
687 wait_loop_output(timo)
688 struct timeval *timo;
694 FD_SET(loop_master, &ready);
695 n = select(loop_master + 1, &ready, NULL, &ready, timo);
696 if (n < 0 && errno != EINTR) {
697 syslog(LOG_ERR, "select: %m");
703 * read_packet - get a PPP packet from the serial device.
709 struct strbuf str, ctl;
711 unsigned char ctlbuf[16];
713 str.maxlen = PPP_MTU + PPP_HDRLEN;
714 str.buf = (caddr_t) buf;
715 ctl.maxlen = sizeof(ctlbuf);
716 ctl.buf = (caddr_t) ctlbuf;
718 len = getmsg(fd, &ctl, &str, &i);
720 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
723 syslog(LOG_ERR, "getmsg(fd) %m");
727 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
729 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
730 ctlbuf[0], ctlbuf[1]);
733 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
742 * read_loop_output - get characters from the "bottom" of the loopback.
745 read_loop_output(buf, maxlen)
751 if ((len = read(loop_master, buf, maxlen)) < 0) {
752 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
754 syslog(LOG_ERR, "read from loopback: %m");
756 } else if (len == 0) {
757 syslog(LOG_ERR, "eof on loopback");
765 * ppp_send_config - configure the transmit characteristics of
769 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
777 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
779 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
780 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
784 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
785 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
790 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
791 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
796 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
797 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
804 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
807 ppp_set_xaccm(unit, accm)
811 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
812 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
817 * ppp_recv_config - configure the receive-side characteristics of
821 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
828 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
829 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
832 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
833 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
836 c = 2 + (pcomp? 1: 0);
837 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
838 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
841 c = 2 + (accomp? 1: 0);
842 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
843 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
848 * ccp_test - ask kernel whether a given compression method
849 * is acceptable for use.
851 ccp_test(unit, opt_ptr, opt_len, for_transmit)
852 int unit, opt_len, for_transmit;
855 struct ppp_option_data data;
857 if ((unsigned) opt_len > MAX_PPP_OPTION)
858 opt_len = MAX_PPP_OPTION;
859 data.length = opt_len;
860 data.transmit = for_transmit;
861 BCOPY(opt_ptr, data.opt_data, opt_len);
862 return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
866 * ccp_flags_set - inform kernel about the current state of CCP.
869 ccp_flags_set(unit, isopen, isup)
870 int unit, isopen, isup;
874 x = (isopen? 1: 0) + (isup? 2: 0);
875 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
876 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
880 * ccp_fatal_error - returns 1 if decompression was disabled as a
881 * result of an error detected after decompression of a packet,
882 * 0 otherwise. This is necessary because of patent nonsense.
885 ccp_fatal_error(unit)
890 if (ioctl(fd, SIOCGIFCOMP, (caddr_t) &x) < 0) {
891 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
894 return x & CCP_FATALERROR;
898 * sifvjcomp - config tcp header compression
901 sifvjcomp(u, vjcomp, cidcomp, maxcid)
902 int u, vjcomp, cidcomp, maxcid;
906 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
907 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
908 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
915 * sifup - Config the interface up.
924 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
925 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
926 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
929 ifr.ifr_flags |= IFF_UP;
930 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
931 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
934 npi.protocol = PPP_IP;
935 npi.mode = NPMODE_PASS;
936 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
937 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
944 * sifdown - Config the interface down.
955 npi.protocol = PPP_IP;
956 npi.mode = NPMODE_ERROR;
957 if (ioctl(fd, SIOCSETNPMODE, (caddr_t) &npi) < 0) {
958 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
962 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
963 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
964 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
967 ifr.ifr_flags &= ~IFF_UP;
968 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
969 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
977 * sifnpmode - Set the mode for handling packets for a given NP.
980 sifnpmode(u, proto, mode)
987 npi.protocol = proto;
989 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
990 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
997 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
999 #define SET_SA_FAMILY(addr, family) \
1000 BZERO((char *) &(addr), sizeof(addr)); \
1001 addr.sa_family = (family);
1004 * sifaddr - Config the interface IP addresses and netmask.
1015 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
1016 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
1017 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
1018 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
1019 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
1022 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
1023 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
1024 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
1028 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
1029 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
1030 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
1031 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
1039 * cifaddr - Clear the interface IP addresses, and delete routes
1040 * through the interface if possible.
1049 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1050 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1051 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1052 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1053 rt.rt_flags = RTF_HOST;
1054 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1055 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
1062 * sifdefaultroute - assign a default route through the address given.
1065 sifdefaultroute(u, g)
1071 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1072 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1073 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1074 rt.rt_flags = RTF_GATEWAY;
1075 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1076 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
1083 * cifdefaultroute - delete a default route through the address given.
1086 cifdefaultroute(u, g)
1092 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1093 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1094 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1095 rt.rt_flags = RTF_GATEWAY;
1096 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1097 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
1104 * sifproxyarp - Make a proxy ARP entry for the peer.
1107 sifproxyarp(unit, hisaddr)
1111 struct arpreq arpreq;
1113 BZERO(&arpreq, sizeof(arpreq));
1116 * Get the hardware address of an interface on the same subnet
1117 * as our local address.
1119 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1120 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
1124 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1125 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1126 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1127 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1128 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1136 * cifproxyarp - Delete the proxy ARP entry for the peer.
1139 cifproxyarp(unit, hisaddr)
1143 struct arpreq arpreq;
1145 BZERO(&arpreq, sizeof(arpreq));
1146 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1147 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1148 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1149 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1156 * get_ether_addr - get the hardware address of an interface on the
1157 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
1158 * in the cslip-2.6 distribution, which is subject to the following
1159 * copyright notice (which also applies to logwtmp below):
1161 * Copyright (c) 1990, 1992 The Regents of the University of California.
1162 * All rights reserved.
1164 * Redistribution and use in source and binary forms, with or without
1165 * modification, are permitted provided that: (1) source code distributions
1166 * retain the above copyright notice and this paragraph in its entirety, (2)
1167 * distributions including binary code include the above copyright notice and
1168 * this paragraph in its entirety in the documentation or other materials
1169 * provided with the distribution, and (3) all advertising materials mentioning
1170 * features or use of this software display the following acknowledgement:
1171 * ``This product includes software developed by the University of California,
1172 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1173 * the University nor the names of its contributors may be used to endorse
1174 * or promote products derived from this software without specific prior
1175 * written permission.
1176 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1177 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1178 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1185 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
1191 #include <netinet/in_var.h>
1193 #include <netinet/if_ether.h>
1195 /* Cast a struct sockaddr to a structaddr_in */
1196 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
1198 /* Determine if "bits" is set in "flag" */
1199 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
1201 static struct nlist nl[] = {
1207 static void kread();
1210 get_ether_addr(ipaddr, hwaddr)
1212 struct sockaddr *hwaddr;
1215 register struct ifnet *ifp;
1216 register struct arpcom *ac;
1217 struct arpcom arpcom;
1218 struct in_addr *inp;
1220 register struct ifaddr *ifa;
1221 register struct in_ifaddr *in;
1224 struct in_ifaddr in;
1227 u_int32_t addr, mask;
1229 /* Open kernel memory for reading */
1230 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
1232 syslog(LOG_ERR, "kvm_open: %m");
1236 /* Fetch namelist */
1237 if (kvm_nlist(kd, nl) != 0) {
1238 syslog(LOG_ERR, "kvm_nlist failed");
1243 ifp = &arpcom.ac_if;
1249 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1251 syslog(LOG_ERR, "error reading ifnet addr");
1254 for ( ; addr; addr = (u_int32_t)ifp->if_next) {
1255 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1256 syslog(LOG_ERR, "error reading ifnet");
1260 /* Only look at configured, broadcast interfaces */
1261 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1264 /* This probably can't happen... */
1265 if (ifp->if_addrlist == 0)
1269 /* Get interface ip address */
1271 if (kvm_read(kd, (u_int32_t)ifp->if_addrlist, (char *)&ifaddr,
1272 sizeof(ifaddr)) != sizeof(ifaddr)) {
1273 syslog(LOG_ERR, "error reading ifaddr");
1276 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
1278 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
1281 /* Check if this interface on the right subnet */
1283 mask = in->ia_subnetmask;
1285 mask = ifp->if_subnetmask;
1287 if ((ipaddr & mask) != (inp->s_addr & mask))
1290 /* Copy out the local ethernet address */
1291 hwaddr->sa_family = AF_UNSPEC;
1292 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
1293 sizeof(arpcom.ac_enaddr));
1294 return 1; /* success! */
1297 /* couldn't find one */
1301 #define WTMPFILE "/usr/adm/wtmp"
1304 logwtmp(line, name, host)
1305 char *line, *name, *host;
1311 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1313 if (!fstat(fd, &buf)) {
1314 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1315 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1316 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1317 (void)time(&ut.ut_time);
1318 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1319 (void)ftruncate(fd, buf.st_size);
1325 * Return user specified netmask, modified by any mask we might determine
1326 * for address `addr' (in network byte order).
1327 * Here we scan through the system's list of interfaces, looking for
1328 * any non-point-to-point interfaces which might appear to be on the same
1329 * network as `addr'. If we find any, we OR in their netmask to the
1330 * user-specified netmask.
1338 u_int32_t mask, nmask, ina;
1339 struct ifreq *ifr, *ifend, ifreq;
1341 struct ifreq ifs[MAX_IFS];
1344 if (IN_CLASSA(addr)) /* determine network mask for address class */
1345 nmask = IN_CLASSA_NET;
1346 else if (IN_CLASSB(addr))
1347 nmask = IN_CLASSB_NET;
1349 nmask = IN_CLASSC_NET;
1350 /* class D nets are disallowed by bad_ip_adrs */
1351 mask = netmask | htonl(nmask);
1354 * Scan through the system's network interfaces.
1356 ifc.ifc_len = sizeof(ifs);
1358 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1359 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1362 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1363 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1365 * Check the interface's internet address.
1367 if (ifr->ifr_addr.sa_family != AF_INET)
1369 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1370 if ((ntohl(ina) & nmask) != (addr & nmask))
1373 * Check that the interface is up, and not point-to-point or loopback.
1375 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1376 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1378 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1382 * Get its netmask and OR it into our mask.
1384 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1386 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1393 * Code for locking/unlocking the serial device.
1394 * This code is derived from chat.c.
1397 #if defined(SUNOS) && SUNOS >= 41 && !defined(HDB)
1404 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1406 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1408 #endif /* LOCK_DIR */
1411 * lock - create a lock file for the named device.
1417 char hdb_lock_buffer[12];
1421 if ((p = strrchr(dev, '/')) != NULL)
1423 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1424 if (lock_file == NULL)
1425 novm("lock file name");
1426 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1428 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1430 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1431 /* Read the lock file to find out who has the device locked */
1433 n = read(fd, hdb_lock_buffer, 11);
1435 hdb_lock_buffer[n] = 0;
1436 pid = atoi(hdb_lock_buffer);
1439 n = read(fd, &pid, sizeof(pid));
1442 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1445 if (kill(pid, 0) == -1 && errno == ESRCH) {
1446 /* pid no longer exists - remove the lock file */
1447 if (unlink(lock_file) == 0) {
1449 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1453 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1456 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1461 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1468 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1469 write(fd, hdb_lock_buffer, 11);
1472 write(fd, &pid, sizeof pid);
1480 * unlock - remove our lockfile