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.8 1996/01/01 23:04:13 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.
191 /* go through and save the name of all the modules, then pop em */
193 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
194 ioctl(fd, I_POP, 0) < 0)
196 MAINDEBUG((LOG_DEBUG, "popped stream module : %s",
197 str_modules[str_module_count].modname));
201 /* now push the async/fcs module */
202 if (ioctl(fd, I_PUSH, "pppasync") < 0) {
203 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_async): %m");
206 /* push the compress module */
207 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
208 syslog(LOG_WARNING, "ioctl(I_PUSH, ppp_comp): %m");
210 /* finally, push the ppp_if module that actually handles the */
211 /* network interface */
212 if (ioctl(fd, I_PUSH, "pppif") < 0) {
213 syslog(LOG_ERR, "ioctl(I_PUSH, ppp_if): %m");
217 /* read mode, message non-discard mode
218 if (ioctl(fd, I_SRDOPT, RMSGN) < 0) {
219 syslog(LOG_ERR, "ioctl(I_SRDOPT, RMSGN): %m");
224 * Find out which interface we were given.
225 * (ppp_if handles this ioctl)
227 if (ioctl(fd, SIOCGETU, &ifunit) < 0) {
228 syslog(LOG_ERR, "ioctl(SIOCGETU): %m");
232 /* Set debug flags in driver */
233 if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
234 syslog(LOG_ERR, "ioctl(SIOCSIFDEBUG): %m");
237 /* close stdin, stdout, stderr if they might refer to the device */
238 if (default_device && !closed_stdio) {
241 for (i = 0; i <= 2; ++i)
242 if (i != fd && i != sockfd)
248 * Set device for non-blocking reads.
250 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
251 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
252 syslog(LOG_WARNING, "Couldn't set device to non-blocking mode: %m");
257 * disestablish_ppp - Restore the serial port to normal operation.
258 * It attempts to reconstruct the stream with the previously popped
259 * modules. This shouldn't call die() because it's called from die().
268 /* Reset non-blocking mode on the file descriptor. */
269 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
270 syslog(LOG_WARNING, "Couldn't restore device fd flags: %m");
274 /* we can't push or pop modules after the stream has hung up */
275 str_module_count = 0;
276 restore_term = 0; /* nor can we fix up terminal settings */
282 * Check whether the link seems not to be 8-bit clean.
284 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
286 switch (~flags & PAI_FLAGS_HIBITS) {
288 s = "bit 7 set to 1";
291 s = "bit 7 set to 0";
293 case PAI_FLAGS_PAR_EVEN:
296 case PAI_FLAGS_PAR_ODD:
301 syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
302 syslog(LOG_WARNING, "All received characters had %s", s);
307 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
311 for (; str_module_count > 0; str_module_count--) {
312 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
314 syslog(LOG_WARNING, "str_restore: couldn't push module %s: %m",
315 str_modules[str_module_count-1].modname);
317 MAINDEBUG((LOG_INFO, "str_restore: pushed module %s",
318 str_modules[str_module_count-1].modname));
325 * List of valid speeds.
328 int speed_int, speed_val;
400 * Translate from bits/second to a speed_t.
406 struct speed *speedp;
410 for (speedp = speeds; speedp->speed_int; speedp++)
411 if (bps == speedp->speed_int)
412 return speedp->speed_val;
413 syslog(LOG_WARNING, "speed %d not supported", bps);
418 * Translate from a speed_t to bits/second.
424 struct speed *speedp;
428 for (speedp = speeds; speedp->speed_int; speedp++)
429 if (speed == speedp->speed_val)
430 return speedp->speed_int;
435 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
436 * at the requested speed, etc. If `local' is true, set CLOCAL
437 * regardless of whether the modem option was specified.
440 set_up_tty(fd, local)
447 if (tcgetattr(fd, &tios) < 0) {
448 syslog(LOG_ERR, "tcgetattr: %m");
455 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
457 bzero(&tiox, sizeof(tiox));
458 ioctl(fd, TCGETX, &tiox);
459 tiox.x_hflag = RTSXOFF;
460 ioctl(fd, TCSETX, &tiox);
461 tios.c_cflag &= ~(IXON | IXOFF);
462 } else if (crtscts < 0) {
463 bzero(&tiox, sizeof(tiox));
464 ioctl(fd, TCGETX, &tiox);
465 tiox.x_hflag &= ~RTSXOFF;
466 ioctl(fd, TCSETX, &tiox);
470 tios.c_cflag |= CS8 | CREAD | HUPCL;
472 tios.c_cflag |= CLOCAL;
473 tios.c_iflag = IGNBRK | IGNPAR;
477 tios.c_cc[VTIME] = 0;
480 tios.c_iflag |= IXON | IXOFF;
481 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
482 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
485 speed = translate_speed(inspeed);
487 cfsetospeed(&tios, speed);
488 cfsetispeed(&tios, speed);
490 speed = cfgetospeed(&tios);
492 * We can't proceed if the serial port speed is B0,
493 * since that implies that the serial port is disabled.
496 syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
502 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
503 syslog(LOG_ERR, "tcsetattr: %m");
507 baud_rate = inspeed = baud_rate_of(speed);
512 * restore_tty - restore the terminal to the saved settings.
519 if (!default_device) {
521 * Turn off echoing, because otherwise we can get into
522 * a loop with the tty and the modem echoing to each other.
523 * We presume we are the sole user of this tty device, so
524 * when we close it, it will revert to its defaults anyway.
526 inittermios.c_lflag &= ~(ECHO | ECHONL);
528 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
530 syslog(LOG_WARNING, "tcsetattr: %m");
536 * setdtr - control the DTR line on the serial port.
537 * This is called from die(), so it shouldn't call die().
543 int modembits = TIOCM_DTR;
545 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
550 * output - Output PPP packet.
563 log_packet(p, len, "sent ");
566 str.buf = (caddr_t) p;
568 while (putmsg(ttyfd, NULL, &str, 0) < 0) {
569 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
571 syslog(LOG_ERR, "Couldn't send packet: %m");
575 pfd.events = POLLOUT;
576 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
581 * wait_input - wait for input, for a length of time specified in *timo.
585 struct timeval *timo;
590 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
592 pfd.events = POLLIN | POLLPRI | POLLHUP;
593 if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
594 syslog(LOG_ERR, "poll: %m");
600 * read_packet - get a PPP packet from the serial device.
606 struct strbuf str, ctl;
608 unsigned char ctlbuf[16];
610 str.maxlen = PPP_MTU + PPP_HDRLEN;
611 str.buf = (caddr_t) buf;
612 ctl.maxlen = sizeof(ctlbuf);
613 ctl.buf = (caddr_t) ctlbuf;
615 len = getmsg(ttyfd, &ctl, &str, &i);
617 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
620 syslog(LOG_ERR, "getmsg %m");
624 MAINDEBUG((LOG_DEBUG, "getmsg returned 0x%x",len));
626 syslog(LOG_NOTICE, "got ctrl msg len %d %x %x\n", ctl.len,
627 ctlbuf[0], ctlbuf[1]);
630 MAINDEBUG((LOG_DEBUG, "getmsg short return length %d", str.len));
639 * ppp_send_config - configure the transmit characteristics of
643 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
651 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
653 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0) {
654 syslog(LOG_ERR, "ioctl(SIOCSIFMTU): %m");
658 if(ioctl(ttyfd, SIOCSIFASYNCMAP, asyncmap) < 0) {
659 syslog(LOG_ERR, "ioctl(SIOCSIFASYNCMAP): %m");
664 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0) {
665 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
670 if(ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0) {
671 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
678 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
681 ppp_set_xaccm(unit, accm)
685 if (ioctl(ttyfd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
686 syslog(LOG_WARNING, "ioctl(set extended ACCM): %m");
691 * ppp_recv_config - configure the receive-side characteristics of
695 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
702 if (ioctl(ttyfd, SIOCSIFMRU, mru) < 0) {
703 syslog(LOG_ERR, "ioctl(SIOCSIFMRU): %m");
706 if (ioctl(ttyfd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
707 syslog(LOG_ERR, "ioctl(SIOCSIFRASYNCMAP): %m");
710 c = 2 + (pcomp? 1: 0);
711 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0) {
712 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPPROT): %m");
715 c = 2 + (accomp? 1: 0);
716 if (ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0) {
717 syslog(LOG_ERR, "ioctl(SIOCSIFCOMPAC): %m");
722 * ccp_test - ask kernel whether a given compression method
723 * is acceptable for use.
726 ccp_test(unit, opt_ptr, opt_len, for_transmit)
727 int unit, opt_len, for_transmit;
730 struct ppp_option_data data;
732 if ((unsigned) opt_len > MAX_PPP_OPTION)
733 opt_len = MAX_PPP_OPTION;
734 data.length = opt_len;
735 data.transmit = for_transmit;
736 BCOPY(opt_ptr, data.opt_data, opt_len);
737 if (ioctl(ttyfd, SIOCSCOMPRESS, &data) >= 0)
739 return (errno == ENOSR)? 0: -1;
743 * ccp_flags_set - inform kernel about the current state of CCP.
746 ccp_flags_set(unit, isopen, isup)
747 int unit, isopen, isup;
751 x = (isopen? 1: 0) + (isup? 2: 0);
752 if (ioctl(ttyfd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
753 syslog(LOG_ERR, "ioctl (SIOCSIFCOMP): %m");
757 * ccp_fatal_error - returns 1 if decompression was disabled as a
758 * result of an error detected after decompression of a packet,
759 * 0 otherwise. This is necessary because of patent nonsense.
762 ccp_fatal_error(unit)
767 if (ioctl(ttyfd, SIOCGIFCOMP, &x) < 0) {
768 syslog(LOG_ERR, "ioctl(SIOCGIFCOMP): %m");
771 return x & CCP_FATALERROR;
775 * sifvjcomp - config tcp header compression
778 sifvjcomp(u, vjcomp, cidcomp, maxcid)
779 int u, vjcomp, cidcomp, maxcid;
783 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
784 if (ioctl(ttyfd, SIOCSIFVJCOMP, x) < 0) {
785 syslog(LOG_ERR, "ioctl(SIOCSIFVJCOMP): %m");
792 * sifup - Config the interface up and enable IP packets to pass.
801 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
802 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
803 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
806 ifr.ifr_flags |= IFF_UP;
807 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
808 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
812 npi.protocol = PPP_IP;
813 npi.mode = NPMODE_PASS;
814 if (ioctl(ttyfd, SIOCSETNPMODE, &npi) < 0) {
815 if (errno != ENOTTY) {
816 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
825 * sifdown - Config the interface down.
836 npi.protocol = PPP_IP;
837 npi.mode = NPMODE_ERROR;
838 if (ioctl(ttyfd, SIOCSETNPMODE, &npi) < 0) {
839 if (errno != ENOTTY) {
840 syslog(LOG_ERR, "ioctl(SIOCSETNPMODE): %m");
845 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
846 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
847 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
850 ifr.ifr_flags &= ~IFF_UP;
851 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
852 syslog(LOG_ERR, "ioctl(SIOCSIFFLAGS): %m");
861 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
863 #define SET_SA_FAMILY(addr, family) \
864 BZERO((char *) &(addr), sizeof(addr)); \
865 addr.sa_family = (family);
868 * sifaddr - Config the interface IP addresses and netmask.
879 strncpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
880 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
882 syslog(LOG_INFO, "Setting interface mask to %s\n", ip_ntoa(m));
883 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
884 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
885 syslog(LOG_ERR, "ioctl(SIOCSIFNETMASK): %m");
890 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
891 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
892 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
896 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
897 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
898 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
899 syslog(LOG_ERR, "ioctl(SIOCSIFDSTADDR): %m");
902 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
903 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
904 syslog(LOG_ERR, "ioctl(SIOCSIFADDR): %m");
911 * cifaddr - Clear the interface IP addresses, and delete routes
912 * through the interface if possible.
921 SET_SA_FAMILY(rt.rt_dst, AF_INET);
922 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
923 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
924 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
925 rt.rt_flags = RTF_HOST;
926 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
927 syslog(LOG_ERR, "ioctl(SIOCDELRT): %m");
934 * sifdefaultroute - assign a default route through the address given.
937 sifdefaultroute(u, g)
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, g)
965 SET_SA_FAMILY(rt.rt_dst, AF_INET);
966 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
967 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
968 rt.rt_flags = RTF_GATEWAY;
969 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
970 syslog(LOG_ERR, "default route ioctl(SIOCDELRT): %m");
973 default_route_gateway = 0;
978 * sifproxyarp - Make a proxy ARP entry for the peer.
981 sifproxyarp(unit, hisaddr)
985 struct arpreq arpreq;
987 BZERO(&arpreq, sizeof(arpreq));
990 * Get the hardware address of an interface on the same subnet
991 * as our local address.
993 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
994 syslog(LOG_WARNING, "Cannot determine ethernet address for proxy ARP");
998 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
999 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1000 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1001 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1002 syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
1006 proxy_arp_addr = hisaddr;
1011 * cifproxyarp - Delete the proxy ARP entry for the peer.
1014 cifproxyarp(unit, hisaddr)
1018 struct arpreq arpreq;
1020 BZERO(&arpreq, sizeof(arpreq));
1021 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1022 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1023 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1024 syslog(LOG_ERR, "ioctl(SIOCDARP): %m");
1032 * get_ether_addr - get the hardware address of an interface on the
1033 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
1034 * in the cslip-2.6 distribution, which is subject to the following
1035 * copyright notice (which also applies to logwtmp below):
1037 * Copyright (c) 1990, 1992 The Regents of the University of California.
1038 * All rights reserved.
1040 * Redistribution and use in source and binary forms, with or without
1041 * modification, are permitted provided that: (1) source code distributions
1042 * retain the above copyright notice and this paragraph in its entirety, (2)
1043 * distributions including binary code include the above copyright notice and
1044 * this paragraph in its entirety in the documentation or other materials
1045 * provided with the distribution, and (3) all advertising materials mentioning
1046 * features or use of this software display the following acknowledgement:
1047 * ``This product includes software developed by the University of California,
1048 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1049 * the University nor the names of its contributors may be used to endorse
1050 * or promote products derived from this software without specific prior
1051 * written permission.
1052 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1053 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1054 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1058 #include <arpa/inet.h>
1059 #include <netinet/in_var.h>
1060 #include <net/if_dl.h>
1062 /* Cast a struct sockaddr to a structaddr_in */
1063 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
1065 /* Determine if "bits" is set in "flag" */
1066 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
1068 static struct nlist nl[] = {
1074 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
1076 if (lseek(fd, offset, SEEK_SET) != offset) {
1077 syslog(LOG_ERR,"lseek in kmem: %m");
1080 return(read(fd, buf, nbytes));
1084 get_ether_addr(ipaddr, hwaddr)
1086 struct sockaddr *hwaddr;
1089 register struct ifnet *ifp;
1090 register struct arpcom *ac;
1091 struct arpcom arpcom;
1092 struct in_addr *inp;
1093 register struct ifaddr *ifa;
1094 register struct in_ifaddr *in;
1097 struct in_ifaddr in;
1099 struct ifaddr *ifap;
1100 struct sockaddr ifaddrsaddr;
1101 struct sockaddr_in ifmasksaddr;
1102 struct sockaddr_in *ifinetptr;
1103 struct sockaddr_dl *iflinkptr;
1104 u_long addr, mask, ifip;
1107 /* Open kernel memory for reading */
1108 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1109 syslog(LOG_ERR, "/dev/kmem: %m");
1113 /* Fetch namelist */
1114 if (nlist("/unix", nl) != 0) {
1115 syslog(LOG_ERR, "nlist(): %m");
1120 ifp = &arpcom.ac_if;
1124 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1126 syslog(LOG_ERR, "error reading ifnet addr");
1129 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1130 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1131 syslog(LOG_ERR, "error reading ifnet");
1135 /* Only look at configured, broadcast interfaces */
1136 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1138 /* This probably can't happen... */
1139 if (ifp->if_addrlist == 0)
1142 /* Get interface ip address */
1143 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1144 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1145 sizeof(ifaddr)) != sizeof(ifaddr)) {
1146 syslog(LOG_ERR, "error reading ifaddr");
1149 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1150 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1151 syslog(LOG_ERR, "error reading ifaddrsaddr");
1154 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1155 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1156 syslog(LOG_ERR, "error reading ifmasksaddr");
1159 /* Check if this interface on the right subnet */
1160 switch (ifaddrsaddr.sa_family) {
1162 hwaddr->sa_family = AF_UNSPEC;
1163 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1164 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1167 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1168 ifip = ifinetptr->sin_addr.s_addr;
1169 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1170 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1171 syslog(LOG_ERR, "error reading ifmasksaddr");
1174 mask = ifmasksaddr.sin_addr.s_addr;
1175 if ((ipaddr & mask) == (ifip & mask))
1188 * Return user specified netmask, modified by any mask we might determine
1189 * for address `addr' (in network byte order).
1190 * Here we scan through the system's list of interfaces, looking for
1191 * any non-point-to-point interfaces which might appear to be on the same
1192 * network as `addr'. If we find any, we OR in their netmask to the
1193 * user-specified netmask.
1201 u_int32_t mask, nmask, ina;
1202 struct ifreq *ifr, *ifend, ifreq;
1204 struct ifreq ifs[MAX_IFS];
1207 if (IN_CLASSA(addr)) /* determine network mask for address class */
1208 nmask = IN_CLASSA_NET;
1209 else if (IN_CLASSB(addr))
1210 nmask = IN_CLASSB_NET;
1212 nmask = IN_CLASSC_NET;
1213 /* class D nets are disallowed by bad_ip_adrs */
1214 mask = netmask | htonl(nmask);
1217 * Scan through the system's network interfaces.
1219 ifc.ifc_len = sizeof(ifs);
1221 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1222 syslog(LOG_WARNING, "ioctl(SIOCGIFCONF): %m");
1225 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1226 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1228 * Check the interface's internet address.
1230 if (ifr->ifr_addr.sa_family != AF_INET)
1232 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1233 if ((ntohl(ina) & nmask) != (addr & nmask))
1236 * Check that the interface is up, and not point-to-point or loopback.
1238 strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1239 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1241 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1245 * Get its netmask and OR it into our mask.
1247 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1249 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1255 #define WTMPFILE "/var/adm/wtmp"
1258 logwtmp(line, name, host)
1259 char *line, *name, *host;
1265 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1267 if (!fstat(fd, &buf)) {
1268 (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1269 (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1270 (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1271 (void)time(&ut.ut_time);
1272 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1273 (void)ftruncate(fd, buf.st_size);
1279 * Code for locking/unlocking the serial device.
1282 static char *devlocked = (char *) 0;
1284 int lock(char *device)
1289 if (devname = strrchr(device,'/'))
1294 if ((rc = ttylock(devname)) == 0) {
1295 devlocked = (char *) malloc(strlen(devname) + 1);
1296 sprintf(devlocked,"%s",devname);
1298 devlocked = (char *) 0;
1308 rc = ttyunlock(devlocked);
1310 devlocked = (char *) 0;