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.7 1995/10/27 03:45:34 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 */
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 default_route_gateway; /* Gateway for default route added */
77 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
79 /* Prototypes for procedures local to this file. */
80 static int translate_speed __P((int));
81 static int baud_rate_of __P((int));
82 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
86 * sys_init - System-dependent initialization.
91 openlog("pppd", LOG_PID | LOG_NDELAY, LOG_PPP);
92 setlogmask(LOG_UPTO(LOG_INFO));
94 setlogmask(LOG_UPTO(LOG_DEBUG));
96 /* Get an internet socket for doing socket ioctl's on. */
97 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
98 syslog(LOG_ERR, "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 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
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 if (default_route_gateway)
123 cifdefaultroute(0, default_route_gateway);
125 cifproxyarp(0, proxy_arp_addr);
129 * note_debug_level - note a change in the debug level.
135 syslog(LOG_INFO, "Debug turned ON, Level %d", debug);
136 setlogmask(LOG_UPTO(LOG_DEBUG));
138 setlogmask(LOG_UPTO(LOG_WARNING));
143 * daemon - Detach us from the terminal session.
146 daemon(nochdir, noclose)
147 int nochdir, noclose;
151 if ((pid = fork()) < 0)
154 exit(0); /* parent dies */
159 fclose(stdin); /* don't need stdin, stdout, stderr */
168 * ppp_available - check if this kernel supports PPP.
175 fd = open("/dev/tty", O_RDONLY, 0);
177 return 1; /* can't find out - assume we have ppp */
178 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
185 * 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 MAINDEBUG((LOG_DEBUG, "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 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
205 /* push the compress module */
206 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
207 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
209 /* finally, push the ppp_if module that actually handles the */
210 /* network interface */
211 if (ioctl(fd, I_PUSH, "pppif") < 0) {
212 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
216 /* read mode, message non-discard mode
217 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
218 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
223 * Find out which interface we were given.
224 * (ppp_if handles this ioctl)
226 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
227 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
231 /* Set debug flags in driver */
232 if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
233 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
236 /* close stdin, stdout, stderr if they might refer to the device */
237 if (default_device && !closed_stdio) {
240 for (i = 0; i <= 2; ++i)
241 if (i != fd && i != sockfd)
247 * Set device for non-blocking reads.
249 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
250 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
251 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
256 * disestablish_ppp - Restore the serial port to normal operation.
257 * It attempts to reconstruct the stream with the previously popped
258 * modules. This shouldn't call die() because it's called from die().
266 /* Reset non-blocking mode on the file descriptor. */
267 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
268 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
272 /* we can't push or pop modules after the stream has hung up */
273 str_module_count = 0;
274 restore_term = 0; /* nor can we fix up terminal settings */
280 * Check whether the link seems not to be 8-bit clean.
282 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
284 switch (~flags & PAI_FLAGS_HIBITS) {
286 s = "bit 7 set to 1";
289 s = "bit 7 set to 0";
291 case PAI_FLAGS_PAR_EVEN:
294 case PAI_FLAGS_PAR_ODD:
299 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
300 syslog(LOG_WARNING, "All received characters had %s", s);
305 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
309 for (; str_module_count > 0; str_module_count--) {
310 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
312 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
313 str_modules[str_module_count-1].modname);
315 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
316 str_modules[str_module_count-1].modname));
323 * List of valid speeds.
326 int speed_int, speed_val;
398 * Translate from bits/second to a speed_t.
404 struct speed *speedp;
408 for (speedp = speeds; speedp->speed_int; speedp++)
409 if (bps == speedp->speed_int)
410 return speedp->speed_val;
411 syslog(LOG_WARNING, "speed %d not supported", bps);
416 * Translate from a speed_t to bits/second.
422 struct speed *speedp;
426 for (speedp = speeds; speedp->speed_int; speedp++)
427 if (speed == speedp->speed_val)
428 return speedp->speed_int;
433 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
434 * at the requested speed, etc. If `local' is true, set CLOCAL
435 * regardless of whether the modem option was specified.
438 set_up_tty(fd, local)
445 if (tcgetattr(fd, &tios) < 0) {
446 syslog(LOG_ERR, "tcgetattr: %m");
453 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
455 bzero(&tiox, sizeof(tiox));
456 ioctl(fd, TCGETX, &tiox);
457 tiox.x_hflag = RTSXOFF;
458 ioctl(fd, TCSETX, &tiox);
459 tios.c_cflag &= ~(IXON | IXOFF);
460 } else if (crtscts < 0) {
461 bzero(&tiox, sizeof(tiox));
462 ioctl(fd, TCGETX, &tiox);
463 tiox.x_hflag &= ~RTSXOFF;
464 ioctl(fd, TCSETX, &tiox);
468 tios.c_cflag |= CS8 | CREAD | HUPCL;
470 tios.c_cflag |= CLOCAL;
471 tios.c_iflag = IGNBRK | IGNPAR;
475 tios.c_cc[VTIME] = 0;
478 tios.c_iflag |= IXON | IXOFF;
479 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
480 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
483 speed = translate_speed(inspeed);
485 cfsetospeed(&tios, speed);
486 cfsetispeed(&tios, speed);
488 speed = cfgetospeed(&tios);
490 * We can't proceed if the serial port speed is B0,
491 * since that implies that the serial port is disabled.
494 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
500 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
501 syslog(LOG_ERR, "tcsetattr: %m");
505 baud_rate = inspeed = baud_rate_of(speed);
510 * restore_tty - restore the terminal to the saved settings.
516 if (!default_device) {
518 * Turn off echoing, because otherwise we can get into
519 * a loop with the tty and the modem echoing to each other.
520 * We presume we are the sole user of this tty device, so
521 * when we close it, it will revert to its defaults anyway.
523 inittermios.c_lflag &= ~(ECHO | ECHONL);
525 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
527 syslog(LOG_WARNING, "tcsetattr: %m");
533 * setdtr - control the DTR line on the serial port.
534 * This is called from die(), so it shouldn't call die().
540 int modembits = TIOCM_DTR;
542 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
547 * output - Output PPP packet.
560 log_packet(p, len, "sent ");
563 str.buf = (caddr_t) p;
565 while (putmsg(fd, NULL, &str, 0) < 0) {
566 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
568 syslog(LOG_ERR, "Couldn't send packet: %m");
572 pfd.events = POLLOUT;
573 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
578 * wait_input - wait for input, for a length of time specified in *timo.
582 struct timeval *timo;
587 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
589 pfd.events = POLLIN | POLLPRI | POLLHUP;
590 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
591 syslog(LOG_ERR, "poll: %m");
597 * read_packet - get a PPP packet from the serial device.
603 struct strbuf str, ctl;
605 unsigned char ctlbuf[16];
607 str.maxlen = PPP_MTU + PPP_HDRLEN;
608 str.buf = (caddr_t) buf;
609 ctl.maxlen = sizeof(ctlbuf);
610 ctl.buf = (caddr_t) ctlbuf;
612 len = getmsg(fd, &ctl, &str, &i);
614 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
617 syslog(LOG_ERR, "getmsg(fd) %m");
621 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
623 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
624 ctlbuf[0], ctlbuf[1]);
627 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
636 * ppp_send_config - configure the transmit characteristics of
640 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
648 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
650 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
651 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
655 if(ioctl(fd, SIOCSIFASYNCMAP, asyncmap) < 0) {
656 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
661 if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
662 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
667 if(ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
668 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
675 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
678 ppp_set_xaccm(unit, accm)
682 if (ioctl(fd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
683 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
688 * ppp_recv_config - configure the receive-side characteristics of
692 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
699 if (ioctl(fd, SIOCSIFMRU, mru) < 0) {
700 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
703 if (ioctl(fd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
704 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
707 c = 2 + (pcomp? 1: 0);
708 if(ioctl(fd, SIOCSIFCOMPPROT, c) < 0) {
709 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
712 c = 2 + (accomp? 1: 0);
713 if (ioctl(fd, SIOCSIFCOMPAC, c) < 0) {
714 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
719 * ccp_test - ask kernel whether a given compression method
720 * is acceptable for use.
723 ccp_test(unit, opt_ptr, opt_len, for_transmit)
724 int unit, opt_len, for_transmit;
727 struct ppp_option_data data;
729 if ((unsigned) opt_len > MAX_PPP_OPTION)
730 opt_len = MAX_PPP_OPTION;
731 data.length = opt_len;
732 data.transmit = for_transmit;
733 BCOPY(opt_ptr, data.opt_data, opt_len);
734 if (ioctl(fd, SIOCSCOMPRESS, &data) >= 0)
736 return (errno == ENOSR)? 0: -1;
740 * ccp_flags_set - inform kernel about the current state of CCP.
743 ccp_flags_set(unit, isopen, isup)
744 int unit, isopen, isup;
748 x = (isopen? 1: 0) + (isup? 2: 0);
749 if (ioctl(fd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
750 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
754 * ccp_fatal_error - returns 1 if decompression was disabled as a
755 * result of an error detected after decompression of a packet,
756 * 0 otherwise. This is necessary because of patent nonsense.
759 ccp_fatal_error(unit)
764 if (ioctl(fd, SIOCGIFCOMP, &x) < 0) {
765 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
768 return x & CCP_FATALERROR;
772 * sifvjcomp - config tcp header compression
775 sifvjcomp(u, vjcomp, cidcomp, maxcid)
776 int u, vjcomp, cidcomp, maxcid;
780 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
781 if (ioctl(fd, SIOCSIFVJCOMP, x) < 0) {
782 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
789 * sifup - Config the interface up and enable IP packets to pass.
798 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
799 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
800 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
803 ifr.ifr_flags |= IFF_UP;
804 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
805 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
809 npi.protocol = PPP_IP;
810 npi.mode = NPMODE_PASS;
811 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
812 if (errno != ENOTTY) {
813 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
822 * sifdown - Config the interface down.
833 npi.protocol = PPP_IP;
834 npi.mode = NPMODE_ERROR;
835 if (ioctl(fd, SIOCSETNPMODE, &npi) < 0) {
836 if (errno != ENOTTY) {
837 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
842 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
843 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
844 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
847 ifr.ifr_flags &= ~IFF_UP;
848 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
849 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
858 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
860 #define SET_SA_FAMILY(addr, family) \
861 BZERO((char *) &(addr), sizeof(addr)); \
862 addr.sa_family = (family);
865 * sifaddr - Config the interface IP addresses and netmask.
876 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
877 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
879 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
880 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
881 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
882 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
887 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
888 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
889 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
893 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
894 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
895 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
896 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
899 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
900 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
901 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
908 * cifaddr - Clear the interface IP addresses, and delete routes
909 * through the interface if possible.
918 SET_SA_FAMILY(rt.rt_dst, AF_INET);
919 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
920 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
921 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
922 rt.rt_flags = RTF_HOST;
923 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
924 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
931 * sifdefaultroute - assign a default route through the address given.
934 sifdefaultroute(u, g)
940 SET_SA_FAMILY(rt.rt_dst, AF_INET);
941 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
942 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
943 rt.rt_flags = RTF_GATEWAY;
944 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
945 syslog(LOG_ERR, "default route ioctl(SIOCADDRT): %m");
948 default_route_gateway = g;
953 * cifdefaultroute - delete a default route through the address given.
956 cifdefaultroute(u, g)
962 SET_SA_FAMILY(rt.rt_dst, AF_INET);
963 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
964 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
965 rt.rt_flags = RTF_GATEWAY;
966 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
967 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
970 default_route_gateway = 0;
975 * sifproxyarp - Make a proxy ARP entry for the peer.
978 sifproxyarp(unit, hisaddr)
982 struct arpreq arpreq;
984 BZERO(&arpreq, sizeof(arpreq));
987 * Get the hardware address of an interface on the same subnet
988 * as our local address.
990 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
991 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
995 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
996 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
997 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
998 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
999 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1003 proxy_arp_addr = hisaddr;
1008 * cifproxyarp - Delete the proxy ARP entry for the peer.
1011 cifproxyarp(unit, hisaddr)
1015 struct arpreq arpreq;
1017 BZERO(&arpreq, sizeof(arpreq));
1018 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1019 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1020 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1021 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1029 * get_ether_addr - get the hardware address of an interface on the
1030 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
1031 * in the cslip-2.6 distribution, which is subject to the following
1032 * copyright notice (which also applies to logwtmp below):
1034 * Copyright (c) 1990, 1992 The Regents of the University of California.
1035 * All rights reserved.
1037 * Redistribution and use in source and binary forms, with or without
1038 * modification, are permitted provided that: (1) source code distributions
1039 * retain the above copyright notice and this paragraph in its entirety, (2)
1040 * distributions including binary code include the above copyright notice and
1041 * this paragraph in its entirety in the documentation or other materials
1042 * provided with the distribution, and (3) all advertising materials mentioning
1043 * features or use of this software display the following acknowledgement:
1044 * ``This product includes software developed by the University of California,
1045 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1046 * the University nor the names of its contributors may be used to endorse
1047 * or promote products derived from this software without specific prior
1048 * written permission.
1049 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1050 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1051 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1055 #include <arpa/inet.h>
1056 #include <netinet/in_var.h>
1057 #include <net/if_dl.h>
1059 /* Cast a struct sockaddr to a structaddr_in */
1060 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
1062 /* Determine if "bits" is set in "flag" */
1063 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
1065 static struct nlist nl[] = {
1071 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
1073 if (lseek(fd, offset, SEEK_SET) != offset) {
1074 syslog(LOG_ERR,"lseek in kmem: %m");
1077 return(read(fd, buf, nbytes));
1081 get_ether_addr(ipaddr, hwaddr)
1083 struct sockaddr *hwaddr;
1086 register struct ifnet *ifp;
1087 register struct arpcom *ac;
1088 struct arpcom arpcom;
1089 struct in_addr *inp;
1090 register struct ifaddr *ifa;
1091 register struct in_ifaddr *in;
1094 struct in_ifaddr in;
1096 struct ifaddr *ifap;
1097 struct sockaddr ifaddrsaddr;
1098 struct sockaddr_in ifmasksaddr;
1099 struct sockaddr_in *ifinetptr;
1100 struct sockaddr_dl *iflinkptr;
1101 u_long addr, mask, ifip;
1104 /* Open kernel memory for reading */
1105 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1106 syslog(LOG_ERR, "/dev/kmem: %m");
1110 /* Fetch namelist */
1111 if (nlist("/unix", nl) != 0) {
1112 syslog(LOG_ERR, "nlist(): %m");
1117 ifp = &arpcom.ac_if;
1121 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1123 syslog(LOG_ERR, "error reading ifnet addr");
1126 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1127 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1128 syslog(LOG_ERR, "error reading ifnet");
1132 /* Only look at configured, broadcast interfaces */
1133 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1135 /* This probably can't happen... */
1136 if (ifp->if_addrlist == 0)
1139 /* Get interface ip address */
1140 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1141 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1142 sizeof(ifaddr)) != sizeof(ifaddr)) {
1143 syslog(LOG_ERR, "error reading ifaddr");
1146 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1147 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1148 syslog(LOG_ERR, "error reading ifaddrsaddr");
1151 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1152 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1153 syslog(LOG_ERR, "error reading ifmasksaddr");
1156 /* Check if this interface on the right subnet */
1157 switch (ifaddrsaddr.sa_family) {
1159 hwaddr->sa_family = AF_UNSPEC;
1160 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1161 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1164 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1165 ifip = ifinetptr->sin_addr.s_addr;
1166 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1167 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1168 syslog(LOG_ERR, "error reading ifmasksaddr");
1171 mask = ifmasksaddr.sin_addr.s_addr;
1172 if ((ipaddr & mask) == (ifip & mask))
1185 * Return user specified netmask, modified by any mask we might determine
1186 * for address `addr' (in network byte order).
1187 * Here we scan through the system's list of interfaces, looking for
1188 * any non-point-to-point interfaces which might appear to be on the same
1189 * network as `addr'. If we find any, we OR in their netmask to the
1190 * user-specified netmask.
1198 u_int32_t mask, nmask, ina;
1199 struct ifreq *ifr, *ifend, ifreq;
1201 struct ifreq ifs[MAX_IFS];
1204 if (IN_CLASSA(addr)) /* determine network mask for address class */
1205 nmask = IN_CLASSA_NET;
1206 else if (IN_CLASSB(addr))
1207 nmask = IN_CLASSB_NET;
1209 nmask = IN_CLASSC_NET;
1210 /* class D nets are disallowed by bad_ip_adrs */
1211 mask = netmask | htonl(nmask);
1214 * Scan through the system's network interfaces.
1216 ifc.ifc_len = sizeof(ifs);
1218 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1219 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1222 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1223 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1225 * Check the interface's internet address.
1227 if (ifr->ifr_addr.sa_family != AF_INET)
1229 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1230 if ((ntohl(ina) & nmask) != (addr & nmask))
1233 * Check that the interface is up, and not point-to-point or loopback.
1235 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1236 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1238 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1242 * Get its netmask and OR it into our mask.
1244 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1246 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1252 #define WTMPFILE "/var/adm/wtmp"
1255 logwtmp(line, name, host)
1256 char *line, *name, *host;
1262 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1264 if (!fstat(fd, &buf)) {
1265 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1266 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1267 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1268 (void)time(&ut.ut_time);
1269 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1270 (void)ftruncate(fd, buf.st_size);
1276 * Code for locking/unlocking the serial device.
1279 static char *devlocked = (char *) 0;
1281 int lock(char *device)
1286 if (devname = strrchr(device,'/'))
1291 if ((rc = ttylock(devname)) == 0) {
1292 devlocked = (char *) malloc(strlen(devname) + 1);
1293 sprintf(devlocked,"%s",devname);
1295 devlocked = (char *) 0;
1305 rc = ttyunlock(devlocked);
1307 devlocked = (char *) 0;