| @@ -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 | |
62 | static void in_constructor(void) __attribute__((constructor)); | | 62 | static void in_constructor(void) __attribute__((constructor)); |
63 | static void in_status(prop_dictionary_t, prop_dictionary_t, bool); | | 63 | static void in_status(prop_dictionary_t, prop_dictionary_t, bool); |
64 | static void in_commit_address(prop_dictionary_t, prop_dictionary_t); | | 64 | static void in_commit_address(prop_dictionary_t, prop_dictionary_t); |
65 | static bool in_addr_tentative(struct ifaddrs *); | | 65 | static bool in_addr_tentative(struct ifaddrs *); |
66 | static bool in_addr_tentative_or_detached(struct ifaddrs *); | | 66 | static bool in_addr_tentative_or_detached(struct ifaddrs *); |
| | | 67 | static in_addr_t in_netmask(struct sockaddr *);; |
67 | static int in_prefixlen(struct sockaddr *); | | 68 | static int in_prefixlen(struct sockaddr *); |
68 | static void in_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t); | | 69 | static void in_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t); |
69 | | | 70 | |
70 | static struct afswtch af = { | | 71 | static 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 | |
| | | 78 | static in_addr_t |
| | | 79 | in_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 | |
77 | static int | | 88 | static int |
78 | in_prefixlen(struct sockaddr *sa) | | 89 | in_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 | |
91 | static void | | 109 | static void |
92 | in_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv) | | 110 | in_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 | |
125 | static void | | 149 | static void |
126 | in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force) | | 150 | in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force) |