2 * sys-aix4.c - System-dependent procedures for setting up
3 * PPP interfaces on AIX 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-aix4.c,v 1.6 1995/05/01 00:26:11 paulus Exp $";
36 #include <sys/termiox.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <sys/stream.h>
46 #include <sys/stropts.h>
49 #include <net/ppp_defs.h>
50 #include <net/ppp_str.h>
51 #include <net/route.h>
52 #include <net/if_arp.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 */
72 int sockfd; /* socket for doing interface ioctls */
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");
187 * Find out which interface we were given.
188 * (ppp_if handles this ioctl)
190 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
191 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
195 /* Set debug flags in driver */
196 if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
197 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
200 /* close stdin, stdout, stderr if they might refer to the device */
201 if (default_device && !closed_stdio) {
204 for (i = 0; i <= 2; ++i)
205 if (i != fd && i != sockfd)
212 * disestablish_ppp - Restore the serial port to normal operation.
213 * It attempts to reconstruct the stream with the previously popped
214 * modules. This shouldn't call die() because it's called from die().
223 /* we can't push or pop modules after the stream has hung up */
224 str_module_count = 0;
225 restore_term = 0; /* nor can we fix up terminal settings */
231 * Check whether the link seems not to be 8-bit clean.
233 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
235 switch (~flags & PAI_FLAGS_HIBITS) {
237 s = "bit 7 set to 1";
240 s = "bit 7 set to 0";
242 case PAI_FLAGS_PAR_EVEN:
245 case PAI_FLAGS_PAR_ODD:
250 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
251 syslog(LOG_WARNING, "All received characters had %s", s);
256 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
260 for (; str_module_count > 0; str_module_count--) {
261 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
263 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
264 str_modules[str_module_count-1].modname);
266 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
267 str_modules[str_module_count-1].modname));
274 * List of valid speeds.
277 int speed_int, speed_val;
349 * Translate from bits/second to a speed_t.
355 struct speed *speedp;
359 for (speedp = speeds; speedp->speed_int; speedp++)
360 if (bps == speedp->speed_int)
361 return speedp->speed_val;
362 syslog(LOG_WARNING, "speed %d not supported", bps);
367 * Translate from a speed_t to bits/second.
373 struct speed *speedp;
377 for (speedp = speeds; speedp->speed_int; speedp++)
378 if (speed == speedp->speed_val)
379 return speedp->speed_int;
384 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
385 * at the requested speed, etc. If `local' is true, set CLOCAL
386 * regardless of whether the modem option was specified.
388 set_up_tty(fd, local)
395 if (tcgetattr(fd, &tios) < 0) {
396 syslog(LOG_ERR, "tcgetattr: %m");
403 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
405 bzero(&tiox, sizeof(tiox));
406 ioctl(fd, TCGETX, &tiox);
407 tiox.x_hflag = RTSXOFF;
408 ioctl(fd, TCSETX, &tiox);
409 tios.c_cflag &= ~(IXOFF);
410 } else if (crtscts < 0) {
411 bzero(&tiox, sizeof(tiox));
412 ioctl(fd, TCGETX, &tiox);
413 tiox.x_hflag &= ~RTSXOFF;
414 ioctl(fd, TCSETX, &tiox);
418 tios.c_cflag |= CS8 | CREAD | HUPCL;
420 tios.c_cflag |= CLOCAL;
421 tios.c_iflag = IGNBRK | IGNPAR;
425 tios.c_cc[VTIME] = 0;
428 tios.c_iflag |= IXOFF;
429 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
430 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
433 speed = translate_speed(inspeed);
435 cfsetospeed(&tios, speed);
436 cfsetispeed(&tios, speed);
438 speed = cfgetospeed(&tios);
440 * We can't proceed if the serial port speed is B0,
441 * since that implies that the serial port is disabled.
444 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
450 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
451 syslog(LOG_ERR, "tcsetattr: %m");
455 baud_rate = inspeed = baud_rate_of(speed);
460 * restore_tty - restore the terminal to the saved settings.
466 if (!default_device) {
468 * Turn off echoing, because otherwise we can get into
469 * a loop with the tty and the modem echoing to each other.
470 * We presume we are the sole user of this tty device, so
471 * when we close it, it will revert to its defaults anyway.
473 inittermios.c_lflag &= ~(ECHO | ECHONL);
475 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
477 syslog(LOG_WARNING, "tcsetattr: %m");
483 * setdtr - control the DTR line on the serial port.
484 * This is called from die(), so it shouldn't call die().
489 int modembits = TIOCM_DTR;
491 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
496 * output - Output PPP packet.
507 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
509 log_packet(p, len, "sent ");
512 str.buf = (caddr_t) p;
513 if (putmsg(fd, NULL, &str, 0) < 0) {
514 if (errno != ENXIO) {
515 syslog(LOG_ERR, "putmsg: %m");
522 * wait_input - wait for input, for a length of time specified in *timo.
525 struct timeval *timo;
530 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
532 pfd.events = POLLIN | POLLPRI | POLLHUP;
533 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
534 syslog(LOG_ERR, "poll: %m");
540 * read_packet - get a PPP packet from the serial device.
546 struct strbuf str, ctl;
548 unsigned char ctlbuf[16];
550 str.maxlen = PPP_MTU + PPP_HDRLEN;
551 str.buf = (caddr_t) buf;
552 ctl.maxlen = sizeof(ctlbuf);
553 ctl.buf = (caddr_t) ctlbuf;
555 len = getmsg(fd, &ctl, &str, &i);
557 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
560 syslog(LOG_ERR, "getmsg(fd) %m");
564 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
566 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
567 ctlbuf[0], ctlbuf[1]);
570 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
579 * ppp_send_config - configure the transmit characteristics of
583 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
591 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
593 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
594 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
598 if(ioctl(fd, SIOCSIFASYNCMAP, asyncmap) < 0) {
599 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
604 if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
605 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
610 if(ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
611 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
618 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
621 ppp_set_xaccm(unit, accm)
625 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
626 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
631 * ppp_recv_config - configure the receive-side characteristics of
635 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
642 if (ioctl(fd, SIOCSIFMRU, mru) < 0) {
643 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
646 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
647 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
650 c = 2 + (pcomp? 1: 0);
651 if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
652 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
655 c = 2 + (accomp? 1: 0);
656 if (ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
657 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
662 * ccp_test - ask kernel whether a given compression method
663 * is acceptable for use.
665 ccp_test(unit, opt_ptr, opt_len, for_transmit)
666 int unit, opt_len, for_transmit;
669 struct ppp_option_data data;
671 if ((unsigned) opt_len > MAX_PPP_OPTION)
672 opt_len = MAX_PPP_OPTION;
673 data.length = opt_len;
674 data.transmit = for_transmit;
675 BCOPY(opt_ptr, data.opt_data, opt_len);
676 return ioctl(fd, SIOCSCOMPRESS, &data) >= 0;
680 * ccp_flags_set - inform kernel about the current state of CCP.
683 ccp_flags_set(unit, isopen, isup)
684 int unit, isopen, isup;
688 x = (isopen? 1: 0) + (isup? 2: 0);
689 if (ioctl(fd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
690 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
694 * ccp_fatal_error - returns 1 if decompression was disabled as a
695 * result of an error detected after decompression of a packet,
696 * 0 otherwise. This is necessary because of patent nonsense.
699 ccp_fatal_error(unit)
704 if (ioctl(fd, SIOCGIFCOMP, &x) < 0) {
705 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
708 return x & CCP_FATALERROR;
712 * sifvjcomp - config tcp header compression
715 sifvjcomp(u, vjcomp, cidcomp, maxcid)
716 int u, vjcomp, cidcomp, maxcid;
720 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
721 if (ioctl(fd, SIOCSIFVJCOMP, x) < 0) {
722 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
729 * sifup - Config the interface up and enable IP packets to pass.
738 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
739 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
740 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
743 ifr.ifr_flags |= IFF_UP;
744 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
745 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
748 npi.protocol = PPP_IP;
749 npi.mode = NPMODE_PASS;
750 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
751 if (errno != ENOTTY) {
752 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, &npi) < 0) {
775 if (errno != ENOTTY) {
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 ifr.ifr_flags &= ~IFF_UP;
787 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
788 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
796 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
798 #define SET_SA_FAMILY(addr, family) \
799 BZERO((char *) &(addr), sizeof(addr)); \
800 addr.sa_family = (family);
803 * sifaddr - Config the interface IP addresses and netmask.
814 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
815 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
817 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
818 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
819 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
820 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
825 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
826 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
827 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
831 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
832 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
833 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
834 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
837 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
838 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
839 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
846 * cifaddr - Clear the interface IP addresses, and delete routes
847 * through the interface if possible.
856 SET_SA_FAMILY(rt.rt_dst, AF_INET);
857 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
858 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
859 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
860 rt.rt_flags = RTF_HOST;
861 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
862 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
869 * sifdefaultroute - assign a default route through the address given.
872 sifdefaultroute(u, g)
878 SET_SA_FAMILY(rt.rt_dst, AF_INET);
879 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
880 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
881 rt.rt_flags = RTF_GATEWAY;
882 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
883 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
890 * cifdefaultroute - delete a default route through the address given.
893 cifdefaultroute(u, g)
899 SET_SA_FAMILY(rt.rt_dst, AF_INET);
900 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
901 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
902 rt.rt_flags = RTF_GATEWAY;
903 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
904 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
911 * sifproxyarp - Make a proxy ARP entry for the peer.
914 sifproxyarp(unit, hisaddr)
918 struct arpreq arpreq;
920 BZERO(&arpreq, sizeof(arpreq));
923 * Get the hardware address of an interface on the same subnet
924 * as our local address.
926 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
927 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
931 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
932 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
933 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
934 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
935 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
943 * cifproxyarp - Delete the proxy ARP entry for the peer.
946 cifproxyarp(unit, hisaddr)
950 struct arpreq arpreq;
952 BZERO(&arpreq, sizeof(arpreq));
953 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
954 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
955 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
956 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
963 * get_ether_addr - get the hardware address of an interface on the
964 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
965 * in the cslip-2.6 distribution, which is subject to the following
966 * copyright notice (which also applies to logwtmp below):
968 * Copyright (c) 1990, 1992 The Regents of the University of California.
969 * All rights reserved.
971 * Redistribution and use in source and binary forms, with or without
972 * modification, are permitted provided that: (1) source code distributions
973 * retain the above copyright notice and this paragraph in its entirety, (2)
974 * distributions including binary code include the above copyright notice and
975 * this paragraph in its entirety in the documentation or other materials
976 * provided with the distribution, and (3) all advertising materials mentioning
977 * features or use of this software display the following acknowledgement:
978 * ``This product includes software developed by the University of California,
979 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
980 * the University nor the names of its contributors may be used to endorse
981 * or promote products derived from this software without specific prior
982 * written permission.
983 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
984 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
985 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
989 #include <arpa/inet.h>
990 #include <netinet/in_var.h>
991 #include <net/if_dl.h>
993 /* Cast a struct sockaddr to a structaddr_in */
994 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
996 /* Determine if "bits" is set in "flag" */
997 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
999 static struct nlist nl[] = {
1005 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
1007 if (lseek(fd, offset, SEEK_SET) != offset) {
1008 syslog(LOG_ERR,"lseek in kmem: %m");
1011 return(read(fd, buf, nbytes));
1015 get_ether_addr(ipaddr, hwaddr)
1017 struct sockaddr *hwaddr;
1020 register struct ifnet *ifp;
1021 register struct arpcom *ac;
1022 struct arpcom arpcom;
1023 struct in_addr *inp;
1024 register struct ifaddr *ifa;
1025 register struct in_ifaddr *in;
1028 struct in_ifaddr in;
1030 struct ifaddr *ifap;
1031 struct sockaddr ifaddrsaddr;
1032 struct sockaddr_in ifmasksaddr;
1033 struct sockaddr_in *ifinetptr;
1034 struct sockaddr_dl *iflinkptr;
1035 u_long addr, mask, ifip;
1038 /* Open kernel memory for reading */
1039 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1040 syslog(LOG_ERR, "/dev/kmem: %m");
1044 /* Fetch namelist */
1045 if (nlist("/unix", nl) != 0) {
1046 syslog(LOG_ERR, "nlist(): %m");
1051 ifp = &arpcom.ac_if;
1055 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1057 syslog(LOG_ERR, "error reading ifnet addr");
1060 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1061 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1062 syslog(LOG_ERR, "error reading ifnet");
1066 /* Only look at configured, broadcast interfaces */
1067 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1069 /* This probably can't happen... */
1070 if (ifp->if_addrlist == 0)
1073 /* Get interface ip address */
1074 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1075 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1076 sizeof(ifaddr)) != sizeof(ifaddr)) {
1077 syslog(LOG_ERR, "error reading ifaddr");
1080 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1081 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1082 syslog(LOG_ERR, "error reading ifaddrsaddr");
1085 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1086 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1087 syslog(LOG_ERR, "error reading ifmasksaddr");
1090 /* Check if this interface on the right subnet */
1091 switch (ifaddrsaddr.sa_family) {
1093 hwaddr->sa_family = AF_UNSPEC;
1094 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1095 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1098 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1099 ifip = ifinetptr->sin_addr.s_addr;
1100 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1101 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1102 syslog(LOG_ERR, "error reading ifmasksaddr");
1105 mask = ifmasksaddr.sin_addr.s_addr;
1106 if ((ipaddr & mask) == (ifip & mask))
1119 * Return user specified netmask, modified by any mask we might determine
1120 * for address `addr' (in network byte order).
1121 * Here we scan through the system's list of interfaces, looking for
1122 * any non-point-to-point interfaces which might appear to be on the same
1123 * network as `addr'. If we find any, we OR in their netmask to the
1124 * user-specified netmask.
1132 u_int32_t mask, nmask, ina;
1133 struct ifreq *ifr, *ifend, ifreq;
1135 struct ifreq ifs[MAX_IFS];
1138 if (IN_CLASSA(addr)) /* determine network mask for address class */
1139 nmask = IN_CLASSA_NET;
1140 else if (IN_CLASSB(addr))
1141 nmask = IN_CLASSB_NET;
1143 nmask = IN_CLASSC_NET;
1144 /* class D nets are disallowed by bad_ip_adrs */
1145 mask = netmask | htonl(nmask);
1148 * Scan through the system's network interfaces.
1150 ifc.ifc_len = sizeof(ifs);
1152 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1153 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1156 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1157 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1159 * Check the interface's internet address.
1161 if (ifr->ifr_addr.sa_family != AF_INET)
1163 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1164 if ((ntohl(ina) & nmask) != (addr & nmask))
1167 * Check that the interface is up, and not point-to-point or loopback.
1169 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1170 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1172 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1176 * Get its netmask and OR it into our mask.
1178 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1180 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1186 #define WTMPFILE "/var/adm/wtmp"
1189 logwtmp(line, name, host)
1190 char *line, *name, *host;
1196 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1198 if (!fstat(fd, &buf)) {
1199 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1200 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1201 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1202 (void)time(&ut.ut_time);
1203 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1204 (void)ftruncate(fd, buf.st_size);
1210 * Code for locking/unlocking the serial device.
1213 static char *devlocked = (char *) 0;
1215 int lock(char *device)
1220 if (devname = strrchr(device,'/'))
1225 if ((rc = ttylock(devname)) == 0) {
1226 devlocked = (char *) malloc(strlen(devname) + 1);
1227 sprintf(devlocked,"%s",devname);
1229 devlocked = (char *) 0;
1239 rc = ttyunlock(devlocked);
1241 devlocked = (char *) 0;