Source-Changes-HG archive

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

[src/trunk]: src/sbin/ifconfig Return to printing explicit "netmask 0x...." i...



details:   https://anonhg.NetBSD.org/src/rev/ed68c61610cf
branches:  trunk
changeset: 818179:ed68c61610cf
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Oct 01 20:59:49 2016 +0000

description:
Return to printing explicit "netmask 0x...." in the case that the
mask set is non-contiguous.   We don't prohibit setting such things
(even if they are basically useless) so they can be set by accident.
        ifconfig ifN 10.0.0.1 netmask 225.0.0.0
produced
        ifN .. inet 10.0.0.1/8
with the previous form (since 225 is 0xE1), now it produces
        ifN ... inet 10.0.0.1 netmask 0xe1000000

If the "netmask" form ever appears in ifconfig output, it (now)
means that the netmask is non-contig, which should make that case
obvious (whther intended, or set by accident)

diffstat:

 sbin/ifconfig/af_inet.c |  40 ++++++++++++++++++++++++++++++++--------
 1 files changed, 32 insertions(+), 8 deletions(-)

diffs (95 lines):

diff -r a867bcb43648 -r ed68c61610cf sbin/ifconfig/af_inet.c
--- a/sbin/ifconfig/af_inet.c   Sat Oct 01 20:18:06 2016 +0000
+++ b/sbin/ifconfig/af_inet.c   Sat Oct 01 20:59:49 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: af_inet.c,v 1.23 2016/10/01 15:10:58 roy Exp $ */
+/*     $NetBSD: af_inet.c,v 1.24 2016/10/01 20:59:49 kre Exp $ */
 
 /*
  * Copyright (c) 1983, 1993
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: af_inet.c,v 1.23 2016/10/01 15:10:58 roy Exp $");
+__RCSID("$NetBSD: af_inet.c,v 1.24 2016/10/01 20:59:49 kre Exp $");
 #endif /* not lint */
 
 #include <sys/param.h> 
@@ -64,6 +64,7 @@
 static void in_commit_address(prop_dictionary_t, prop_dictionary_t);
 static bool in_addr_tentative(struct ifaddrs *);
 static bool in_addr_tentative_or_detached(struct ifaddrs *);
+static in_addr_t in_netmask(struct sockaddr *);;
 static int  in_prefixlen(struct sockaddr *);
 static void in_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t);
 
@@ -74,17 +75,34 @@
        .af_addr_tentative_or_detached = in_addr_tentative_or_detached
 };
 
+static in_addr_t
+in_netmask(struct sockaddr *sa)
+{
+       struct sockaddr_in sin;
+
+       memset(&sin, 0, sizeof(sin));
+       memcpy(&sin, sa, sa->sa_len);
+       return ntohl(sin.sin_addr.s_addr);
+}
+
 static int
 in_prefixlen(struct sockaddr *sa)
 {
-       struct sockaddr_in sin;
        in_addr_t mask;
        int cidr;
 
-       memset(&sin, 0, sizeof(sin));
-       memcpy(&sin, sa, sa->sa_len);
-       mask = ntohl(sin.sin_addr.s_addr);
-       cidr = 33 - ffs(mask);
+       mask = in_netmask(sa);
+       if (mask == 0)                  /* mask 0 ==> /0 */
+               return 0;
+
+       cidr = 33 - ffs(mask);          /* 33 - (1 .. 32) -> 32 .. 1 */
+
+       if (cidr < 32) {                /* more than 1 bit in mask */
+               /* check for non-contig netmask */
+               if ((mask ^ (((1 << cidr) - 1) << (32 - cidr))) != 0)
+                       return -1;      /* noncontig, no pfxlen */
+       }
+
        return cidr;
 }
 
@@ -93,6 +111,7 @@
 {
        char hbuf[NI_MAXHOST];
        const int niflag = Nflag ? 0 : NI_NUMERICHOST;
+       int pfxlen;
        char fbuf[1024];
 
        if (lflag)
@@ -102,7 +121,9 @@
                        hbuf, sizeof(hbuf), NULL, 0, niflag))
                strlcpy(hbuf, "", sizeof(hbuf));        /* some message? */
        printf("\tinet %s", hbuf);
-       printf("/%d", in_prefixlen(ifa->ifa_netmask));
+       pfxlen = in_prefixlen(ifa->ifa_netmask);
+       if (pfxlen >= 0)
+               printf("/%d", pfxlen);
 
        if (ifa->ifa_flags & IFF_POINTOPOINT) {
                if (getnameinfo(ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_len,
@@ -111,6 +132,9 @@
                printf(" -> %s", hbuf);
        }
 
+       if (pfxlen < 0)
+               printf(" netmask %#x", in_netmask(ifa->ifa_netmask));
+
        if (ifa->ifa_flags & IFF_BROADCAST) {
                if (getnameinfo(ifa->ifa_broadaddr, ifa->ifa_broadaddr->sa_len,
                                hbuf, sizeof(hbuf), NULL, 0, niflag))



Home | Main Index | Thread Index | Old Index