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.13 1998/03/25 02:19:23 paulus Exp $";
38 #include <sys/termiox.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
47 #include <sys/stream.h>
48 #include <sys/stropts.h>
51 #include <net/ppp_defs.h>
52 #include <net/route.h>
53 #include <net/if_arp.h>
54 #include <netinet/in.h>
59 #define ifr_mtu ifr_metric
62 #define MAXMODULES 10 /* max number of module names to save */
63 static struct modlist {
64 char modname[FMNAMESZ+1];
65 } str_modules[MAXMODULES];
66 static int str_module_count = 0;
67 static int pushed_ppp;
68 static int closed_stdio;
70 static int restore_term; /* 1 => we've munged the terminal */
71 static struct termios inittermios; /* Initial TTY termios */
72 static int initfdflags = -1; /* Initial file descriptor flags for fd */
74 static int sockfd; /* socket for doing interface ioctls */
76 static int if_is_up; /* Interface has been marked up */
77 static u_int32_t ifaddrs[2]; /* local and remote addresses */
78 static u_int32_t default_route_gateway; /* Gateway for default route added */
79 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
81 /* Prototypes for procedures local to this file. */
82 static int translate_speed __P((int));
83 static int baud_rate_of __P((int));
84 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
88 * sys_init - System-dependent initialization.
93 /* Get an internet socket for doing socket ioctl's on. */
94 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
95 syslog(LOG_ERR, "Couldn't create IP socket: %m");
101 * sys_cleanup - restore any system state we modified before exiting:
102 * mark the interface down, delete default route and/or proxy arp entry.
103 * This should call die() because it's called from die().
111 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
112 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
113 && ((ifr.ifr_flags & IFF_UP) != 0)) {
114 ifr.ifr_flags &= ~IFF_UP;
115 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
119 cifaddr(0, ifaddrs[0], ifaddrs[1]);
120 if (default_route_gateway)
121 cifdefaultroute(0, 0, default_route_gateway);
123 cifproxyarp(0, proxy_arp_addr);
128 * daemon - Detach us from the terminal session.
131 daemon(nochdir, noclose)
132 int nochdir, noclose;
136 if ((pid = fork()) < 0)
139 exit(0); /* parent dies */
144 fclose(stdin); /* don't need stdin, stdout, stderr */
153 * ppp_available - check if this kernel supports PPP.
160 fd = open("/dev/tty", O_RDONLY, 0);
162 return 1; /* can't find out - assume we have ppp */
163 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
170 * establish_ppp - Turn the serial port into a ppp interface.
176 /* go through and save the name of all the modules, then pop em */
178 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
179 ioctl(fd, I_POP, 0) < 0)
181 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
182 str_modules[str_module_count].modname));
186 /* now push the async/fcs module */
187 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
188 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
191 /* push the compress module */
192 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
193 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
195 /* finally, push the ppp_if module that actually handles the */
196 /* network interface */
197 if (ioctl(fd, I_PUSH, "pppif") < 0) {
198 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
202 /* read mode, message non-discard mode
203 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
204 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
209 * Find out which interface we were given.
210 * (ppp_if handles this ioctl)
212 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
213 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
217 /* Set debug flags in driver */
218 if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
219 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
222 /* close stdin, stdout, stderr if they might refer to the device */
223 if (default_device && !closed_stdio) {
226 for (i = 0; i <= 2; ++i)
227 if (i != fd && i != sockfd)
233 * Set device for non-blocking reads.
235 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
236 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
237 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
242 * disestablish_ppp - Restore the serial port to normal operation.
243 * It attempts to reconstruct the stream with the previously popped
244 * modules. This shouldn't call die() because it's called from die().
253 /* Reset non-blocking mode on the file descriptor. */
254 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
255 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
259 /* we can't push or pop modules after the stream has hung up */
260 str_module_count = 0;
261 restore_term = 0; /* nor can we fix up terminal settings */
267 * Check whether the link seems not to be 8-bit clean.
269 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
271 switch (~flags & PAI_FLAGS_HIBITS) {
273 s = "bit 7 set to 1";
276 s = "bit 7 set to 0";
278 case PAI_FLAGS_PAR_EVEN:
281 case PAI_FLAGS_PAR_ODD:
286 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
287 syslog(LOG_WARNING, "All received characters had %s", s);
292 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
296 for (; str_module_count > 0; str_module_count--) {
297 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
299 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
300 str_modules[str_module_count-1].modname);
302 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
303 str_modules[str_module_count-1].modname));
310 * List of valid speeds.
313 int speed_int, speed_val;
385 * Translate from bits/second to a speed_t.
391 struct speed *speedp;
395 for (speedp = speeds; speedp->speed_int; speedp++)
396 if (bps == speedp->speed_int)
397 return speedp->speed_val;
398 syslog(LOG_WARNING, "speed %d not supported", bps);
403 * Translate from a speed_t to bits/second.
409 struct speed *speedp;
413 for (speedp = speeds; speedp->speed_int; speedp++)
414 if (speed == speedp->speed_val)
415 return speedp->speed_int;
420 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
421 * at the requested speed, etc. If `local' is true, set CLOCAL
422 * regardless of whether the modem option was specified.
425 set_up_tty(fd, local)
432 if (tcgetattr(fd, &tios) < 0) {
433 syslog(LOG_ERR, "tcgetattr: %m");
440 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
442 bzero(&tiox, sizeof(tiox));
443 ioctl(fd, TCGETX, &tiox);
444 tiox.x_hflag = RTSXOFF;
445 ioctl(fd, TCSETX, &tiox);
446 tios.c_cflag &= ~(IXON | IXOFF);
447 } else if (crtscts < 0) {
448 bzero(&tiox, sizeof(tiox));
449 ioctl(fd, TCGETX, &tiox);
450 tiox.x_hflag &= ~RTSXOFF;
451 ioctl(fd, TCSETX, &tiox);
455 tios.c_cflag |= CS8 | CREAD | HUPCL;
457 tios.c_cflag |= CLOCAL;
458 tios.c_iflag = IGNBRK | IGNPAR;
462 tios.c_cc[VTIME] = 0;
465 tios.c_iflag |= IXON | IXOFF;
466 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
467 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
470 speed = translate_speed(inspeed);
472 cfsetospeed(&tios, speed);
473 cfsetispeed(&tios, speed);
475 speed = cfgetospeed(&tios);
477 * We can't proceed if the serial port speed is B0,
478 * since that implies that the serial port is disabled.
481 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
487 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
488 syslog(LOG_ERR, "tcsetattr: %m");
492 baud_rate = inspeed = baud_rate_of(speed);
497 * restore_tty - restore the terminal to the saved settings.
504 if (!default_device) {
506 * Turn off echoing, because otherwise we can get into
507 * a loop with the tty and the modem echoing to each other.
508 * We presume we are the sole user of this tty device, so
509 * when we close it, it will revert to its defaults anyway.
511 inittermios.c_lflag &= ~(ECHO | ECHONL);
513 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
515 syslog(LOG_WARNING, "tcsetattr: %m");
521 * setdtr - control the DTR line on the serial port.
522 * This is called from die(), so it shouldn't call die().
528 int modembits = TIOCM_DTR;
530 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
535 * output - Output PPP packet.
548 log_packet(p, len, "sent ", LOG_DEBUG);
551 str.buf = (caddr_t) p;
553 while (putmsg(ttyfd, NULL, &str, 0) < 0) {
554 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
556 syslog(LOG_ERR, "Couldn't send packet: %m");
560 pfd.events = POLLOUT;
561 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
566 * wait_input - wait for input, for a length of time specified in *timo.
570 struct timeval *timo;
575 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
577 pfd.events = POLLIN | POLLPRI | POLLHUP;
578 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
579 syslog(LOG_ERR, "poll: %m");
585 * read_packet - get a PPP packet from the serial device.
591 struct strbuf str, ctl;
593 unsigned char ctlbuf[16];
595 str.maxlen = PPP_MTU + PPP_HDRLEN;
596 str.buf = (caddr_t) buf;
597 ctl.maxlen = sizeof(ctlbuf);
598 ctl.buf = (caddr_t) ctlbuf;
600 len = getmsg(ttyfd, &ctl, &str, &i);
602 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
605 syslog(LOG_ERR, "getmsg %m");
609 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
611 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
612 ctlbuf[0], ctlbuf[1]);
615 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
624 * ppp_send_config - configure the transmit characteristics of
628 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
636 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
638 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
639 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
643 if(ioctl(ttyfd, SIOCSIFASYNCMAP, asyncmap) < 0) {
644 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
649 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0) {
650 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
655 if(ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0) {
656 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
663 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
666 ppp_set_xaccm(unit, accm)
670 if (ioctl(ttyfd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
671 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
676 * ppp_recv_config - configure the receive-side characteristics of
680 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
687 if (ioctl(ttyfd, SIOCSIFMRU, mru) < 0) {
688 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
691 if (ioctl(ttyfd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
692 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
695 c = 2 + (pcomp? 1: 0);
696 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0) {
697 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
700 c = 2 + (accomp? 1: 0);
701 if (ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0) {
702 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
707 * ccp_test - ask kernel whether a given compression method
708 * is acceptable for use.
711 ccp_test(unit, opt_ptr, opt_len, for_transmit)
712 int unit, opt_len, for_transmit;
715 struct ppp_option_data data;
717 if ((unsigned) opt_len > MAX_PPP_OPTION)
718 opt_len = MAX_PPP_OPTION;
719 data.length = opt_len;
720 data.transmit = for_transmit;
721 BCOPY(opt_ptr, data.opt_data, opt_len);
722 if (ioctl(ttyfd, SIOCSCOMPRESS, &data) >= 0)
724 return (errno == ENOSR)? 0: -1;
728 * ccp_flags_set - inform kernel about the current state of CCP.
731 ccp_flags_set(unit, isopen, isup)
732 int unit, isopen, isup;
736 x = (isopen? 1: 0) + (isup? 2: 0);
737 if (ioctl(ttyfd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
738 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
742 * ccp_fatal_error - returns 1 if decompression was disabled as a
743 * result of an error detected after decompression of a packet,
744 * 0 otherwise. This is necessary because of patent nonsense.
747 ccp_fatal_error(unit)
752 if (ioctl(ttyfd, SIOCGIFCOMP, &x) < 0) {
753 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
756 return x & CCP_FATALERROR;
760 * sifvjcomp - config tcp header compression
763 sifvjcomp(u, vjcomp, cidcomp, maxcid)
764 int u, vjcomp, cidcomp, maxcid;
768 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
769 if (ioctl(ttyfd, SIOCSIFVJCOMP, x) < 0) {
770 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
777 * sifup - Config the interface up and enable IP packets to pass.
785 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
786 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
787 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
790 ifr.ifr_flags |= IFF_UP;
791 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
792 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
801 * sifnpmode - Set the mode for handling packets for a given NP.
804 sifnpmode(u, proto, mode)
811 npi.protocol = proto;
813 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
814 syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
821 * sifdown - Config the interface down.
832 npi.protocol = PPP_IP;
833 npi.mode = NPMODE_ERROR;
834 ioctl(ttyfd, SIOCSETNPMODE, &npi);
835 /* ignore errors, because ttyfd might have been closed by now. */
837 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
838 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
839 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
842 ifr.ifr_flags &= ~IFF_UP;
843 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
844 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
853 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
855 #define SET_SA_FAMILY(addr, family) \
856 BZERO((char *) &(addr), sizeof(addr)); \
857 addr.sa_family = (family);
860 * sifaddr - Config the interface IP addresses and netmask.
871 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
872 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
874 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
875 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
876 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
877 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
882 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
883 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
884 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
888 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
889 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
890 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
891 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
895 /* XXX is this necessary? */
896 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
897 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
898 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
908 * cifaddr - Clear the interface IP addresses, and delete routes
909 * through the interface if possible.
919 BZERO(&rt, sizeof(rt));
920 SET_SA_FAMILY(rt.rt_dst, AF_INET);
921 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
922 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
923 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
924 rt.rt_flags = RTF_HOST;
925 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
926 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
933 * sifdefaultroute - assign a default route through the address given.
936 sifdefaultroute(u, l, g)
942 BZERO(&rt, sizeof(rt));
943 SET_SA_FAMILY(rt.rt_dst, AF_INET);
944 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
945 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
946 rt.rt_flags = RTF_GATEWAY;
947 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
948 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
951 default_route_gateway = g;
956 * cifdefaultroute - delete a default route through the address given.
959 cifdefaultroute(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, SIOCDELRT, &rt) < 0) {
971 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
974 default_route_gateway = 0;
979 * sifproxyarp - Make a proxy ARP entry for the peer.
982 sifproxyarp(unit, hisaddr)
986 struct arpreq arpreq;
988 BZERO(&arpreq, sizeof(arpreq));
991 * Get the hardware address of an interface on the same subnet
992 * as our local address.
994 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
995 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
999 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1000 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1001 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1002 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1003 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1007 proxy_arp_addr = hisaddr;
1012 * cifproxyarp - Delete the proxy ARP entry for the peer.
1015 cifproxyarp(unit, hisaddr)
1019 struct arpreq arpreq;
1021 BZERO(&arpreq, sizeof(arpreq));
1022 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1023 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1024 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1025 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1033 * get_ether_addr - get the hardware address of an interface on the
1034 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
1035 * in the cslip-2.6 distribution, which is subject to the following
1036 * copyright notice (which also applies to logwtmp below):
1038 * Copyright (c) 1990, 1992 The Regents of the University of California.
1039 * All rights reserved.
1041 * Redistribution and use in source and binary forms, with or without
1042 * modification, are permitted provided that: (1) source code distributions
1043 * retain the above copyright notice and this paragraph in its entirety, (2)
1044 * distributions including binary code include the above copyright notice and
1045 * this paragraph in its entirety in the documentation or other materials
1046 * provided with the distribution, and (3) all advertising materials mentioning
1047 * features or use of this software display the following acknowledgement:
1048 * ``This product includes software developed by the University of California,
1049 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1050 * the University nor the names of its contributors may be used to endorse
1051 * or promote products derived from this software without specific prior
1052 * written permission.
1053 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1054 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1055 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1059 #include <arpa/inet.h>
1060 #include <netinet/in_var.h>
1061 #include <net/if_dl.h>
1063 /* Cast a struct sockaddr to a structaddr_in */
1064 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
1066 /* Determine if "bits" is set in "flag" */
1067 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
1069 static struct nlist nl[] = {
1075 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
1077 if (lseek(fd, offset, SEEK_SET) != offset) {
1078 syslog(LOG_ERR,"lseek in kmem: %m");
1081 return(read(fd, buf, nbytes));
1085 get_ether_addr(ipaddr, hwaddr)
1087 struct sockaddr *hwaddr;
1090 register struct ifnet *ifp;
1091 register struct arpcom *ac;
1092 struct arpcom arpcom;
1093 struct in_addr *inp;
1094 register struct ifaddr *ifa;
1095 register struct in_ifaddr *in;
1098 struct in_ifaddr in;
1100 struct ifaddr *ifap;
1101 struct sockaddr ifaddrsaddr;
1102 struct sockaddr_in ifmasksaddr;
1103 struct sockaddr_in *ifinetptr;
1104 struct sockaddr_dl *iflinkptr;
1105 u_long addr, mask, ifip;
1108 /* Open kernel memory for reading */
1109 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1110 syslog(LOG_ERR, "/dev/kmem: %m");
1114 /* Fetch namelist */
1115 if (nlist("/unix", nl) != 0) {
1116 syslog(LOG_ERR, "nlist(): %m");
1121 ifp = &arpcom.ac_if;
1125 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1127 syslog(LOG_ERR, "error reading ifnet addr");
1130 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1131 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1132 syslog(LOG_ERR, "error reading ifnet");
1136 /* Only look at configured, broadcast interfaces */
1137 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1139 /* This probably can't happen... */
1140 if (ifp->if_addrlist == 0)
1143 /* Get interface ip address */
1144 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1145 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1146 sizeof(ifaddr)) != sizeof(ifaddr)) {
1147 syslog(LOG_ERR, "error reading ifaddr");
1150 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1151 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1152 syslog(LOG_ERR, "error reading ifaddrsaddr");
1155 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1156 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1157 syslog(LOG_ERR, "error reading ifmasksaddr");
1160 /* Check if this interface on the right subnet */
1161 switch (ifaddrsaddr.sa_family) {
1163 hwaddr->sa_family = AF_UNSPEC;
1164 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1165 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1168 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1169 ifip = ifinetptr->sin_addr.s_addr;
1170 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1171 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1172 syslog(LOG_ERR, "error reading ifmasksaddr");
1175 mask = ifmasksaddr.sin_addr.s_addr;
1176 if ((ipaddr & mask) == (ifip & mask))
1189 * Return user specified netmask, modified by any mask we might determine
1190 * for address `addr' (in network byte order).
1191 * Here we scan through the system's list of interfaces, looking for
1192 * any non-point-to-point interfaces which might appear to be on the same
1193 * network as `addr'. If we find any, we OR in their netmask to the
1194 * user-specified netmask.
1202 u_int32_t mask, nmask, ina;
1203 struct ifreq *ifr, *ifend, ifreq;
1205 struct ifreq ifs[MAX_IFS];
1208 if (IN_CLASSA(addr)) /* determine network mask for address class */
1209 nmask = IN_CLASSA_NET;
1210 else if (IN_CLASSB(addr))
1211 nmask = IN_CLASSB_NET;
1213 nmask = IN_CLASSC_NET;
1214 /* class D nets are disallowed by bad_ip_adrs */
1215 mask = netmask | htonl(nmask);
1218 * Scan through the system's network interfaces.
1220 ifc.ifc_len = sizeof(ifs);
1222 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1223 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1226 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1227 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1229 * Check the interface's internet address.
1231 if (ifr->ifr_addr.sa_family != AF_INET)
1233 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1234 if ((ntohl(ina) & nmask) != (addr & nmask))
1237 * Check that the interface is up, and not point-to-point or loopback.
1239 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1240 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1242 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1246 * Get its netmask and OR it into our mask.
1248 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1250 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1256 #define WTMPFILE "/var/adm/wtmp"
1259 logwtmp(line, name, host)
1260 const char *line, *name, *host;
1266 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1268 if (!fstat(fd, &buf)) {
1269 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1270 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1271 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1272 (void)time(&ut.ut_time);
1273 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1274 (void)ftruncate(fd, buf.st_size);
1280 * Use the hostid as part of the random number seed.
1289 * Code for locking/unlocking the serial device.
1292 static char *devlocked = (char *) 0;
1294 int lock(char *device)
1299 if (devname = strrchr(device,'/'))
1304 if ((rc = ttylock(devname)) == 0) {
1305 devlocked = (char *) malloc(strlen(devname) + 1);
1306 sprintf(devlocked,"%s",devname);
1308 devlocked = (char *) 0;
1318 rc = ttyunlock(devlocked);
1320 devlocked = (char *) 0;