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) 1994-2002 Paul Mackerras. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The name(s) of the authors of this software must not be used to
20 * endorse or promote products derived from this software without
21 * prior written permission.
23 * 4. Redistributions of any form whatsoever must retain the following
25 * "This product includes software developed by Paul Mackerras
26 * <paulus@samba.org>".
28 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
29 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
30 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
31 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
32 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
33 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
34 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 * Derived from main.c and pppd.h, which are:
38 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in
49 * the documentation and/or other materials provided with the
52 * 3. The name "Carnegie Mellon University" must not be used to
53 * endorse or promote products derived from this software without
54 * prior written permission. For permission or any legal
55 * details, please contact
56 * Office of Technology Transfer
57 * Carnegie Mellon University
59 * Pittsburgh, PA 15213-3890
60 * (412) 268-4387, fax: (412) 268-7395
61 * tech-transfer@andrew.cmu.edu
63 * 4. Redistributions of any form whatsoever must retain the following
65 * "This product includes software developed by Computing Services
66 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
68 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
69 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
70 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
71 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
72 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
73 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
74 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
77 #define RCSID "$Id: sys-aix4.c,v 1.23 2002/12/04 23:03:32 paulus Exp $"
89 #include <sys/termiox.h>
95 #include <sys/types.h>
96 #include <sys/socket.h>
98 #include <sys/stream.h>
99 #include <sys/stropts.h>
102 #include <net/ppp_defs.h>
103 #include <net/route.h>
104 #include <net/if_arp.h>
105 #include <netinet/in.h>
109 static const char rcsid[] = RCSID;
112 #define ifr_mtu ifr_metric
115 #define MAXMODULES 10 /* max number of module names to save */
116 static struct modlist {
117 char modname[FMNAMESZ+1];
118 } str_modules[MAXMODULES];
119 static int str_module_count = 0;
120 static int pushed_ppp;
121 static int closed_stdio;
123 static int restore_term; /* 1 => we've munged the terminal */
124 static struct termios inittermios; /* Initial TTY termios */
125 static int initfdflags = -1; /* Initial file descriptor flags for fd */
127 static int sockfd; /* socket for doing interface ioctls */
129 static int if_is_up; /* Interface has been marked up */
130 static u_int32_t ifaddrs[2]; /* local and remote addresses */
131 static u_int32_t default_route_gateway; /* Gateway for default route added */
132 static u_int32_t proxy_arp_addr; /* Addr for proxy arp entry added */
134 #define MAX_POLLFDS 32
135 static struct pollfd pollfds[MAX_POLLFDS];
136 static int n_pollfds;
138 /* Prototypes for procedures local to this file. */
139 static int translate_speed __P((int));
140 static int baud_rate_of __P((int));
141 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
145 * sys_init - System-dependent initialization.
150 /* Get an internet socket for doing socket ioctl's on. */
151 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
152 fatal("Couldn't create IP socket: %m");
158 * sys_cleanup - restore any system state we modified before exiting:
159 * mark the interface down, delete default route and/or proxy arp entry.
160 * This should call die() because it's called from die().
168 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
169 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) >= 0
170 && ((ifr.ifr_flags & IFF_UP) != 0)) {
171 ifr.ifr_flags &= ~IFF_UP;
172 ioctl(sockfd, SIOCSIFFLAGS, &ifr);
176 cifaddr(0, ifaddrs[0], ifaddrs[1]);
177 if (default_route_gateway)
178 cifdefaultroute(0, 0, default_route_gateway);
180 cifproxyarp(0, proxy_arp_addr);
184 * have_route_to - determine if the system has any route to
185 * a given IP address.
186 * For demand mode to work properly, we have to ignore routes
187 * through our own interface.
189 int have_route_to(u_int32_t addr)
196 * daemon - Detach us from the terminal session.
199 daemon(nochdir, noclose)
200 int nochdir, noclose;
204 if ((pid = fork()) < 0)
207 exit(0); /* parent dies */
212 fclose(stdin); /* don't need stdin, stdout, stderr */
221 * ppp_available - check if this kernel supports PPP.
228 fd = open("/dev/tty", O_RDONLY, 0);
230 return 1; /* can't find out - assume we have ppp */
231 ret = ioctl(fd, I_FIND, "pppasync") >= 0;
238 * establish_ppp - Turn the serial port into a ppp interface.
244 /* go through and save the name of all the modules, then pop em */
246 if (ioctl(fd, I_LOOK, str_modules[str_module_count].modname) < 0 ||
247 ioctl(fd, I_POP, 0) < 0)
249 SYSDEBUG(("popped stream module : %s",
250 str_modules[str_module_count].modname));
254 /* now push the async/fcs module */
255 if (ioctl(fd, I_PUSH, "pppasync") < 0)
256 fatal("ioctl(I_PUSH, ppp_async): %m");
257 /* push the compress module */
258 if (ioctl(fd, I_PUSH, "pppcomp") < 0) {
259 warn("ioctl(I_PUSH, ppp_comp): %m");
261 /* finally, push the ppp_if module that actually handles the */
262 /* network interface */
263 if (ioctl(fd, I_PUSH, "pppif") < 0)
264 fatal("ioctl(I_PUSH, ppp_if): %m");
266 /* read mode, message non-discard mode
267 if (ioctl(fd, I_SRDOPT, RMSGN) < 0)
268 fatal("ioctl(I_SRDOPT, RMSGN): %m");
271 * Find out which interface we were given.
272 * (ppp_if handles this ioctl)
274 if (ioctl(fd, SIOCGETU, &ifunit) < 0)
275 fatal("ioctl(SIOCGETU): %m");
277 /* Set debug flags in driver */
278 if (ioctl(fd, SIOCSIFDEBUG, kdebugflag) < 0) {
279 error("ioctl(SIOCSIFDEBUG): %m");
282 /* close stdin, stdout, stderr if they might refer to the device */
283 if (default_device && !closed_stdio) {
286 for (i = 0; i <= 2; ++i)
287 if (i != fd && i != sockfd)
293 * Set device for non-blocking reads.
295 if ((initfdflags = fcntl(fd, F_GETFL)) == -1
296 || fcntl(fd, F_SETFL, initfdflags | O_NONBLOCK) == -1) {
297 warn("Couldn't set device to non-blocking mode: %m");
304 * disestablish_ppp - Restore the serial port to normal operation.
305 * It attempts to reconstruct the stream with the previously popped
306 * modules. This shouldn't call die() because it's called from die().
315 /* Reset non-blocking mode on the file descriptor. */
316 if (initfdflags != -1 && fcntl(fd, F_SETFL, initfdflags) < 0)
317 warn("Couldn't restore device fd flags: %m");
321 /* we can't push or pop modules after the stream has hung up */
322 str_module_count = 0;
323 restore_term = 0; /* nor can we fix up terminal settings */
329 * Check whether the link seems not to be 8-bit clean.
331 if (ioctl(fd, SIOCGIFDEBUG, (caddr_t) &flags) == 0) {
333 switch (~flags & PAI_FLAGS_HIBITS) {
335 s = "bit 7 set to 1";
338 s = "bit 7 set to 0";
340 case PAI_FLAGS_PAR_EVEN:
343 case PAI_FLAGS_PAR_ODD:
348 warn("Serial link is not 8-bit clean:");
349 warn("All received characters had %s", s);
354 while (ioctl(fd, I_POP, 0) == 0) /* pop any we pushed */
358 for (; str_module_count > 0; str_module_count--) {
359 if (ioctl(fd, I_PUSH, str_modules[str_module_count-1].modname)) {
361 warn("str_restore: couldn't push module %s: %m",
362 str_modules[str_module_count-1].modname);
364 SYSDEBUG(("str_restore: pushed module %s",
365 str_modules[str_module_count-1].modname));
372 * List of valid speeds.
375 int speed_int, speed_val;
447 * Translate from bits/second to a speed_t.
453 struct speed *speedp;
457 for (speedp = speeds; speedp->speed_int; speedp++)
458 if (bps == speedp->speed_int)
459 return speedp->speed_val;
460 warn("speed %d not supported", bps);
465 * Translate from a speed_t to bits/second.
471 struct speed *speedp;
475 for (speedp = speeds; speedp->speed_int; speedp++)
476 if (speed == speedp->speed_val)
477 return speedp->speed_int;
482 * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
483 * at the requested speed, etc. If `local' is true, set CLOCAL
484 * regardless of whether the modem option was specified.
487 set_up_tty(fd, local)
494 if (tcgetattr(fd, &tios) < 0)
495 fatal("tcgetattr: %m");
501 tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
503 bzero(&tiox, sizeof(tiox));
504 ioctl(fd, TCGETX, &tiox);
505 tiox.x_hflag = RTSXOFF;
506 ioctl(fd, TCSETX, &tiox);
507 tios.c_cflag &= ~(IXON | IXOFF);
508 } else if (crtscts < 0) {
509 bzero(&tiox, sizeof(tiox));
510 ioctl(fd, TCGETX, &tiox);
511 tiox.x_hflag &= ~RTSXOFF;
512 ioctl(fd, TCSETX, &tiox);
516 tios.c_cflag |= CS8 | CREAD | HUPCL;
518 tios.c_cflag |= CLOCAL;
519 tios.c_iflag = IGNBRK | IGNPAR;
523 tios.c_cc[VTIME] = 0;
526 tios.c_iflag |= IXON | IXOFF;
527 tios.c_cc[VSTOP] = 0x13; /* DC3 = XOFF = ^S */
528 tios.c_cc[VSTART] = 0x11; /* DC1 = XON = ^Q */
531 speed = translate_speed(inspeed);
533 cfsetospeed(&tios, speed);
534 cfsetispeed(&tios, speed);
536 speed = cfgetospeed(&tios);
538 * We can't proceed if the serial port speed is B0,
539 * since that implies that the serial port is disabled.
542 fatal("Baud rate for %s is 0; need explicit baud rate", devnam);
545 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0)
546 fatal("tcsetattr: %m");
548 baud_rate = inspeed = baud_rate_of(speed);
553 * restore_tty - restore the terminal to the saved settings.
560 if (!default_device) {
562 * Turn off echoing, because otherwise we can get into
563 * a loop with the tty and the modem echoing to each other.
564 * We presume we are the sole user of this tty device, so
565 * when we close it, it will revert to its defaults anyway.
567 inittermios.c_lflag &= ~(ECHO | ECHONL);
569 if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
571 warn("tcsetattr: %m");
577 * setdtr - control the DTR line on the serial port.
578 * This is called from die(), so it shouldn't call die().
584 int modembits = TIOCM_DTR;
586 ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
591 * output - Output PPP packet.
604 dbglog("sent %P", p, len);
607 str.buf = (caddr_t) p;
609 while (putmsg(ttyfd, NULL, &str, 0) < 0) {
610 if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
612 error("Couldn't send packet: %m");
616 pfd.events = POLLOUT;
617 poll(&pfd, 1, 250); /* wait for up to 0.25 seconds */
622 * wait_input - wait for input, for a length of time specified in *timo.
626 struct timeval *timo;
630 t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
631 if (poll(pollfds, n_pollfds, t) < 0 && errno != EINTR)
636 * add_fd - add an fd to the set that wait_input waits for.
643 for (n = 0; n < n_pollfds; ++n)
644 if (pollfds[n].fd == fd)
646 if (n_pollfds < MAX_POLLFDS) {
647 pollfds[n_pollfds].fd = fd;
648 pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
651 error("Too many inputs!");
655 * remove_fd - remove an fd from the set that wait_input waits for.
662 for (n = 0; n < n_pollfds; ++n) {
663 if (pollfds[n].fd == fd) {
664 while (++n < n_pollfds)
665 pollfds[n-1] = pollfds[n];
674 * read_packet - get a PPP packet from the serial device.
680 struct strbuf str, ctl;
682 unsigned char ctlbuf[16];
684 str.maxlen = PPP_MTU + PPP_HDRLEN;
685 str.buf = (caddr_t) buf;
686 ctl.maxlen = sizeof(ctlbuf);
687 ctl.buf = (caddr_t) ctlbuf;
689 len = getmsg(ttyfd, &ctl, &str, &i);
691 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
697 SYSDEBUG(("getmsg returned 0x%x", len));
699 notice("got ctrl msg len %d %x %x\n", ctl.len, ctlbuf[0], ctlbuf[1]);
702 SYSDEBUG(("getmsg short return length %d", str.len));
711 * ppp_send_config - configure the transmit characteristics of
715 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
723 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
725 if (ioctl(sockfd, SIOCSIFMTU, (caddr_t) &ifr) < 0)
726 fatal("ioctl(SIOCSIFMTU): %m");
728 if(ioctl(ttyfd, SIOCSIFASYNCMAP, asyncmap) < 0)
729 fatal("ioctl(SIOCSIFASYNCMAP): %m");
732 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0)
733 fatal("ioctl(SIOCSIFCOMPPROT): %m");
736 if(ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0)
737 fatal("ioctl(SIOCSIFCOMPAC): %m");
742 * ppp_set_xaccm - set the extended transmit ACCM for the interface.
745 ppp_set_xaccm(unit, accm)
749 if (ioctl(ttyfd, SIOCSIFXASYNCMAP, accm) < 0 && errno != ENOTTY)
750 warn("ioctl(set extended ACCM): %m");
755 * ppp_recv_config - configure the receive-side characteristics of
759 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
766 if (ioctl(ttyfd, SIOCSIFMRU, mru) < 0) {
767 error("ioctl(SIOCSIFMRU): %m");
770 if (ioctl(ttyfd, SIOCSIFRASYNCMAP, (caddr_t) asyncmap) < 0) {
771 error("ioctl(SIOCSIFRASYNCMAP): %m");
774 c = 2 + (pcomp? 1: 0);
775 if(ioctl(ttyfd, SIOCSIFCOMPPROT, c) < 0) {
776 error("ioctl(SIOCSIFCOMPPROT): %m");
779 c = 2 + (accomp? 1: 0);
780 if (ioctl(ttyfd, SIOCSIFCOMPAC, c) < 0) {
781 error("ioctl(SIOCSIFCOMPAC): %m");
786 * ccp_test - ask kernel whether a given compression method
787 * is acceptable for use.
790 ccp_test(unit, opt_ptr, opt_len, for_transmit)
791 int unit, opt_len, for_transmit;
794 struct ppp_option_data data;
796 if ((unsigned) opt_len > MAX_PPP_OPTION)
797 opt_len = MAX_PPP_OPTION;
798 data.length = opt_len;
799 data.transmit = for_transmit;
800 BCOPY(opt_ptr, data.opt_data, opt_len);
801 if (ioctl(ttyfd, SIOCSCOMPRESS, &data) >= 0)
803 return (errno == ENOSR)? 0: -1;
807 * ccp_flags_set - inform kernel about the current state of CCP.
810 ccp_flags_set(unit, isopen, isup)
811 int unit, isopen, isup;
815 x = (isopen? 1: 0) + (isup? 2: 0);
816 if (ioctl(ttyfd, SIOCSIFCOMP, x) < 0 && errno != ENOTTY)
817 error("ioctl (SIOCSIFCOMP): %m");
821 * ccp_fatal_error - returns 1 if decompression was disabled as a
822 * result of an error detected after decompression of a packet,
823 * 0 otherwise. This is necessary because of patent nonsense.
826 ccp_fatal_error(unit)
831 if (ioctl(ttyfd, SIOCGIFCOMP, &x) < 0) {
832 error("ioctl(SIOCGIFCOMP): %m");
835 return x & CCP_FATALERROR;
839 * sifvjcomp - config tcp header compression
842 sifvjcomp(u, vjcomp, cidcomp, maxcid)
843 int u, vjcomp, cidcomp, maxcid;
847 x = (vjcomp? 1: 0) + (cidcomp? 0: 2) + (maxcid << 4);
848 if (ioctl(ttyfd, SIOCSIFVJCOMP, x) < 0) {
849 error("ioctl(SIOCSIFVJCOMP): %m");
856 * sifup - Config the interface up and enable IP packets to pass.
864 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
865 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
866 error("ioctl (SIOCGIFFLAGS): %m");
869 ifr.ifr_flags |= IFF_UP;
870 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
871 error("ioctl(SIOCSIFFLAGS): %m");
880 * sifnpmode - Set the mode for handling packets for a given NP.
883 sifnpmode(u, proto, mode)
890 npi.protocol = proto;
892 if (ioctl(ppp_fd, PPPIOCSNPMODE, &npi) < 0) {
893 error("ioctl(set NP %d mode to %d): %m", proto, mode);
900 * sifdown - Config the interface down.
911 npi.protocol = PPP_IP;
912 npi.mode = NPMODE_ERROR;
913 ioctl(ttyfd, SIOCSETNPMODE, &npi);
914 /* ignore errors, because ttyfd might have been closed by now. */
916 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
917 if (ioctl(sockfd, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
918 error("ioctl (SIOCGIFFLAGS): %m");
921 ifr.ifr_flags &= ~IFF_UP;
922 if (ioctl(sockfd, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
923 error("ioctl(SIOCSIFFLAGS): %m");
932 * SET_SA_FAMILY - initialize a struct sockaddr, setting the sa_family field.
934 #define SET_SA_FAMILY(addr, family) \
935 BZERO((char *) &(addr), sizeof(addr)); \
936 addr.sa_family = (family);
939 * sifaddr - Config the interface IP addresses and netmask.
950 strlcpy(ifr.ifr_name, ifname, sizeof (ifr.ifr_name));
951 SET_SA_FAMILY(ifr.ifr_addr, AF_INET);
953 info("Setting interface mask to %s\n", ip_ntoa(m));
954 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = m;
955 if (ioctl(sockfd, SIOCSIFNETMASK, (caddr_t) &ifr) < 0) {
956 error("ioctl(SIOCSIFNETMASK): %m");
961 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
962 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
963 error("ioctl(SIOCSIFADDR): %m");
967 SET_SA_FAMILY(ifr.ifr_dstaddr, AF_INET);
968 ((struct sockaddr_in *) &ifr.ifr_dstaddr)->sin_addr.s_addr = h;
969 if (ioctl(sockfd, SIOCSIFDSTADDR, (caddr_t) &ifr) < 0) {
970 error("ioctl(SIOCSIFDSTADDR): %m");
974 /* XXX is this necessary? */
975 ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr = o;
976 if (ioctl(sockfd, SIOCSIFADDR, (caddr_t) &ifr) < 0) {
977 error("ioctl(SIOCSIFADDR): %m");
987 * cifaddr - Clear the interface IP addresses, and delete routes
988 * through the interface if possible.
998 BZERO(&rt, sizeof(rt));
999 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1000 ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr = h;
1001 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1002 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = o;
1003 rt.rt_flags = RTF_HOST;
1004 if (ioctl(sockfd, SIOCDELRT, (caddr_t) &rt) < 0) {
1005 error("ioctl(SIOCDELRT): %m");
1012 * sifdefaultroute - assign a default route through the address given.
1015 sifdefaultroute(u, l, g)
1021 BZERO(&rt, sizeof(rt));
1022 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1023 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1024 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1025 rt.rt_flags = RTF_GATEWAY;
1026 if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1027 error("default route ioctl(SIOCADDRT): %m");
1030 default_route_gateway = g;
1035 * cifdefaultroute - delete a default route through the address given.
1038 cifdefaultroute(u, l, g)
1044 BZERO(&rt, sizeof(rt));
1045 SET_SA_FAMILY(rt.rt_dst, AF_INET);
1046 SET_SA_FAMILY(rt.rt_gateway, AF_INET);
1047 ((struct sockaddr_in *) &rt.rt_gateway)->sin_addr.s_addr = g;
1048 rt.rt_flags = RTF_GATEWAY;
1049 if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1050 error("default route ioctl(SIOCDELRT): %m");
1053 default_route_gateway = 0;
1058 * sifproxyarp - Make a proxy ARP entry for the peer.
1061 sifproxyarp(unit, hisaddr)
1065 struct arpreq arpreq;
1067 BZERO(&arpreq, sizeof(arpreq));
1070 * Get the hardware address of an interface on the same subnet
1071 * as our local address.
1073 if (!get_ether_addr(hisaddr, &arpreq.arp_ha)) {
1074 warn("Cannot determine ethernet address for proxy ARP");
1078 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1079 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1080 arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1081 if (ioctl(sockfd, SIOCSARP, (caddr_t)&arpreq) < 0) {
1082 error("ioctl(SIOCSARP): %m");
1086 proxy_arp_addr = hisaddr;
1091 * cifproxyarp - Delete the proxy ARP entry for the peer.
1094 cifproxyarp(unit, hisaddr)
1098 struct arpreq arpreq;
1100 BZERO(&arpreq, sizeof(arpreq));
1101 SET_SA_FAMILY(arpreq.arp_pa, AF_INET);
1102 ((struct sockaddr_in *) &arpreq.arp_pa)->sin_addr.s_addr = hisaddr;
1103 if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1104 error("ioctl(SIOCDARP): %m");
1112 * get_ether_addr - get the hardware address of an interface on the
1113 * the same subnet as ipaddr. Code borrowed from myetheraddr.c
1114 * in the cslip-2.6 distribution, which is subject to the following
1115 * copyright notice (which also applies to logwtmp below):
1117 * Copyright (c) 1990, 1992 The Regents of the University of California.
1118 * All rights reserved.
1120 * Redistribution and use in source and binary forms, with or without
1121 * modification, are permitted provided that: (1) source code distributions
1122 * retain the above copyright notice and this paragraph in its entirety, (2)
1123 * distributions including binary code include the above copyright notice and
1124 * this paragraph in its entirety in the documentation or other materials
1125 * provided with the distribution, and (3) all advertising materials mentioning
1126 * features or use of this software display the following acknowledgement:
1127 * ``This product includes software developed by the University of California,
1128 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1129 * the University nor the names of its contributors may be used to endorse
1130 * or promote products derived from this software without specific prior
1131 * written permission.
1132 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1133 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1134 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1138 #include <arpa/inet.h>
1139 #include <netinet/in_var.h>
1140 #include <net/if_dl.h>
1142 /* Cast a struct sockaddr to a structaddr_in */
1143 #define SATOSIN(sa) ((struct sockaddr_in *)(sa))
1145 /* Determine if "bits" is set in "flag" */
1146 #define ALLSET(flag, bits) (((flag) & (bits)) == (bits))
1148 static struct nlist nl[] = {
1154 int kvm_read(int fd, off_t offset, void *buf, int nbytes)
1156 if (lseek(fd, offset, SEEK_SET) != offset) {
1157 error("lseek in kmem: %m");
1160 return(read(fd, buf, nbytes));
1164 get_ether_addr(ipaddr, hwaddr)
1166 struct sockaddr *hwaddr;
1169 register struct ifnet *ifp;
1170 register struct arpcom *ac;
1171 struct arpcom arpcom;
1172 struct in_addr *inp;
1173 register struct ifaddr *ifa;
1174 register struct in_ifaddr *in;
1177 struct in_ifaddr in;
1179 struct ifaddr *ifap;
1180 struct sockaddr ifaddrsaddr;
1181 struct sockaddr_in ifmasksaddr;
1182 struct sockaddr_in *ifinetptr;
1183 struct sockaddr_dl *iflinkptr;
1184 u_long addr, mask, ifip;
1187 /* Open kernel memory for reading */
1188 if ((kd = open("/dev/kmem", O_RDONLY)) < 0) {
1189 error("/dev/kmem: %m");
1193 /* Fetch namelist */
1194 if (nlist("/unix", nl) != 0) {
1195 error("nlist(): %m");
1200 ifp = &arpcom.ac_if;
1204 if (kvm_read(kd, nl[N_IFNET].n_value, (char *)&addr, sizeof(addr))
1206 error("error reading ifnet addr");
1209 for ( ; addr && !found; addr = (u_long)ifp->if_next) {
1210 if (kvm_read(kd, addr, (char *)ac, sizeof(*ac)) != sizeof(*ac)) {
1211 error("error reading ifnet");
1215 /* Only look at configured, broadcast interfaces */
1216 if (!ALLSET(ifp->if_flags, IFF_UP | IFF_BROADCAST))
1218 /* This probably can't happen... */
1219 if (ifp->if_addrlist == 0)
1222 /* Get interface ip address */
1223 for (ifap = ifp->if_addrlist; ifap; ifap=ifaddr.ifa.ifa_next) {
1224 if (kvm_read(kd, (u_long)ifap, (char *)&ifaddr,
1225 sizeof(ifaddr)) != sizeof(ifaddr)) {
1226 error("error reading ifaddr");
1229 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_addr, &ifaddrsaddr,
1230 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1231 error("error reading ifaddrsaddr");
1234 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1235 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1236 error("error reading ifmasksaddr");
1239 /* Check if this interface on the right subnet */
1240 switch (ifaddrsaddr.sa_family) {
1242 hwaddr->sa_family = AF_UNSPEC;
1243 iflinkptr = (struct sockaddr_dl *) &ifaddrsaddr;
1244 bcopy(LLADDR(iflinkptr),hwaddr->sa_data,iflinkptr->sdl_alen);
1247 ifinetptr= (struct sockaddr_in *) &ifaddrsaddr;
1248 ifip = ifinetptr->sin_addr.s_addr;
1249 if (kvm_read(kd, (u_long)ifaddr.ifa.ifa_netmask, &ifmasksaddr,
1250 sizeof(struct sockaddr_in)) != sizeof(struct sockaddr_in)) {
1251 error("error reading ifmasksaddr");
1254 mask = ifmasksaddr.sin_addr.s_addr;
1255 if ((ipaddr & mask) == (ifip & mask))
1268 * Return user specified netmask, modified by any mask we might determine
1269 * for address `addr' (in network byte order).
1270 * Here we scan through the system's list of interfaces, looking for
1271 * any non-point-to-point interfaces which might appear to be on the same
1272 * network as `addr'. If we find any, we OR in their netmask to the
1273 * user-specified netmask.
1281 u_int32_t mask, nmask, ina;
1282 struct ifreq *ifr, *ifend, ifreq;
1284 struct ifreq ifs[MAX_IFS];
1287 if (IN_CLASSA(addr)) /* determine network mask for address class */
1288 nmask = IN_CLASSA_NET;
1289 else if (IN_CLASSB(addr))
1290 nmask = IN_CLASSB_NET;
1292 nmask = IN_CLASSC_NET;
1293 /* class D nets are disallowed by bad_ip_adrs */
1294 mask = netmask | htonl(nmask);
1297 * Scan through the system's network interfaces.
1299 ifc.ifc_len = sizeof(ifs);
1301 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1302 warn("ioctl(SIOCGIFCONF): %m");
1305 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1306 for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1308 * Check the interface's internet address.
1310 if (ifr->ifr_addr.sa_family != AF_INET)
1312 ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1313 if ((ntohl(ina) & nmask) != (addr & nmask))
1316 * Check that the interface is up, and not point-to-point or loopback.
1318 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1319 if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1321 if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1325 * Get its netmask and OR it into our mask.
1327 if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1329 mask |= ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr;
1335 #define WTMPFILE "/var/adm/wtmp"
1338 logwtmp(line, name, host)
1339 const char *line, *name, *host;
1345 if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1347 if (!fstat(fd, &buf)) {
1348 strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1349 strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1350 strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1351 (void)time(&ut.ut_time);
1352 if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1353 (void)ftruncate(fd, buf.st_size);
1359 * Use the hostid as part of the random number seed.
1368 * Code for locking/unlocking the serial device.
1371 static char *devlocked = (char *) 0;
1373 int lock(char *device)
1378 if (devname = strrchr(device,'/'))
1383 if ((rc = ttylock(devname)) == 0) {
1384 devlocked = strdup(devname);
1386 devlocked = (char *) 0;
1396 rc = ttyunlock(devlocked);
1398 devlocked = (char *) 0;