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.4 1995/04/27 00:33:49 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 (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
468 syslog(LOG_WARNING, "tcsetattr: %m");
474 * setdtr - control the DTR line on the serial port.
475 * This is called from die(), so it shouldn't call die().
480 int modembits = TIOCM_DTR;
482 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
487 * output - Output PPP packet.
498 MAINDEBUG((LOG_WARNING, "output: unit != 0!"));
500 log_packet(p, len, "sent ");
503 str.buf = (caddr_t) p;
504 if (putmsg(fd, NULL, &str, 0) < 0) {
505 if (errno != ENXIO) {
506 syslog(LOG_ERR, "putmsg: %m");
513 * wait_input - wait for input, for a length of time specified in *timo.
516 struct timeval *timo;
521 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
523 pfd.events = POLLIN | POLLPRI | POLLHUP;
524 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
525 syslog(LOG_ERR, "poll: %m");
531 * read_packet - get a PPP packet from the serial device.
537 struct strbuf str, ctl;
539 unsigned char ctlbuf[16];
541 str.maxlen = PPP_MTU + PPP_HDRLEN;
542 str.buf = (caddr_t) buf;
543 ctl.maxlen = sizeof(ctlbuf);
544 ctl.buf = (caddr_t) ctlbuf;
546 len = getmsg(fd, &ctl, &str, &i);
548 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
551 syslog(LOG_ERR, "getmsg(fd) %m");
555 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
557 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
558 ctlbuf[0], ctlbuf[1]);
561 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
570 * ppp_send_config - configure the transmit characteristics of
574 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
582 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
584 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
585 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
589 if(ioctl(fd, SIOCSIFASYNCMAP, asyncmap) < 0) {
590 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
595 if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
596 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
601 if(ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
602 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
609 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
612 ppp_set_xaccm(unit, accm)
616 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
617 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
622 * ppp_recv_config - configure the receive-side characteristics of
626 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
633 if (ioctl(fd, SIOCSIFMRU, mru) < 0) {
634 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
637 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
638 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
641 c = 2 + (pcomp? 1: 0);
642 if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
643 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
646 c = 2 + (accomp? 1: 0);
647 if (ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
648 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
653 * ccp_test - ask kernel whether a given compression method
654 * is acceptable for use.
656 ccp_test(unit, opt_ptr, opt_len, for_transmit)
657 int unit, opt_len, for_transmit;
660 struct ppp_option_data data;
662 if ((unsigned) opt_len > MAX_PPP_OPTION)
663 opt_len = MAX_PPP_OPTION;
664 data.length = opt_len;
665 data.transmit = for_transmit;
666 BCOPY(opt_ptr, data.opt_data, opt_len);
667 return ioctl(fd, SIOCSCOMPRESS, &data) >= 0;
671 * ccp_flags_set - inform kernel about the current state of CCP.
674 ccp_flags_set(unit, isopen, isup)
675 int unit, isopen, isup;
679 x = (isopen? 1: 0) + (isup? 2: 0);
680 if (ioctl(fd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
681 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
685 * ccp_fatal_error - returns 1 if decompression was disabled as a
686 * result of an error detected after decompression of a packet,
687 * 0 otherwise. This is necessary because of patent nonsense.
690 ccp_fatal_error(unit)
695 if (ioctl(fd, SIOCGIFCOMP, &x) < 0) {
696 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
699 return x & CCP_FATALERROR;
703 * sifvjcomp - config tcp header compression
706 sifvjcomp(u, vjcomp, cidcomp, maxcid)
707 int u, vjcomp, cidcomp, maxcid;
711 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
712 if (ioctl(fd, SIOCSIFVJCOMP, x) < 0) {
713 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
720 * sifup - Config the interface up and enable IP packets to pass.
729 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
730 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
731 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
734 ifr.ifr_flags |= IFF_UP;
735 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
736 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
739 npi.protocol = PPP_IP;
740 npi.mode = NPMODE_PASS;
741 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
742 if (errno != ENOTTY) {
743 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
752 * sifdown - Config the interface down.
763 npi.protocol = PPP_IP;
764 npi.mode = NPMODE_ERROR;
765 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
766 if (errno != ENOTTY) {
767 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
772 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
773 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
774 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
777 ifr.ifr_flags &= ~IFF_UP;
778 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
779 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
787 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
789 #define SET_SA_FAMILY(addr, family) \
790 BZERO((char *) &(addr), sizeof(addr)); \
791 addr.sa_family = (family);
794 * sifaddr - Config the interface IP addresses and netmask.
805 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
806 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
808 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
809 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
810 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
811 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
816 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
817 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
818 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
822 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
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");
828 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
829 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
830 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
837 * cifaddr - Clear the interface IP addresses, and delete routes
838 * through the interface if possible.
847 SET_SA_FAMILY(rt.rt_dst, AF_INET);
848 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
849 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
850 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
851 rt.rt_flags = RTF_HOST;
852 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
853 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
860 * sifdefaultroute - assign a default route through the address given.
863 sifdefaultroute(u, g)
869 SET_SA_FAMILY(rt.rt_dst, AF_INET);
870 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
871 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
872 rt.rt_flags = RTF_GATEWAY;
873 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
874 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
881 * cifdefaultroute - delete a default route through the address given.
884 cifdefaultroute(u, g)
890 SET_SA_FAMILY(rt.rt_dst, AF_INET);
891 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
892 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
893 rt.rt_flags = RTF_GATEWAY;
894 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
895 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
902 * sifproxyarp - Make a proxy ARP entry for the peer.
905 sifproxyarp(unit, hisaddr)
909 struct arpreq arpreq;
911 BZERO(&arpreq, sizeof(arpreq));
914 * Get the hardware address of an interface on the same subnet
915 * as our local address.
917 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
918 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
922 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
923 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
924 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
925 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
926 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
934 * cifproxyarp - Delete the proxy ARP entry for the peer.
937 cifproxyarp(unit, hisaddr)
941 struct arpreq arpreq;
943 BZERO(&arpreq, sizeof(arpreq));
944 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
945 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
946 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
947 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
954 * get_ether_addr - get the hardware address of an interface on the
955 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
956 * in the cslip-2.6 distribution, which is subject to the following
957 * copyright notice (which also applies to logwtmp below):
959 * Copyright (c) 1990, 1992 The Regents of the University of California.
960 * All rights reserved.
962 * Redistribution and use in source and binary forms, with or without
963 * modification, are permitted provided that: (1) source code distributions
964 * retain the above copyright notice and this paragraph in its entirety, (2)
965 * distributions including binary code include the above copyright notice and
966 * this paragraph in its entirety in the documentation or other materials
967 * provided with the distribution, and (3) all advertising materials mentioning
968 * features or use of this software display the following acknowledgement:
969 * ``This product includes software developed by the University of California,
970 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
971 * the University nor the names of its contributors may be used to endorse
972 * or promote products derived from this software without specific prior
973 * written permission.
974 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
975 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
976 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
980 #include <arpa/inet.h>
981 #include <netinet/in_var.h>
982 #include <net/if_dl.h>
984 /* Cast a struct sockaddr to a structaddr_in */
985 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
987 /* Determine if "bits" is set in "flag" */
988 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
990 static struct nlist nl[] = {
996 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
998 if (lseek(fd, offset, SEEK_SET) != offset) {
999 syslog(LOG_ERR,"lseek in kmem: %m");
1002 return(read(fd, buf, nbytes));
1006 get_ether_addr(ipaddr, hwaddr)
1008 struct sockaddr *hwaddr;
1011 register struct ifnet *ifp;
1012 register struct arpcom *ac;
1013 struct arpcom arpcom;
1014 struct in_addr *inp;
1015 register struct ifaddr *ifa;
1016 register struct in_ifaddr *in;
1019 struct in_ifaddr in;
1021 struct ifaddr *ifap;
1022 struct sockaddr ifaddrsaddr;
1023 struct sockaddr_in ifmasksaddr;
1024 struct sockaddr_in *ifinetptr;
1025 struct sockaddr_dl *iflinkptr;
1026 u_long addr, mask, ifip;
1029 /* Open kernel memory for reading */
1030 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1031 syslog(LOG_ERR, "/dev/kmem: %m");
1035 /* Fetch namelist */
1036 if (nlist("/unix", nl) != 0) {
1037 syslog(LOG_ERR, "nlist(): %m");
1042 ifp = &arpcom.ac_if;
1046 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1048 syslog(LOG_ERR, "error reading ifnet addr");
1051 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1052 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1053 syslog(LOG_ERR, "error reading ifnet");
1057 /* Only look at configured, broadcast interfaces */
1058 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1060 /* This probably can't happen... */
1061 if (ifp->if_addrlist == 0)
1064 /* Get interface ip address */
1065 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1066 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1067 sizeof(ifaddr)) != sizeof(ifaddr)) {
1068 syslog(LOG_ERR, "error reading ifaddr");
1071 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1072 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1073 syslog(LOG_ERR, "error reading ifaddrsaddr");
1076 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1077 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1078 syslog(LOG_ERR, "error reading ifmasksaddr");
1081 /* Check if this interface on the right subnet */
1082 switch (ifaddrsaddr.sa_family) {
1084 hwaddr->sa_family = AF_UNSPEC;
1085 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1086 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1089 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1090 ifip = ifinetptr->sin_addr.s_addr;
1091 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1092 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1093 syslog(LOG_ERR, "error reading ifmasksaddr");
1096 mask = ifmasksaddr.sin_addr.s_addr;
1097 if ((ipaddr & mask) == (ifip & mask))
1110 * Return user specified netmask, modified by any mask we might determine
1111 * for address `addr' (in network byte order).
1112 * Here we scan through the system's list of interfaces, looking for
1113 * any non-point-to-point interfaces which might appear to be on the same
1114 * network as `addr'. If we find any, we OR in their netmask to the
1115 * user-specified netmask.
1121 u_int32_t mask, nmask, ina;
1122 struct ifreq *ifr, *ifend, ifreq;
1124 struct ifreq ifs[MAX_IFS];
1127 if (IN_CLASSA(addr)) /* determine network mask for address class */
1128 nmask = IN_CLASSA_NET;
1129 else if (IN_CLASSB(addr))
1130 nmask = IN_CLASSB_NET;
1132 nmask = IN_CLASSC_NET;
1133 /* class D nets are disallowed by bad_ip_adrs */
1134 mask = netmask | htonl(nmask);
1137 * Scan through the system's network interfaces.
1139 ifc.ifc_len = sizeof(ifs);
1141 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1142 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1145 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1146 for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1147 ((char *)&ifr->ifr_addr + ifr->ifr_addr.sa_len)) {
1149 * Check the interface's internet address.
1151 if (ifr->ifr_addr.sa_family != AF_INET)
1153 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1154 if ((ntohl(ina) & nmask) != (addr & nmask))
1157 * Check that the interface is up, and not point-to-point or loopback.
1159 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1160 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1162 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1166 * Get its netmask and OR it into our mask.
1168 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1170 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1176 #define WTMPFILE "/var/adm/wtmp"
1179 logwtmp(line, name, host)
1180 char *line, *name, *host;
1186 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1188 if (!fstat(fd, &buf)) {
1189 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1190 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1191 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1192 (void)time(&ut.ut_time);
1193 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1194 (void)ftruncate(fd, buf.st_size);
1200 * Code for locking/unlocking the serial device.
1203 static char *devlocked = (char *) 0;
1205 int lock(char *device)
1210 if (devname = strrchr(device,'/'))
1215 if ((rc = ttylock(devname)) == 0) {
1216 devlocked = (char *) malloc(strlen(devname) + 1);
1217 sprintf(devlocked,"%s",devname);
1219 devlocked = (char *) 0;
1229 rc = ttyunlock(devlocked);
1231 devlocked = (char *) 0;