Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src avoid use of kvm, mostly for boot floppy's convenience.



details:   https://anonhg.NetBSD.org/src/rev/a3457b094b1a
branches:  trunk
changeset: 480060:a3457b094b1a
user:      itojun <itojun%NetBSD.org@localhost>
date:      Fri Dec 31 19:40:19 1999 +0000

description:
avoid use of kvm, mostly for boot floppy's convenience.
(sync with latest kame)

diffstat:

 sbin/rtsol/Makefile      |    5 +-
 usr.sbin/rtsold/Makefile |    5 +-
 usr.sbin/rtsold/if.c     |  178 +++++++++++++++++++++-------------------------
 usr.sbin/rtsold/rtsol.c  |   20 +++++-
 4 files changed, 101 insertions(+), 107 deletions(-)

diffs (truncated from 326 to 300 lines):

diff -r 8f721d7b3fc9 -r a3457b094b1a sbin/rtsol/Makefile
--- a/sbin/rtsol/Makefile       Fri Dec 31 18:34:04 1999 +0000
+++ b/sbin/rtsol/Makefile       Fri Dec 31 19:40:19 1999 +0000
@@ -1,13 +1,10 @@
-# $NetBSD: Makefile,v 1.3 1999/12/09 15:08:35 itojun Exp $
+# $NetBSD: Makefile,v 1.4 1999/12/31 19:40:20 itojun Exp $
 
 PROG=  rtsol
 SRCS=  rtsold.c rtsol.c if.c probe.c dump.c
 
 CPPFLAGS+=-DINET6
 
-LDADD+=        -lkvm
-DPADD+=        ${LIBKVM}
-
 #MAN=  rtsold.8
 NOMAN= yes
 
diff -r 8f721d7b3fc9 -r a3457b094b1a usr.sbin/rtsold/Makefile
--- a/usr.sbin/rtsold/Makefile  Fri Dec 31 18:34:04 1999 +0000
+++ b/usr.sbin/rtsold/Makefile  Fri Dec 31 19:40:19 1999 +0000
@@ -1,13 +1,10 @@
-# $NetBSD: Makefile,v 1.3 1999/12/09 15:08:33 itojun Exp $
+# $NetBSD: Makefile,v 1.4 1999/12/31 19:40:19 itojun Exp $
 
 PROG=  rtsold
 SRCS=  rtsold.c rtsol.c if.c probe.c dump.c
 
 CPPFLAGS+=-DINET6
 
-LDADD+=        -lkvm
-DPADD+=        ${LIBKVM}
-
 MAN=   rtsold.8
 MLINKS=        rtsold.8 rtsol.8
 
diff -r 8f721d7b3fc9 -r a3457b094b1a usr.sbin/rtsold/if.c
--- a/usr.sbin/rtsold/if.c      Fri Dec 31 18:34:04 1999 +0000
+++ b/usr.sbin/rtsold/if.c      Fri Dec 31 19:40:19 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if.c,v 1.3 1999/12/09 15:08:33 itojun Exp $    */
+/*     $NetBSD: if.c,v 1.4 1999/12/31 19:40:19 itojun Exp $    */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -64,15 +64,14 @@
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <kvm.h>
-#include <nlist.h>
 #include <limits.h>
 
 #include "rtsold.h"
 
 static int ifsock;
 
-static int getifa __P((char *name, struct in6_ifaddr *ifap));
+static int get_llflag __P((const char *name));
+static unsigned int if_maxindex __P((void));
 static void get_rtaddrs __P((int addrs, struct sockaddr *sa,
                             struct sockaddr **rti_info));
 
@@ -91,7 +90,7 @@
 interface_up(char *name)
 {
        struct ifreq ifr;
-       struct in6_ifaddr ifa;
+       int llflag;
 
        strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
 
@@ -111,24 +110,24 @@
 
        warnmsg(LOG_DEBUG, __FUNCTION__, "checking if %s is ready...", name);
 
-       if (getifa(name, &ifa) < 0) {
+       llflag = get_llflag(name);
+       if (llflag < 0) {
                warnmsg(LOG_WARNING, __FUNCTION__,
-                       "getifa() failed, anyway I'll try");
+                       "get_llflag() failed, anyway I'll try");
                return 0;
        }
 
-       if (!(ifa.ia6_flags & IN6_IFF_NOTREADY)) {
+       if (!(llflag & IN6_IFF_NOTREADY)) {
                warnmsg(LOG_DEBUG, __FUNCTION__,
                        "%s is ready", name);
                return(0);
-       }
-       else {
-               if (ifa.ia6_flags & IN6_IFF_TENTATIVE) {
+       } else {
+               if (llflag & IN6_IFF_TENTATIVE) {
                        warnmsg(LOG_DEBUG, __FUNCTION__, "%s is tentative",
                               name);
                        return IFS_TENTATIVE;
                }
-               if (ifa.ia6_flags & IN6_IFF_DUPLICATED)
+               if (llflag & IN6_IFF_DUPLICATED)
                        warnmsg(LOG_DEBUG, __FUNCTION__, "%s is duplicated",
                               name);
                return -1;
@@ -310,103 +309,86 @@
 
 /*------------------------------------------------------------*/
 
-static struct nlist nl[] = {
-#define N_IFNET        0
-       { "_ifnet" },
-       { "" },
-};
-
-#define KREAD(x, y, z) { \
-       if (kvm_read(kvmd, (u_long)x, (void *)y, sizeof(z)) != sizeof(z)) { \
-               warnmsg(LOG_ERR, __FUNCTION__, "kvm_read failed");      \
-               goto bad;                                               \
-       }                                                               \
-   }
-
+/* get ia6_flags for link-local addr on if.  returns -1 on error. */
 static int
-getifa(char *name, struct in6_ifaddr *ifap)
+get_llflag(const char *name)
 {
-       u_short index;
-       kvm_t *kvmd = NULL;
-       char buf[_POSIX2_LINE_MAX];
-       struct ifnet *ifp;
-       struct ifnet ifnet;
-       struct in6_ifaddr *ifa;
-
-       if (!ifap)
-               exit(1);
+       int s;
+       unsigned int maxif;
+       struct ifreq *iflist;
+       struct ifconf ifconf;
+       struct ifreq *ifr, *ifr_end;
+       struct sockaddr_in6 *sin6;
+       struct in6_ifreq ifr6;
 
-       index = (u_short)if_nametoindex(name);
-       if (index == 0) {
-               warnmsg(LOG_ERR, __FUNCTION__, "if_nametoindex failed for %s",
-                      name);
-               goto bad;
+       maxif = if_maxindex() + 1;
+       iflist = (struct ifreq *)malloc(maxif * BUFSIZ);        /* XXX */
+       if (iflist == NULL) {
+               warnmsg(LOG_ERR, __FUNCTION__, "not enough core");
+               exit(1);
        }
-       if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
-               warnmsg(LOG_ERR, __FUNCTION__, "kvm_openfiles failed");
-               goto bad;
+
+       if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
+               warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s",
+                   strerror(errno));
+               exit(1);
        }
-       if (kvm_nlist(kvmd, nl) < 0) {
-               warnmsg(LOG_ERR, __FUNCTION__, "kvm_nlist failed");
-               goto bad;
-       }
-       if (nl[N_IFNET].n_value == 0) {
-               warnmsg(LOG_ERR, __FUNCTION__, "symbol \"%s\" not found",
-                      nl[N_IFNET].n_name);
-               goto bad;
+       memset(&ifconf, 0, sizeof(ifconf));
+       ifconf.ifc_req = iflist;
+       ifconf.ifc_len = maxif * BUFSIZ;        /* XXX */
+       if (ioctl(s, SIOCGIFCONF, &ifconf) < 0) {
+               warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGIFCONF): %s",
+                   strerror(errno));
+               exit(1);
        }
 
-       KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
-       while (ifp) {
-               KREAD(ifp, &ifnet, struct ifnet);
-               if (ifnet.if_index == index)
-                       break;
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-               ifp = TAILQ_NEXT(&ifnet, if_list);
-#elif defined(__FreeBSD__) && __FreeBSD__ >= 3
-               ifp = TAILQ_NEXT(&ifnet, if_link);
-#else
-               ifp = ifnet.if_next;
-#endif
-       }
-       if (!ifp) {
-               warnmsg(LOG_ERR, __FUNCTION__, "interface \"%s\" not found",
-                      name);
-               goto bad;
+       /* Look for this interface in the list */
+       ifr_end = (struct ifreq *) (ifconf.ifc_buf + ifconf.ifc_len);
+       for (ifr = ifconf.ifc_req;
+            ifr < ifr_end;
+            ifr = (struct ifreq *) ((char *) &ifr->ifr_addr
+                                   + ifr->ifr_addr.sa_len)) {
+               if (strlen(ifr->ifr_name) != strlen(name)
+                || strncmp(ifr->ifr_name, name, strlen(name)) != 0)
+                       continue;
+               if (ifr->ifr_addr.sa_family != AF_INET6)
+                       continue;
+               sin6 = (struct sockaddr_in6 *)&ifr->ifr_addr;
+               if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
+                       continue;
+
+               memset(&ifr6, 0, sizeof(ifr6));
+               strcpy(ifr6.ifr_name, name);
+               memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
+               if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
+                       warnmsg(LOG_ERR, __FUNCTION__,
+                           "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
+                       exit(1);
+               }
+
+               free(iflist);
+               close(s);
+               return ifr6.ifr_ifru.ifru_flags6;
        }
 
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-       ifa = (struct in6_ifaddr *)TAILQ_FIRST(&ifnet.if_addrlist);
-#elif defined(__FreeBSD__) && __FreeBSD__ >= 3
-       ifa = (struct in6_ifaddr *)TAILQ_FIRST(&ifnet.if_addrhead);
-#else
-       ifa = (struct in6_ifaddr *)ifnet.if_addrlist;
-#endif
-       while (ifa) {
-               KREAD(ifa, ifap, *ifap);
-               if (ifap->ia_addr.sin6_family == AF_INET6
-                && IN6_IS_ADDR_LINKLOCAL(&ifap->ia_addr.sin6_addr)) {
-                       kvm_close(kvmd);
-                       return 0;
-               }
+       free(iflist);
+       close(s);
+       return -1;
+}
 
-#if defined(__NetBSD__) || defined(__OpenBSD__)
-               ifa = (struct in6_ifaddr *)
-                       TAILQ_NEXT((struct ifaddr *)ifap, ifa_list);
-#elif defined(__FreeBSD__) && __FreeBSD__ >= 3
-               ifa = (struct in6_ifaddr *)
-                       TAILQ_NEXT((struct ifaddr *)ifap, ifa_link);
-#else
-               ifa = (struct in6_ifaddr *)(((struct ifaddr *)ifap)->ifa_next);
-#endif
+static unsigned int
+if_maxindex()
+{
+       struct if_nameindex *p, *p0;
+       unsigned int max = 0;
+
+       p0 = if_nameindex();
+       for (p = p0; p && p->if_index && p->if_name; p++) {
+               if (max < p->if_index)
+                       max = p->if_index;
        }
-       warnmsg(LOG_ERR, __FUNCTION__, "no IPv6 link-local address for %s",
-              name);
-
-  bad:
-       if (kvmd)
-               kvm_close(kvmd);
-       return -1;
+       if_freenameindex(p0);
+       return max;
 }
 
 static void
diff -r 8f721d7b3fc9 -r a3457b094b1a usr.sbin/rtsold/rtsol.c
--- a/usr.sbin/rtsold/rtsol.c   Fri Dec 31 18:34:04 1999 +0000
+++ b/usr.sbin/rtsold/rtsol.c   Fri Dec 31 19:40:19 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtsol.c,v 1.3 1999/12/09 15:08:33 itojun Exp $ */
+/*     $NetBSD: rtsol.c,v 1.4 1999/12/31 19:40:19 itojun Exp $ */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -93,21 +93,39 @@
 
        /* specify to tell receiving interface */
        on = 1;
+#ifdef IPV6_RECVPKTINFO
+       if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
+                      sizeof(on)) < 0) {
+               warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVPKTINFO: %s",
+                      strerror(errno));
+               exit(1);
+       }
+#else  /* old adv. API */
        if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
                       sizeof(on)) < 0) {



Home | Main Index | Thread Index | Old Index