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.
6 * Copyright (c) 1995 The Australian National University.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University and The Australian National University.
15 * The names of the Universities may not be used to endorse or promote
16 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 static char rcsid[] = "$Id: sys-aix4.c,v 1.17 1999/03/16 22:53:46 paulus Exp $";
37 #include <sys/termiox.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <sys/stream.h>
47 #include <sys/stropts.h>
50 #include <net/ppp_defs.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 */
71 static int initfdflags = -1; /* Initial file descriptor flags for fd */
73 static int sockfd; /* socket for doing interface ioctls */
75 static int if_is_up; /* Interface has been marked up */
76 static u_int32_t ifaddrs[2]; /* local and remote addresses */
77 static u_int32_t default_route_gateway; /* Gateway for default route added */
78 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
80 #define MAX_POLLFDS 32
81 static struct pollfd pollfds[MAX_POLLFDS];
84 /* Prototypes for procedures local to this file. */
85 static int translate_speed __P((int));
86 static int baud_rate_of __P((int));
87 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
91 * sys_init - System-dependent initialization.
96 /* Get an internet socket for doing socket ioctl's on. */
97 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
98 fatal("Couldn't create IP socket: %m");
104 * sys_cleanup - restore any system state we modified before exiting:
105 * mark the interface down, delete default route and/or proxy arp entry.
106 * This should call die() because it's called from die().
114 strlcpy(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
115 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
116 && ((ifr.ifr_flags & IFF_UP) != 0)) {
117 ifr.ifr_flags &= ~IFF_UP;
118 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
122 cifaddr(0, ifaddrs[0], ifaddrs[1]);
123 if (default_route_gateway)
124 cifdefaultroute(0, 0, default_route_gateway);
126 cifproxyarp(0, proxy_arp_addr);
130 * have_route_to - determine if the system has any route to
131 * a given IP address.
132 * For demand mode to work properly, we have to ignore routes
133 * through our own interface.
135 int have_route_to(u_int32_t addr)
142 * daemon - Detach us from the terminal session.
145 daemon(nochdir, noclose)
146 int nochdir, noclose;
150 if ((pid = fork()) < 0)
153 exit(0); /* parent dies */
158 fclose(stdin); /* don't need stdin, stdout, stderr */
167 * ppp_available - check if this kernel supports PPP.
174 fd = open("/dev/tty", O_RDONLY, 0);
176 return 1; /* can't find out - assume we have ppp */
177 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
184 * establish_ppp - Turn the serial port into a ppp interface.
190 /* go through and save the name of all the modules, then pop em */
192 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
193 ioctl(fd, I_POP, 0) < 0)
195 SYSDEBUG(("popped stream module : %s",
196 str_modules[str_module_count].modname));
200 /* now push the async/fcs module */
201 if (ioctl(fd, I_PUSH, "pppasync") < 0)
202 fatal("ioctl(I_PUSH, ppp_async): %m");
203 /* push the compress module */
204 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
205 warn("ioctl(I_PUSH, ppp_comp): %m");
207 /* finally, push the ppp_if module that actually handles the */
208 /* network interface */
209 if (ioctl(fd, I_PUSH, "pppif") < 0)
210 fatal("ioctl(I_PUSH, ppp_if): %m");
212 /* read mode, message non-discard mode
213 if (ioctl(fd, I_SRDOPT, RMSGN) < 0)
214 fatal("ioctl(I_SRDOPT, RMSGN): %m");
217 * Find out which interface we were given.
218 * (ppp_if handles this ioctl)
220 if (ioctl(fd, SIOCGETU, &ifunit) < 0)
221 fatal("ioctl(SIOCGETU): %m");
223 /* Set debug flags in driver */
224 if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
225 error("ioctl(SIOCSIFDEBUG): %m");
228 /* close stdin, stdout, stderr if they might refer to the device */
229 if (default_device && !closed_stdio) {
232 for (i = 0; i <= 2; ++i)
233 if (i != fd && i != sockfd)
239 * Set device for non-blocking reads.
241 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
242 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
243 warn("Couldn't set device to non-blocking mode: %m");
250 * disestablish_ppp - Restore the serial port to normal operation.
251 * It attempts to reconstruct the stream with the previously popped
252 * modules. This shouldn't call die() because it's called from die().
261 /* Reset non-blocking mode on the file descriptor. */
262 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
263 warn("Couldn't restore device fd flags: %m");
267 /* we can't push or pop modules after the stream has hung up */
268 str_module_count = 0;
269 restore_term = 0; /* nor can we fix up terminal settings */
275 * Check whether the link seems not to be 8-bit clean.
277 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
279 switch (~flags & PAI_FLAGS_HIBITS) {
281 s = "bit 7 set to 1";
284 s = "bit 7 set to 0";
286 case PAI_FLAGS_PAR_EVEN:
289 case PAI_FLAGS_PAR_ODD:
294 warn("Serial link is not 8-bit clean:");
295 warn("All received characters had %s", s);
300 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
304 for (; str_module_count > 0; str_module_count--) {
305 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
307 warn("str_restore: couldn't push module %s: %m",
308 str_modules[str_module_count-1].modname);
310 SYSDEBUG(("str_restore: pushed module %s",
311 str_modules[str_module_count-1].modname));
318 * List of valid speeds.
321 int speed_int, speed_val;
393 * Translate from bits/second to a speed_t.
399 struct speed *speedp;
403 for (speedp = speeds; speedp->speed_int; speedp++)
404 if (bps == speedp->speed_int)
405 return speedp->speed_val;
406 warn("speed %d not supported", bps);
411 * Translate from a speed_t to bits/second.
417 struct speed *speedp;
421 for (speedp = speeds; speedp->speed_int; speedp++)
422 if (speed == speedp->speed_val)
423 return speedp->speed_int;
428 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
429 * at the requested speed, etc. If `local' is true, set CLOCAL
430 * regardless of whether the modem option was specified.
433 set_up_tty(fd, local)
440 if (tcgetattr(fd, &tios) < 0)
441 fatal("tcgetattr: %m");
447 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
449 bzero(&tiox, sizeof(tiox));
450 ioctl(fd, TCGETX, &tiox);
451 tiox.x_hflag = RTSXOFF;
452 ioctl(fd, TCSETX, &tiox);
453 tios.c_cflag &= ~(IXON | IXOFF);
454 } else if (crtscts < 0) {
455 bzero(&tiox, sizeof(tiox));
456 ioctl(fd, TCGETX, &tiox);
457 tiox.x_hflag &= ~RTSXOFF;
458 ioctl(fd, TCSETX, &tiox);
462 tios.c_cflag |= CS8 | CREAD | HUPCL;
464 tios.c_cflag |= CLOCAL;
465 tios.c_iflag = IGNBRK | IGNPAR;
469 tios.c_cc[VTIME] = 0;
472 tios.c_iflag |= IXON | IXOFF;
473 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
474 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
477 speed = translate_speed(inspeed);
479 cfsetospeed(&tios, speed);
480 cfsetispeed(&tios, speed);
482 speed = cfgetospeed(&tios);
484 * We can't proceed if the serial port speed is B0,
485 * since that implies that the serial port is disabled.
488 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
491 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
492 fatal("tcsetattr: %m");
494 baud_rate = inspeed = baud_rate_of(speed);
499 * restore_tty - restore the terminal to the saved settings.
506 if (!default_device) {
508 * Turn off echoing, because otherwise we can get into
509 * a loop with the tty and the modem echoing to each other.
510 * We presume we are the sole user of this tty device, so
511 * when we close it, it will revert to its defaults anyway.
513 inittermios.c_lflag &= ~(ECHO | ECHONL);
515 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
517 warn("tcsetattr: %m");
523 * setdtr - control the DTR line on the serial port.
524 * This is called from die(), so it shouldn't call die().
530 int modembits = TIOCM_DTR;
532 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
537 * output - Output PPP packet.
550 dbglog("sent %P", p, len);
553 str.buf = (caddr_t) p;
555 while (putmsg(ttyfd, NULL, &str, 0) < 0) {
556 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
558 error("Couldn't send packet: %m");
562 pfd.events = POLLOUT;
563 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
568 * wait_input - wait for input, for a length of time specified in *timo.
572 struct timeval *timo;
576 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
577 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
582 * add_fd - add an fd to the set that wait_input waits for.
588 for (n = 0; n < n_pollfds; ++n)
589 if (pollfds[n].fd == fd)
591 if (n_pollfds < MAX_POLLFDS) {
592 pollfds[n_pollfds].fd = fd;
593 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
596 error("Too many inputs!");
600 * remove_fd - remove an fd from the set that wait_input waits for.
602 void remove_fd(int fd)
606 for (n = 0; n < n_pollfds; ++n) {
607 if (pollfds[n].fd == fd) {
608 while (++n < n_pollfds)
609 pollfds[n-1] = pollfds[n];
618 * read_packet - get a PPP packet from the serial device.
624 struct strbuf str, ctl;
626 unsigned char ctlbuf[16];
628 str.maxlen = PPP_MTU + PPP_HDRLEN;
629 str.buf = (caddr_t) buf;
630 ctl.maxlen = sizeof(ctlbuf);
631 ctl.buf = (caddr_t) ctlbuf;
633 len = getmsg(ttyfd, &ctl, &str, &i);
635 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
641 SYSDEBUG(("getmsg returned 0x%x", len));
643 notice("got ctrl msg len %d %x %x\n", ctl.len, ctlbuf[0], ctlbuf[1]);
646 SYSDEBUG(("getmsg short return length %d", str.len));
655 * ppp_send_config - configure the transmit characteristics of
659 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
667 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
669 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
670 fatal("ioctl(SIOCSIFMTU): %m");
672 if(ioctl(ttyfd, SIOCSIFASYNCMAP, asyncmap) < 0)
673 fatal("ioctl(SIOCSIFASYNCMAP): %m");
676 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0)
677 fatal("ioctl(SIOCSIFCOMPPROT): %m");
680 if(ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0)
681 fatal("ioctl(SIOCSIFCOMPAC): %m");
686 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
689 ppp_set_xaccm(unit, accm)
693 if (ioctl(ttyfd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
694 warn("ioctl(set extended ACCM): %m");
699 * ppp_recv_config - configure the receive-side characteristics of
703 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
710 if (ioctl(ttyfd, SIOCSIFMRU, mru) < 0) {
711 error("ioctl(SIOCSIFMRU): %m");
714 if (ioctl(ttyfd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
715 error("ioctl(SIOCSIFRASYNCMAP): %m");
718 c = 2 + (pcomp? 1: 0);
719 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0) {
720 error("ioctl(SIOCSIFCOMPPROT): %m");
723 c = 2 + (accomp? 1: 0);
724 if (ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0) {
725 error("ioctl(SIOCSIFCOMPAC): %m");
730 * ccp_test - ask kernel whether a given compression method
731 * is acceptable for use.
734 ccp_test(unit, opt_ptr, opt_len, for_transmit)
735 int unit, opt_len, for_transmit;
738 struct ppp_option_data data;
740 if ((unsigned) opt_len > MAX_PPP_OPTION)
741 opt_len = MAX_PPP_OPTION;
742 data.length = opt_len;
743 data.transmit = for_transmit;
744 BCOPY(opt_ptr, data.opt_data, opt_len);
745 if (ioctl(ttyfd, SIOCSCOMPRESS, &data) >= 0)
747 return (errno == ENOSR)? 0: -1;
751 * ccp_flags_set - inform kernel about the current state of CCP.
754 ccp_flags_set(unit, isopen, isup)
755 int unit, isopen, isup;
759 x = (isopen? 1: 0) + (isup? 2: 0);
760 if (ioctl(ttyfd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
761 error("ioctl (SIOCSIFCOMP): %m");
765 * ccp_fatal_error - returns 1 if decompression was disabled as a
766 * result of an error detected after decompression of a packet,
767 * 0 otherwise. This is necessary because of patent nonsense.
770 ccp_fatal_error(unit)
775 if (ioctl(ttyfd, SIOCGIFCOMP, &x) < 0) {
776 error("ioctl(SIOCGIFCOMP): %m");
779 return x & CCP_FATALERROR;
783 * sifvjcomp - config tcp header compression
786 sifvjcomp(u, vjcomp, cidcomp, maxcid)
787 int u, vjcomp, cidcomp, maxcid;
791 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
792 if (ioctl(ttyfd, SIOCSIFVJCOMP, x) < 0) {
793 error("ioctl(SIOCSIFVJCOMP): %m");
800 * sifup - Config the interface up and enable IP packets to pass.
808 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
809 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
810 error("ioctl (SIOCGIFFLAGS): %m");
813 ifr.ifr_flags |= IFF_UP;
814 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
815 error("ioctl(SIOCSIFFLAGS): %m");
824 * sifnpmode - Set the mode for handling packets for a given NP.
827 sifnpmode(u, proto, mode)
834 npi.protocol = proto;
836 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
837 error("ioctl(set NP %d mode to %d): %m", proto, mode);
844 * sifdown - Config the interface down.
855 npi.protocol = PPP_IP;
856 npi.mode = NPMODE_ERROR;
857 ioctl(ttyfd, SIOCSETNPMODE, &npi);
858 /* ignore errors, because ttyfd might have been closed by now. */
860 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
861 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
862 error("ioctl (SIOCGIFFLAGS): %m");
865 ifr.ifr_flags &= ~IFF_UP;
866 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
867 error("ioctl(SIOCSIFFLAGS): %m");
876 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
878 #define SET_SA_FAMILY(addr, family) \
879 BZERO((char *) &(addr), sizeof(addr)); \
880 addr.sa_family = (family);
883 * sifaddr - Config the interface IP addresses and netmask.
894 strlcpy(ifr.ifr_name, sizeof (ifr.ifr_name), ifname);
895 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
897 info("Setting interface mask to %s\n", ip_ntoa(m));
898 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
899 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
900 error("ioctl(SIOCSIFNETMASK): %m");
905 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
906 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
907 error("ioctl(SIOCSIFADDR): %m");
911 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
912 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
913 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
914 error("ioctl(SIOCSIFDSTADDR): %m");
918 /* XXX is this necessary? */
919 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
920 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
921 error("ioctl(SIOCSIFADDR): %m");
931 * cifaddr - Clear the interface IP addresses, and delete routes
932 * through the interface if possible.
942 BZERO(&rt, sizeof(rt));
943 SET_SA_FAMILY(rt.rt_dst, AF_INET);
944 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
945 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
946 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
947 rt.rt_flags = RTF_HOST;
948 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
949 error("ioctl(SIOCDELRT): %m");
956 * sifdefaultroute - assign a default route through the address given.
959 sifdefaultroute(u, l, g)
965 BZERO(&rt, sizeof(rt));
966 SET_SA_FAMILY(rt.rt_dst, AF_INET);
967 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
968 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
969 rt.rt_flags = RTF_GATEWAY;
970 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
971 error("default route ioctl(SIOCADDRT): %m");
974 default_route_gateway = g;
979 * cifdefaultroute - delete a default route through the address given.
982 cifdefaultroute(u, l, g)
988 BZERO(&rt, sizeof(rt));
989 SET_SA_FAMILY(rt.rt_dst, AF_INET);
990 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
991 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
992 rt.rt_flags = RTF_GATEWAY;
993 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
994 error("default route ioctl(SIOCDELRT): %m");
997 default_route_gateway = 0;
1002 * sifproxyarp - Make a proxy ARP entry for the peer.
1005 sifproxyarp(unit, hisaddr)
1009 struct arpreq arpreq;
1011 BZERO(&arpreq, sizeof(arpreq));
1014 * Get the hardware address of an interface on the same subnet
1015 * as our local address.
1017 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1018 warn("Cannot determine ethernet address for proxy ARP");
1022 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1023 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1024 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1025 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1026 error("ioctl(SIOCSARP): %m");
1030 proxy_arp_addr = hisaddr;
1035 * cifproxyarp - Delete the proxy ARP entry for the peer.
1038 cifproxyarp(unit, hisaddr)
1042 struct arpreq arpreq;
1044 BZERO(&arpreq, sizeof(arpreq));
1045 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1046 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1047 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1048 error("ioctl(SIOCDARP): %m");
1056 * get_ether_addr - get the hardware address of an interface on the
1057 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
1058 * in the cslip-2.6 distribution, which is subject to the following
1059 * copyright notice (which also applies to logwtmp below):
1061 * Copyright (c) 1990, 1992 The Regents of the University of California.
1062 * All rights reserved.
1064 * Redistribution and use in source and binary forms, with or without
1065 * modification, are permitted provided that: (1) source code distributions
1066 * retain the above copyright notice and this paragraph in its entirety, (2)
1067 * distributions including binary code include the above copyright notice and
1068 * this paragraph in its entirety in the documentation or other materials
1069 * provided with the distribution, and (3) all advertising materials mentioning
1070 * features or use of this software display the following acknowledgement:
1071 * ``This product includes software developed by the University of California,
1072 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1073 * the University nor the names of its contributors may be used to endorse
1074 * or promote products derived from this software without specific prior
1075 * written permission.
1076 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1077 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1078 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1082 #include <arpa/inet.h>
1083 #include <netinet/in_var.h>
1084 #include <net/if_dl.h>
1086 /* Cast a struct sockaddr to a structaddr_in */
1087 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
1089 /* Determine if "bits" is set in "flag" */
1090 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
1092 static struct nlist nl[] = {
1098 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
1100 if (lseek(fd, offset, SEEK_SET) != offset) {
1101 error("lseek in kmem: %m");
1104 return(read(fd, buf, nbytes));
1108 get_ether_addr(ipaddr, hwaddr)
1110 struct sockaddr *hwaddr;
1113 register struct ifnet *ifp;
1114 register struct arpcom *ac;
1115 struct arpcom arpcom;
1116 struct in_addr *inp;
1117 register struct ifaddr *ifa;
1118 register struct in_ifaddr *in;
1121 struct in_ifaddr in;
1123 struct ifaddr *ifap;
1124 struct sockaddr ifaddrsaddr;
1125 struct sockaddr_in ifmasksaddr;
1126 struct sockaddr_in *ifinetptr;
1127 struct sockaddr_dl *iflinkptr;
1128 u_long addr, mask, ifip;
1131 /* Open kernel memory for reading */
1132 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1133 error("/dev/kmem: %m");
1137 /* Fetch namelist */
1138 if (nlist("/unix", nl) != 0) {
1139 error("nlist(): %m");
1144 ifp = &arpcom.ac_if;
1148 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1150 error("error reading ifnet addr");
1153 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1154 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1155 error("error reading ifnet");
1159 /* Only look at configured, broadcast interfaces */
1160 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1162 /* This probably can't happen... */
1163 if (ifp->if_addrlist == 0)
1166 /* Get interface ip address */
1167 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1168 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1169 sizeof(ifaddr)) != sizeof(ifaddr)) {
1170 error("error reading ifaddr");
1173 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1174 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1175 error("error reading ifaddrsaddr");
1178 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1179 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1180 error("error reading ifmasksaddr");
1183 /* Check if this interface on the right subnet */
1184 switch (ifaddrsaddr.sa_family) {
1186 hwaddr->sa_family = AF_UNSPEC;
1187 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1188 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1191 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1192 ifip = ifinetptr->sin_addr.s_addr;
1193 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1194 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1195 error("error reading ifmasksaddr");
1198 mask = ifmasksaddr.sin_addr.s_addr;
1199 if ((ipaddr & mask) == (ifip & mask))
1212 * Return user specified netmask, modified by any mask we might determine
1213 * for address `addr' (in network byte order).
1214 * Here we scan through the system's list of interfaces, looking for
1215 * any non-point-to-point interfaces which might appear to be on the same
1216 * network as `addr'. If we find any, we OR in their netmask to the
1217 * user-specified netmask.
1225 u_int32_t mask, nmask, ina;
1226 struct ifreq *ifr, *ifend, ifreq;
1228 struct ifreq ifs[MAX_IFS];
1231 if (IN_CLASSA(addr)) /* determine network mask for address class */
1232 nmask = IN_CLASSA_NET;
1233 else if (IN_CLASSB(addr))
1234 nmask = IN_CLASSB_NET;
1236 nmask = IN_CLASSC_NET;
1237 /* class D nets are disallowed by bad_ip_adrs */
1238 mask = netmask | htonl(nmask);
1241 * Scan through the system's network interfaces.
1243 ifc.ifc_len = sizeof(ifs);
1245 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1246 warn("ioctl(SIOCGIFCONF): %m");
1249 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1250 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1252 * Check the interface's internet address.
1254 if (ifr->ifr_addr.sa_family != AF_INET)
1256 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1257 if ((ntohl(ina) & nmask) != (addr & nmask))
1260 * Check that the interface is up, and not point-to-point or loopback.
1262 strlcpy(ifreq.ifr_name, sizeof(ifreq.ifr_name), ifr->ifr_name);
1263 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1265 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1269 * Get its netmask and OR it into our mask.
1271 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1273 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1279 #define WTMPFILE "/var/adm/wtmp"
1282 logwtmp(line, name, host)
1283 const char *line, *name, *host;
1289 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1291 if (!fstat(fd, &buf)) {
1292 strlcpy(ut.ut_line, sizeof(ut.ut_line), line);
1293 strlcpy(ut.ut_name, sizeof(ut.ut_name), name);
1294 strlcpy(ut.ut_host, sizeof(ut.ut_host), host);
1295 (void)time(&ut.ut_time);
1296 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1297 (void)ftruncate(fd, buf.st_size);
1303 * Use the hostid as part of the random number seed.
1312 * Code for locking/unlocking the serial device.
1315 static char *devlocked = (char *) 0;
1317 int lock(char *device)
1322 if (devname = strrchr(device,'/'))
1327 if ((rc = ttylock(devname)) == 0) {
1328 devlocked = strdup(devname);
1330 devlocked = (char *) 0;
1340 rc = ttyunlock(devlocked);
1342 devlocked = (char *) 0;