Sat Oct 1 20:59:49 2016 UTC ()
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)


(kre)
diff -r1.23 -r1.24 src/sbin/ifconfig/af_inet.c

cvs diff -r1.23 -r1.24 src/sbin/ifconfig/af_inet.c (expand / switch to unified diff)

--- src/sbin/ifconfig/af_inet.c 2016/10/01 15:10:58 1.23
+++ src/sbin/ifconfig/af_inet.c 2016/10/01 20:59:49 1.24
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: af_inet.c,v 1.23 2016/10/01 15:10:58 roy Exp $ */ 1/* $NetBSD: af_inet.c,v 1.24 2016/10/01 20:59:49 kre Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1983, 1993 4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__RCSID("$NetBSD: af_inet.c,v 1.23 2016/10/01 15:10:58 roy Exp $"); 34__RCSID("$NetBSD: af_inet.c,v 1.24 2016/10/01 20:59:49 kre Exp $");
35#endif /* not lint */ 35#endif /* not lint */
36 36
37#include <sys/param.h>  37#include <sys/param.h>
38#include <sys/ioctl.h>  38#include <sys/ioctl.h>
39#include <sys/socket.h> 39#include <sys/socket.h>
40 40
41#include <net/if.h>  41#include <net/if.h>
42#include <netinet/in.h> 42#include <netinet/in.h>
43#include <netinet/in_var.h> 43#include <netinet/in_var.h>
44 44
45#include <arpa/inet.h> 45#include <arpa/inet.h>
46 46
47#include <assert.h> 47#include <assert.h>
@@ -54,73 +54,97 @@ __RCSID("$NetBSD: af_inet.c,v 1.23 2016/ @@ -54,73 +54,97 @@ __RCSID("$NetBSD: af_inet.c,v 1.23 2016/
54#include <stdio.h> 54#include <stdio.h>
55#include <util.h> 55#include <util.h>
56 56
57#include "env.h" 57#include "env.h"
58#include "extern.h" 58#include "extern.h"
59#include "af_inetany.h" 59#include "af_inetany.h"
60#include "prog_ops.h" 60#include "prog_ops.h"
61 61
62static void in_constructor(void) __attribute__((constructor)); 62static void in_constructor(void) __attribute__((constructor));
63static void in_status(prop_dictionary_t, prop_dictionary_t, bool); 63static void in_status(prop_dictionary_t, prop_dictionary_t, bool);
64static void in_commit_address(prop_dictionary_t, prop_dictionary_t); 64static void in_commit_address(prop_dictionary_t, prop_dictionary_t);
65static bool in_addr_tentative(struct ifaddrs *); 65static bool in_addr_tentative(struct ifaddrs *);
66static bool in_addr_tentative_or_detached(struct ifaddrs *); 66static bool in_addr_tentative_or_detached(struct ifaddrs *);
 67static in_addr_t in_netmask(struct sockaddr *);;
67static int in_prefixlen(struct sockaddr *); 68static int in_prefixlen(struct sockaddr *);
68static void in_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t); 69static void in_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t);
69 70
70static struct afswtch af = { 71static struct afswtch af = {
71 .af_name = "inet", .af_af = AF_INET, .af_status = in_status, 72 .af_name = "inet", .af_af = AF_INET, .af_status = in_status,
72 .af_addr_commit = in_commit_address, 73 .af_addr_commit = in_commit_address,
73 .af_addr_tentative = in_addr_tentative, 74 .af_addr_tentative = in_addr_tentative,
74 .af_addr_tentative_or_detached = in_addr_tentative_or_detached 75 .af_addr_tentative_or_detached = in_addr_tentative_or_detached
75}; 76};
76 77
 78static in_addr_t
 79in_netmask(struct sockaddr *sa)
 80{
 81 struct sockaddr_in sin;
 82
 83 memset(&sin, 0, sizeof(sin));
 84 memcpy(&sin, sa, sa->sa_len);
 85 return ntohl(sin.sin_addr.s_addr);
 86}
 87
77static int 88static int
78in_prefixlen(struct sockaddr *sa) 89in_prefixlen(struct sockaddr *sa)
79{ 90{
80 struct sockaddr_in sin; 
81 in_addr_t mask; 91 in_addr_t mask;
82 int cidr; 92 int cidr;
83 93
84 memset(&sin, 0, sizeof(sin)); 94 mask = in_netmask(sa);
85 memcpy(&sin, sa, sa->sa_len); 95 if (mask == 0) /* mask 0 ==> /0 */
86 mask = ntohl(sin.sin_addr.s_addr); 96 return 0;
87 cidr = 33 - ffs(mask); 97
 98 cidr = 33 - ffs(mask); /* 33 - (1 .. 32) -> 32 .. 1 */
 99
 100 if (cidr < 32) { /* more than 1 bit in mask */
 101 /* check for non-contig netmask */
 102 if ((mask ^ (((1 << cidr) - 1) << (32 - cidr))) != 0)
 103 return -1; /* noncontig, no pfxlen */
 104 }
 105
88 return cidr; 106 return cidr;
89} 107}
90 108
91static void 109static void
92in_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) 110in_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv)
93{ 111{
94 char hbuf[NI_MAXHOST]; 112 char hbuf[NI_MAXHOST];
95 const int niflag = Nflag ? 0 : NI_NUMERICHOST; 113 const int niflag = Nflag ? 0 : NI_NUMERICHOST;
 114 int pfxlen;
96 char fbuf[1024]; 115 char fbuf[1024];
97 116
98 if (lflag) 117 if (lflag)
99 return; 118 return;
100 119
101 if (getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len, 120 if (getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
102 hbuf, sizeof(hbuf), NULL, 0, niflag)) 121 hbuf, sizeof(hbuf), NULL, 0, niflag))
103 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ 122 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
104 printf("\tinet %s", hbuf); 123 printf("\tinet %s", hbuf);
105 printf("/%d", in_prefixlen(ifa->ifa_netmask)); 124 pfxlen = in_prefixlen(ifa->ifa_netmask);
 125 if (pfxlen >= 0)
 126 printf("/%d", pfxlen);
106 127
107 if (ifa->ifa_flags & IFF_POINTOPOINT) { 128 if (ifa->ifa_flags & IFF_POINTOPOINT) {
108 if (getnameinfo(ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_len, 129 if (getnameinfo(ifa->ifa_dstaddr, ifa->ifa_dstaddr->sa_len,
109 hbuf, sizeof(hbuf), NULL, 0, niflag)) 130 hbuf, sizeof(hbuf), NULL, 0, niflag))
110 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ 131 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
111 printf(" -> %s", hbuf); 132 printf(" -> %s", hbuf);
112 } 133 }
113 134
 135 if (pfxlen < 0)
 136 printf(" netmask %#x", in_netmask(ifa->ifa_netmask));
 137
114 if (ifa->ifa_flags & IFF_BROADCAST) { 138 if (ifa->ifa_flags & IFF_BROADCAST) {
115 if (getnameinfo(ifa->ifa_broadaddr, ifa->ifa_broadaddr->sa_len, 139 if (getnameinfo(ifa->ifa_broadaddr, ifa->ifa_broadaddr->sa_len,
116 hbuf, sizeof(hbuf), NULL, 0, niflag)) 140 hbuf, sizeof(hbuf), NULL, 0, niflag))
117 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */ 141 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
118 printf(" broadcast %s", hbuf); 142 printf(" broadcast %s", hbuf);
119 } 143 }
120 144
121 (void)snprintb(fbuf, sizeof(fbuf), IN_IFFBITS, ifa->ifa_addrflags); 145 (void)snprintb(fbuf, sizeof(fbuf), IN_IFFBITS, ifa->ifa_addrflags);
122 printf(" flags %s", fbuf); 146 printf(" flags %s", fbuf);
123} 147}
124 148
125static void 149static void
126in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force) 150in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)