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.23 1995/08/11 02:36:23 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 <net/nit_if.h>
53 #include <netinet/in.h>
58 #define ifr_mtu ifr_metric
61 #define MAXMODULES 10 /* max number of module names to save */
62 static struct modlist {
63 char modname[FMNAMESZ+1];
64 } str_modules[MAXMODULES];
65 static int str_module_count = 0;
66 static int pushed_ppp;
67 static int closed_stdio;
69 static int restore_term; /* 1 => we've munged the terminal */
70 static struct termios inittermios; /* Initial TTY termios */
71 static struct winsize wsinfo; /* Initial window size info */
73 int sockfd; /* socket for doing interface ioctls */
74 static char *lock_file; /* lock file created for serial port */
77 * sys_init - System-dependent initialization.
82 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
83 setlogmask(LOG_UPTO(LOG_INFO));
85 setlogmask(LOG_UPTO(LOG_DEBUG));
87 /* Get an internet socket for doing socket ioctl's on. */
88 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
89 syslog(LOG_ERR, "Couldn't create IP socket: %m");
95 * note_debug_level - note a change in the debug level.
101 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
102 setlogmask(LOG_UPTO(LOG_DEBUG));
104 setlogmask(LOG_UPTO(LOG_WARNING));
109 * daemon - Detach us from the terminal session.
112 daemon(nochdir, noclose)
113 int nochdir, noclose;
117 if ((pid = fork()) < 0)
120 exit(0); /* parent dies */
125 fclose(stdin); /* don't need stdin, stdout, stderr */
134 * ppp_available - check if this kernel supports PPP.
141 fd = open("/dev/tty", O_RDONLY, 0);
143 return 1; /* can't find out - assume we have ppp */
144 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
151 * establish_ppp - Turn the serial port into a ppp interface.
156 /* go through and save the name of all the modules, then pop em */
158 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
159 ioctl(fd, I_POP, 0) < 0)
161 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
162 str_modules[str_module_count].modname));
166 /* now push the async/fcs module */
167 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
168 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
171 /* push the compress module */
172 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
173 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
175 /* finally, push the ppp_if module that actually handles the */
176 /* network interface */
177 if (ioctl(fd, I_PUSH, "pppif") < 0) {
178 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
182 /* read mode, message non-discard mode */
183 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
184 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
188 * Find out which interface we were given.
189 * (ppp_if handles this ioctl)
191 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
192 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
196 /* Set debug flags in driver */
197 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
198 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
201 /* close stdin, stdout, stderr if they might refer to the device */
202 if (default_device && !closed_stdio) {
205 for (i = 0; i <= 2; ++i)
206 if (i != fd && i != sockfd)
213 * disestablish_ppp - Restore the serial port to normal operation.
214 * It attempts to reconstruct the stream with the previously popped
215 * modules. This shouldn't call die() because it's called from die().
224 /* we can't push or pop modules after the stream has hung up */
225 str_module_count = 0;
226 restore_term = 0; /* nor can we fix up terminal settings */
232 * Check whether the link seems not to be 8-bit clean.
234 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
236 switch (~flags & PAI_FLAGS_HIBITS) {
238 s = "bit 7 set to 1";
241 s = "bit 7 set to 0";
243 case PAI_FLAGS_PAR_EVEN:
246 case PAI_FLAGS_PAR_ODD:
251 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
252 syslog(LOG_WARNING, "All received characters had %s", s);
256 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
259 for (; str_module_count > 0; str_module_count--) {
260 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
262 syslog(LOG_WARNING, "Couldn't restore module %s: %m",
263 str_modules[str_module_count-1].modname);
265 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
266 str_modules[str_module_count-1].modname));
275 * List of valid speeds.
278 int speed_int, speed_val;
350 * Translate from bits/second to a speed_t.
356 struct speed *speedp;
360 for (speedp = speeds; speedp->speed_int; speedp++)
361 if (bps == speedp->speed_int)
362 return speedp->speed_val;
363 syslog(LOG_WARNING, "speed %d not supported", bps);
368 * Translate from a speed_t to bits/second.
374 struct speed *speedp;
378 for (speedp = speeds; speedp->speed_int; speedp++)
379 if (speed == speedp->speed_val)
380 return speedp->speed_int;
385 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
386 * at the requested speed, etc. If `local' is true, set CLOCAL
387 * regardless of whether the modem option was specified.
389 set_up_tty(fd, local)
395 if (tcgetattr(fd, &tios) < 0) {
396 syslog(LOG_ERR, "tcgetattr: %m");
402 ioctl(fd, TIOCGWINSZ, &wsinfo);
405 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
407 tios.c_cflag |= CRTSCTS;
408 else if (crtscts < 0)
409 tios.c_cflag &= ~CRTSCTS;
411 tios.c_cflag |= CS8 | CREAD | HUPCL;
413 tios.c_cflag |= CLOCAL;
414 tios.c_iflag = IGNBRK | IGNPAR;
418 tios.c_cc[VTIME] = 0;
421 tios.c_iflag |= IXOFF;
422 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
423 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
426 speed = translate_speed(inspeed);
428 cfsetospeed(&tios, speed);
429 cfsetispeed(&tios, speed);
431 speed = cfgetospeed(&tios);
433 * We can't proceed if the serial port speed is B0,
434 * since that implies that the serial port is disabled.
437 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
443 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
444 syslog(LOG_ERR, "tcsetattr: %m");
448 baud_rate = inspeed = baud_rate_of(speed);
453 * restore_tty - restore the terminal to the saved settings.
459 if (!default_device) {
461 * Turn off echoing, because otherwise we can get into
462 * a loop with the tty and the modem echoing to each other.
463 * We presume we are the sole user of this tty device, so
464 * when we close it, it will revert to its defaults anyway.
466 inittermios.c_lflag &= ~(ECHO | ECHONL);
468 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
470 syslog(LOG_WARNING, "tcsetattr: %m");
471 ioctl(fd, TIOCSWINSZ, &wsinfo);
477 * setdtr - control the DTR line on the serial port.
478 * This is called from die(), so it shouldn't call die().
483 int modembits = TIOCM_DTR;
485 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
490 * output - Output PPP packet.
501 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
503 log_packet(p, len, "sent ");
506 str.buf = (caddr_t) p;
507 if (putmsg(fd, NULL, &str, 0) < 0) {
508 if (errno != ENXIO) {
509 syslog(LOG_ERR, "putmsg: %m");
516 * wait_input - wait for input, for a length of time specified in *timo.
519 struct timeval *timo;
524 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
526 pfd.events = POLLIN | POLLPRI | POLLHUP;
527 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
528 syslog(LOG_ERR, "poll: %m");
534 * read_packet - get a PPP packet from the serial device.
540 struct strbuf str, ctl;
542 unsigned char ctlbuf[16];
544 str.maxlen = PPP_MTU + PPP_HDRLEN;
545 str.buf = (caddr_t) buf;
546 ctl.maxlen = sizeof(ctlbuf);
547 ctl.buf = (caddr_t) ctlbuf;
549 len = getmsg(fd, &ctl, &str, &i);
551 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
554 syslog(LOG_ERR, "getmsg(fd) %m");
558 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
560 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
561 ctlbuf[0], ctlbuf[1]);
564 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
573 * ppp_send_config - configure the transmit characteristics of
577 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
585 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
587 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
588 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
592 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
593 if (hungup && errno == ENXIO)
595 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
600 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
601 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
606 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
607 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
614 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
617 ppp_set_xaccm(unit, accm)
621 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
622 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
627 * ppp_recv_config - configure the receive-side characteristics of
631 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
638 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
639 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
642 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
643 if (hungup && errno == ENXIO)
645 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
648 c = 2 + (pcomp? 1: 0);
649 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
650 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
653 c = 2 + (accomp? 1: 0);
654 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
655 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
660 * ccp_test - ask kernel whether a given compression method
661 * is acceptable for use.
663 ccp_test(unit, opt_ptr, opt_len, for_transmit)
664 int unit, opt_len, for_transmit;
667 struct ppp_option_data data;
669 if ((unsigned) opt_len > MAX_PPP_OPTION)
670 opt_len = MAX_PPP_OPTION;
671 data.length = opt_len;
672 data.transmit = for_transmit;
673 BCOPY(opt_ptr, data.opt_data, opt_len);
674 return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
678 * ccp_flags_set - inform kernel about the current state of CCP.
681 ccp_flags_set(unit, isopen, isup)
682 int unit, isopen, isup;
686 x = (isopen? 1: 0) + (isup? 2: 0);
687 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY) {
688 if (hungup && errno == ENXIO)
690 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
695 * ccp_fatal_error - returns 1 if decompression was disabled as a
696 * result of an error detected after decompression of a packet,
697 * 0 otherwise. This is necessary because of patent nonsense.
700 ccp_fatal_error(unit)
705 if (ioctl(fd, SIOCGIFCOMP, (caddr_t) &x) < 0) {
706 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
709 return x & CCP_FATALERROR;
713 * sifvjcomp - config tcp header compression
716 sifvjcomp(u, vjcomp, cidcomp, maxcid)
717 int u, vjcomp, cidcomp, maxcid;
721 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
722 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
723 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
730 * sifup - Config the interface up and enable IP packets to pass.
739 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
740 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
741 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
744 ifr.ifr_flags |= IFF_UP;
745 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
746 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
749 npi.protocol = PPP_IP;
750 npi.mode = NPMODE_PASS;
751 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
752 if (errno != ENOTTY) {
753 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
761 * sifdown - Config the interface down.
772 npi.protocol = PPP_IP;
773 npi.mode = NPMODE_ERROR;
774 if (ioctl(fd, SIOCSETNPMODE, (caddr_t) &npi) < 0) {
775 if (errno != ENOTTY && errno != ENXIO) {
776 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
781 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
782 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
783 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
786 if ((ifr.ifr_flags & IFF_UP) != 0) {
787 ifr.ifr_flags &= ~IFF_UP;
788 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
789 syslog(LOG_ERR, "Couldn't mark interface down: %m");
798 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
800 #define SET_SA_FAMILY(addr, family) \
801 BZERO((char *) &(addr), sizeof(addr)); \
802 addr.sa_family = (family);
805 * sifaddr - Config the interface IP addresses and netmask.
816 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
817 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
818 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
819 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
820 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
823 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
824 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
825 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
829 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
830 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
831 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
832 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
840 * cifaddr - Clear the interface IP addresses, and delete routes
841 * through the interface if possible.
850 SET_SA_FAMILY(rt.rt_dst, AF_INET);
851 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
852 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
853 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
854 rt.rt_flags = RTF_HOST;
855 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
856 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
863 * sifdefaultroute - assign a default route through the address given.
866 sifdefaultroute(u, g)
872 SET_SA_FAMILY(rt.rt_dst, AF_INET);
873 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
874 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
875 rt.rt_flags = RTF_GATEWAY;
876 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
877 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
884 * cifdefaultroute - delete a default route through the address given.
887 cifdefaultroute(u, g)
893 SET_SA_FAMILY(rt.rt_dst, AF_INET);
894 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
895 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
896 rt.rt_flags = RTF_GATEWAY;
897 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
898 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
905 * sifproxyarp - Make a proxy ARP entry for the peer.
908 sifproxyarp(unit, hisaddr)
912 struct arpreq arpreq;
914 BZERO(&arpreq, sizeof(arpreq));
917 * Get the hardware address of an interface on the same subnet
918 * as our local address.
920 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
921 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
925 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
926 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
927 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
928 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
929 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
937 * cifproxyarp - Delete the proxy ARP entry for the peer.
940 cifproxyarp(unit, hisaddr)
944 struct arpreq arpreq;
946 BZERO(&arpreq, sizeof(arpreq));
947 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
948 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
949 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
950 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
957 * get_ether_addr - get the hardware address of an interface on the
958 * the same subnet as ipaddr.
963 get_ether_addr(ipaddr, hwaddr)
965 struct sockaddr *hwaddr;
967 struct ifreq *ifr, *ifend;
971 struct ifreq ifs[MAX_IFS];
974 ifc.ifc_len = sizeof(ifs);
976 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
977 syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
982 * Scan through looking for an interface with an Internet
983 * address on the same subnet as `ipaddr'.
985 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
986 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
987 ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
988 if (ifr->ifr_addr.sa_family == AF_INET) {
991 * Check that the interface is up, and not point-to-point
994 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
995 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
997 if ((ifreq.ifr_flags &
998 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
999 != (IFF_UP|IFF_BROADCAST))
1003 * Get its netmask and check that it's on the right subnet.
1005 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1007 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1008 mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1009 if ((ipaddr & mask) != (ina & mask))
1018 syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1021 * Grab the physical address for this interface.
1023 if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1024 syslog(LOG_ERR, "Couldn't open /dev/nit: %m");
1027 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1028 if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1029 || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1030 syslog(LOG_ERR, "Couldn't get hardware address for %s: %m",
1036 hwaddr->sa_family = AF_UNSPEC;
1037 memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1042 #define WTMPFILE "/usr/adm/wtmp"
1045 logwtmp(line, name, host)
1046 char *line, *name, *host;
1052 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1054 if (!fstat(fd, &buf)) {
1055 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1056 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1057 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1058 (void)time(&ut.ut_time);
1059 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1060 (void)ftruncate(fd, buf.st_size);
1066 * Return user specified netmask, modified by any mask we might determine
1067 * for address `addr' (in network byte order).
1068 * Here we scan through the system's list of interfaces, looking for
1069 * any non-point-to-point interfaces which might appear to be on the same
1070 * network as `addr'. If we find any, we OR in their netmask to the
1071 * user-specified netmask.
1079 u_int32_t mask, nmask, ina;
1080 struct ifreq *ifr, *ifend, ifreq;
1082 struct ifreq ifs[MAX_IFS];
1085 if (IN_CLASSA(addr)) /* determine network mask for address class */
1086 nmask = IN_CLASSA_NET;
1087 else if (IN_CLASSB(addr))
1088 nmask = IN_CLASSB_NET;
1090 nmask = IN_CLASSC_NET;
1091 /* class D nets are disallowed by bad_ip_adrs */
1092 mask = netmask | htonl(nmask);
1095 * Scan through the system's network interfaces.
1097 ifc.ifc_len = sizeof(ifs);
1099 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1100 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1103 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1104 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1106 * Check the interface's internet address.
1108 if (ifr->ifr_addr.sa_family != AF_INET)
1110 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1111 if ((ntohl(ina) & nmask) != (addr & nmask))
1114 * Check that the interface is up, and not point-to-point or loopback.
1116 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1117 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1119 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1123 * Get its netmask and OR it into our mask.
1125 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1127 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1134 * Code for locking/unlocking the serial device.
1135 * This code is derived from chat.c.
1138 #if !defined(HDB) && !defined(SUNOS3)
1139 #define HDB 1 /* ascii lock files are the default */
1145 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1147 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1149 #endif /* LOCK_DIR */
1152 * lock - create a lock file for the named device.
1158 char hdb_lock_buffer[12];
1162 if ((p = strrchr(dev, '/')) != NULL)
1164 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1165 if (lock_file == NULL)
1166 novm("lock file name");
1167 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1169 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1171 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1172 /* Read the lock file to find out who has the device locked */
1174 n = read(fd, hdb_lock_buffer, 11);
1176 hdb_lock_buffer[n] = 0;
1177 pid = atoi(hdb_lock_buffer);
1180 n = read(fd, &pid, sizeof(pid));
1183 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1186 if (kill(pid, 0) == -1 && errno == ESRCH) {
1187 /* pid no longer exists - remove the lock file */
1188 if (unlink(lock_file) == 0) {
1190 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1194 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1197 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1202 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1209 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1210 write(fd, hdb_lock_buffer, 11);
1213 write(fd, &pid, sizeof pid);
1221 * unlock - remove our lockfile
1234 * SunOS doesn't have strtoul :-(
1237 strtoul(str, ptr, base)
1241 return (unsigned long) strtol(str, ptr, base);