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.17 1995/04/24 05:36:14 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 restore_term; /* 1 => we've munged the terminal */
69 static struct termios inittermios; /* Initial TTY termios */
71 int sockfd; /* socket for doing interface ioctls */
72 static char *lock_file; /* lock file created for serial port */
75 * sys_init - System-dependent initialization.
80 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
81 setlogmask(LOG_UPTO(LOG_INFO));
83 setlogmask(LOG_UPTO(LOG_DEBUG));
85 /* Get an internet socket for doing socket ioctl's on. */
86 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
87 syslog(LOG_ERR, "Couldn't create IP socket: %m");
93 * note_debug_level - note a change in the debug level.
99 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
100 setlogmask(LOG_UPTO(LOG_DEBUG));
102 setlogmask(LOG_UPTO(LOG_WARNING));
107 * daemon - Detach us from the terminal session.
110 daemon(nochdir, noclose)
111 int nochdir, noclose;
115 if ((pid = fork()) < 0)
118 exit(0); /* parent dies */
123 fclose(stdin); /* don't need stdin, stdout, stderr */
132 * ppp_available - check if this kernel supports PPP.
139 fd = open("/dev/tty", O_RDONLY, 0);
141 return 1; /* can't find out - assume we have ppp */
142 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
149 * establish_ppp - Turn the serial port into a ppp interface.
154 /* go through and save the name of all the modules, then pop em */
156 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
157 ioctl(fd, I_POP, 0) < 0)
159 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
160 str_modules[str_module_count].modname));
164 /* now push the async/fcs module */
165 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
166 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
169 /* push the compress module */
170 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
171 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
173 /* finally, push the ppp_if module that actually handles the */
174 /* network interface */
175 if (ioctl(fd, I_PUSH, "pppif") < 0) {
176 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
180 /* read mode, message non-discard mode */
181 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
182 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
186 * Find out which interface we were given.
187 * (ppp_if handles this ioctl)
189 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
190 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
194 /* Set debug flags in driver */
195 if (ioctl(fd, SIOCSIFDEBUG, &kdebugflag) < 0) {
196 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
199 /* close stdin, stdout, stderr if they might refer to the device */
200 if (default_device && !closed_stdio) {
203 for (i = 0; i <= 2; ++i)
204 if (i != fd && i != sockfd)
211 * disestablish_ppp - Restore the serial port to normal operation.
212 * It attempts to reconstruct the stream with the previously popped
213 * modules. This shouldn't call die() because it's called from die().
222 /* we can't push or pop modules after the stream has hung up */
223 str_module_count = 0;
224 restore_term = 0; /* nor can we fix up terminal settings */
230 * Check whether the link seems not to be 8-bit clean.
232 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
234 switch (~flags & PAI_FLAGS_HIBITS) {
236 s = "bit 7 set to 1";
239 s = "bit 7 set to 0";
241 case PAI_FLAGS_PAR_EVEN:
244 case PAI_FLAGS_PAR_ODD:
249 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
250 syslog(LOG_WARNING, "All received characters had %s", s);
254 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
257 for (; str_module_count > 0; str_module_count--) {
258 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
260 syslog(LOG_WARNING, "Couldn't restore module %s: %m",
261 str_modules[str_module_count-1].modname);
263 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
264 str_modules[str_module_count-1].modname));
273 * List of valid speeds.
276 int speed_int, speed_val;
348 * Translate from bits/second to a speed_t.
354 struct speed *speedp;
358 for (speedp = speeds; speedp->speed_int; speedp++)
359 if (bps == speedp->speed_int)
360 return speedp->speed_val;
361 syslog(LOG_WARNING, "speed %d not supported", bps);
366 * Translate from a speed_t to bits/second.
372 struct speed *speedp;
376 for (speedp = speeds; speedp->speed_int; speedp++)
377 if (speed == speedp->speed_val)
378 return speedp->speed_int;
383 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
384 * at the requested speed, etc. If `local' is true, set CLOCAL
385 * regardless of whether the modem option was specified.
387 set_up_tty(fd, local)
393 if (tcgetattr(fd, &tios) < 0) {
394 syslog(LOG_ERR, "tcgetattr: %m");
401 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
403 tios.c_cflag |= CRTSCTS;
404 else if (crtscts < 0)
405 tios.c_cflag &= ~CRTSCTS;
407 tios.c_cflag |= CS8 | CREAD | HUPCL;
409 tios.c_cflag |= CLOCAL;
410 tios.c_iflag = IGNBRK | IGNPAR;
414 tios.c_cc[VTIME] = 0;
417 tios.c_iflag |= IXOFF;
418 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
419 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
422 speed = translate_speed(inspeed);
424 cfsetospeed(&tios, speed);
425 cfsetispeed(&tios, speed);
427 speed = cfgetospeed(&tios);
429 * We can't proceed if the serial port speed is B0,
430 * since that implies that the serial port is disabled.
433 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
439 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
440 syslog(LOG_ERR, "tcsetattr: %m");
444 baud_rate = inspeed = baud_rate_of(speed);
449 * restore_tty - restore the terminal to the saved settings.
455 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
457 syslog(LOG_WARNING, "tcsetattr: %m");
463 * setdtr - control the DTR line on the serial port.
464 * This is called from die(), so it shouldn't call die().
469 int modembits = TIOCM_DTR;
471 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
476 * output - Output PPP packet.
487 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
489 log_packet(p, len, "sent ");
492 str.buf = (caddr_t) p;
493 if (putmsg(fd, NULL, &str, 0) < 0) {
494 if (errno != ENXIO) {
495 syslog(LOG_ERR, "putmsg: %m");
502 * wait_input - wait for input, for a length of time specified in *timo.
505 struct timeval *timo;
510 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
512 pfd.events = POLLIN | POLLPRI | POLLHUP;
513 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
514 syslog(LOG_ERR, "poll: %m");
520 * read_packet - get a PPP packet from the serial device.
526 struct strbuf str, ctl;
528 unsigned char ctlbuf[16];
530 str.maxlen = PPP_MTU + PPP_HDRLEN;
531 str.buf = (caddr_t) buf;
532 ctl.maxlen = sizeof(ctlbuf);
533 ctl.buf = (caddr_t) ctlbuf;
535 len = getmsg(fd, &ctl, &str, &i);
537 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
540 syslog(LOG_ERR, "getmsg(fd) %m");
544 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
546 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
547 ctlbuf[0], ctlbuf[1]);
550 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
559 * ppp_send_config - configure the transmit characteristics of
563 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
571 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
573 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
574 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
578 if(ioctl(fd, SIOCSIFASYNCMAP, (caddr_t) &asyncmap) < 0) {
579 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
584 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
585 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
590 if(ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
591 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
598 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
601 ppp_set_xaccm(unit, accm)
605 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
606 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
611 * ppp_recv_config - configure the receive-side characteristics of
615 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
622 if (ioctl(fd, SIOCSIFMRU, &mru) < 0) {
623 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
626 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) &asyncmap) < 0) {
627 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
630 c = 2 + (pcomp? 1: 0);
631 if(ioctl(fd, SIOCSIFCOMPPROT, &c) < 0) {
632 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
635 c = 2 + (accomp? 1: 0);
636 if (ioctl(fd, SIOCSIFCOMPAC, &c) < 0) {
637 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
642 * ccp_test - ask kernel whether a given compression method
643 * is acceptable for use.
645 ccp_test(unit, opt_ptr, opt_len, for_transmit)
646 int unit, opt_len, for_transmit;
649 struct ppp_option_data data;
651 if ((unsigned) opt_len > MAX_PPP_OPTION)
652 opt_len = MAX_PPP_OPTION;
653 data.length = opt_len;
654 data.transmit = for_transmit;
655 BCOPY(opt_ptr, data.opt_data, opt_len);
656 return ioctl(fd, SIOCSCOMPRESS, (caddr_t) &data) >= 0;
660 * ccp_flags_set - inform kernel about the current state of CCP.
663 ccp_flags_set(unit, isopen, isup)
664 int unit, isopen, isup;
668 x = (isopen? 1: 0) + (isup? 2: 0);
669 if (ioctl(fd, SIOCSIFCOMP, (caddr_t) &x) < 0 && errno != ENOTTY)
670 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
674 * ccp_fatal_error - returns 1 if decompression was disabled as a
675 * result of an error detected after decompression of a packet,
676 * 0 otherwise. This is necessary because of patent nonsense.
679 ccp_fatal_error(unit)
684 if (ioctl(fd, SIOCGIFCOMP, (caddr_t) &x) < 0) {
685 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
688 return x & CCP_FATALERROR;
692 * sifvjcomp - config tcp header compression
695 sifvjcomp(u, vjcomp, cidcomp, maxcid)
696 int u, vjcomp, cidcomp, maxcid;
700 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
701 if (ioctl(fd, SIOCSIFVJCOMP, (caddr_t) &x) < 0) {
702 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
709 * sifup - Config the interface up and enable IP packets to pass.
718 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
719 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
720 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
723 ifr.ifr_flags |= IFF_UP;
724 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
725 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
728 npi.protocol = PPP_IP;
729 npi.mode = NPMODE_PASS;
730 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
731 if (errno != ENOTTY) {
732 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
740 * sifdown - Config the interface down.
751 npi.protocol = PPP_IP;
752 npi.mode = NPMODE_ERROR;
753 if (ioctl(fd, SIOCSETNPMODE, (caddr_t) &npi) < 0) {
754 if (errno != ENOTTY) {
755 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
760 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
761 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
762 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
765 ifr.ifr_flags &= ~IFF_UP;
766 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
767 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
775 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
777 #define SET_SA_FAMILY(addr, family) \
778 BZERO((char *) &(addr), sizeof(addr)); \
779 addr.sa_family = (family);
782 * sifaddr - Config the interface IP addresses and netmask.
793 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
794 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
795 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
796 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
797 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
800 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
801 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
802 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
806 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
807 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
808 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
809 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
817 * cifaddr - Clear the interface IP addresses, and delete routes
818 * through the interface if possible.
827 SET_SA_FAMILY(rt.rt_dst, AF_INET);
828 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
829 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
830 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
831 rt.rt_flags = RTF_HOST;
832 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
833 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
840 * sifdefaultroute - assign a default route through the address given.
843 sifdefaultroute(u, g)
849 SET_SA_FAMILY(rt.rt_dst, AF_INET);
850 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
851 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
852 rt.rt_flags = RTF_GATEWAY;
853 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
854 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
861 * cifdefaultroute - delete a default route through the address given.
864 cifdefaultroute(u, g)
870 SET_SA_FAMILY(rt.rt_dst, AF_INET);
871 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
872 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
873 rt.rt_flags = RTF_GATEWAY;
874 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
875 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
882 * sifproxyarp - Make a proxy ARP entry for the peer.
885 sifproxyarp(unit, hisaddr)
889 struct arpreq arpreq;
891 BZERO(&arpreq, sizeof(arpreq));
894 * Get the hardware address of an interface on the same subnet
895 * as our local address.
897 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
898 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
902 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
903 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
904 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
905 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
906 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
914 * cifproxyarp - Delete the proxy ARP entry for the peer.
917 cifproxyarp(unit, hisaddr)
921 struct arpreq arpreq;
923 BZERO(&arpreq, sizeof(arpreq));
924 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
925 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
926 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
927 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
934 * get_ether_addr - get the hardware address of an interface on the
935 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
936 * in the cslip-2.6 distribution, which is subject to the following
937 * copyright notice (which also applies to logwtmp below):
939 * Copyright (c) 1990, 1992 The Regents of the University of California.
940 * All rights reserved.
942 * Redistribution and use in source and binary forms, with or without
943 * modification, are permitted provided that: (1) source code distributions
944 * retain the above copyright notice and this paragraph in its entirety, (2)
945 * distributions including binary code include the above copyright notice and
946 * this paragraph in its entirety in the documentation or other materials
947 * provided with the distribution, and (3) all advertising materials mentioning
948 * features or use of this software display the following acknowledgement:
949 * ``This product includes software developed by the University of California,
950 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
951 * the University nor the names of its contributors may be used to endorse
952 * or promote products derived from this software without specific prior
953 * written permission.
954 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
955 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
956 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
963 /* XXX SunOS 4.1 defines this and 3.5 doesn't... */
969 #include <netinet/in_var.h>
971 #include <netinet/if_ether.h>
973 /* Cast a struct sockaddr to a structaddr_in */
974 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
976 /* Determine if "bits" is set in "flag" */
977 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
979 static struct nlist nl[] = {
988 get_ether_addr(ipaddr, hwaddr)
990 struct sockaddr *hwaddr;
993 register struct ifnet *ifp;
994 register struct arpcom *ac;
995 struct arpcom arpcom;
998 register struct ifaddr *ifa;
999 register struct in_ifaddr *in;
1002 struct in_ifaddr in;
1005 u_int32_t addr, mask;
1007 /* Open kernel memory for reading */
1008 kd = kvm_open(0, 0, 0, O_RDONLY, NULL);
1010 syslog(LOG_ERR, "kvm_open: %m");
1014 /* Fetch namelist */
1015 if (kvm_nlist(kd, nl) != 0) {
1016 syslog(LOG_ERR, "kvm_nlist failed");
1021 ifp = &arpcom.ac_if;
1027 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1029 syslog(LOG_ERR, "error reading ifnet addr");
1032 for ( ; addr; addr = (u_int32_t)ifp->if_next) {
1033 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1034 syslog(LOG_ERR, "error reading ifnet");
1038 /* Only look at configured, broadcast interfaces */
1039 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1042 /* This probably can't happen... */
1043 if (ifp->if_addrlist == 0)
1047 /* Get interface ip address */
1049 if (kvm_read(kd, (u_int32_t)ifp->if_addrlist, (char *)&ifaddr,
1050 sizeof(ifaddr)) != sizeof(ifaddr)) {
1051 syslog(LOG_ERR, "error reading ifaddr");
1054 inp = &SATOSIN(&ifa->ifa_addr)->sin_addr;
1056 inp = &SATOSIN(&ifp->if_addr)->sin_addr;
1059 /* Check if this interface on the right subnet */
1061 mask = in->ia_subnetmask;
1063 mask = ifp->if_subnetmask;
1065 if ((ipaddr & mask) != (inp->s_addr & mask))
1068 /* Copy out the local ethernet address */
1069 hwaddr->sa_family = AF_UNSPEC;
1070 BCOPY((caddr_t) &arpcom.ac_enaddr, hwaddr->sa_data,
1071 sizeof(arpcom.ac_enaddr));
1072 return 1; /* success! */
1075 /* couldn't find one */
1079 #define WTMPFILE "/usr/adm/wtmp"
1082 logwtmp(line, name, host)
1083 char *line, *name, *host;
1089 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1091 if (!fstat(fd, &buf)) {
1092 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1093 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1094 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1095 (void)time(&ut.ut_time);
1096 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1097 (void)ftruncate(fd, buf.st_size);
1103 * Code for locking/unlocking the serial device.
1104 * This code is derived from chat.c.
1107 #if defined(SUNOS) && SUNOS >= 41 && !defined(HDB)
1114 # define LOCK_PREFIX "/usr/spool/locks/LCK.."
1116 # define LOCK_PREFIX "/usr/spool/uucp/LCK.."
1118 #endif /* LOCK_DIR */
1121 * lock - create a lock file for the named device.
1127 char hdb_lock_buffer[12];
1131 if ((p = strrchr(dev, '/')) != NULL)
1133 lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1134 if (lock_file == NULL)
1135 novm("lock file name");
1136 strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1138 while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1140 && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1141 /* Read the lock file to find out who has the device locked */
1143 n = read(fd, hdb_lock_buffer, 11);
1145 hdb_lock_buffer[n] = 0;
1146 pid = atoi(hdb_lock_buffer);
1149 n = read(fd, &pid, sizeof(pid));
1152 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1155 if (kill(pid, 0) == -1 && errno == ESRCH) {
1156 /* pid no longer exists - remove the lock file */
1157 if (unlink(lock_file) == 0) {
1159 syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1163 syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1166 syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1171 syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1178 sprintf(hdb_lock_buffer, "%10d\n", getpid());
1179 write(fd, hdb_lock_buffer, 11);
1182 write(fd, &pid, sizeof pid);
1190 * unlock - remove our lockfile