1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2016 Nexenta Systems, Inc.
*/
/*
* inet_matchaddr
*
* Match IPv4 or IPv6 address provided in sa (sockaddr_in/sockaddr_in6)
* against standard text representation specified in name:
*
* IPv4:
* IPv4
* IPv4/netmask
*
* IPv6:
* [IPv6]
* [IPv6]/prefix
*
* Return values:
*
* 0 mismatch
* 1 match
* -1 error occured, caller should check errno:
* EINVAL access list entry is invalid
* ENOMEM failed to allocate memory
*/
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
int
inet_matchaddr(const void *sa, const char *name)
{
int ret = -1;
char *lname, *mp, *p;
char *ep;
int serrno = errno;
uint32_t claddr4 = 0;
if ((p = lname = strdup(name)) == NULL) {
errno = ENOMEM;
return (-1);
}
if ((mp = strchr(p, '/')) != NULL)
*mp++ = '\0';
switch (((struct sockaddr_in *)sa)->sin_family) {
case AF_INET6: {
char *pp;
ipaddr_t ipaddr4;
struct in6_addr hcaddr6;
struct in6_addr *claddr6 =
&((struct sockaddr_in6 *)sa)->sin6_addr;
if (!IN6_IS_ADDR_V4MAPPED(claddr6)) {
/* IPv6 address */
if (*p != '[') {
errno = EINVAL;
break;
}
p++;
if ((pp = strchr(p, ']')) == NULL ||
(mp != NULL && pp != mp - 2) ||
(mp == NULL && *(pp + 1) != '\0')) {
errno = EINVAL;
break;
}
*pp = '\0';
if (inet_pton(AF_INET6, p, &hcaddr6) != 1) {
errno = EINVAL;
break;
}
if (mp != NULL) {
/* Match only first prefix bits */
long prefix6;
errno = 0;
prefix6 = strtol(mp, &ep, 10);
if (errno != 0 || prefix6 < 0 ||
prefix6 > 128 || *ep != '\0') {
errno = EINVAL;
break;
}
ret = IN6_ARE_PREFIXEDADDR_EQUAL(claddr6,
&hcaddr6, prefix6) ? 1 : 0;
break;
} else {
/* No prefix, exact match */
ret = IN6_ARE_ADDR_EQUAL(claddr6,
&hcaddr6) ? 1 : 0;
break;
}
} else {
/* IPv4-mapped IPv6 address, fallthrough to IPv4 */
IN6_V4MAPPED_TO_IPADDR(claddr6, ipaddr4);
claddr4 = ntohl(ipaddr4);
}
}
/*FALLTHROUGH*/
case AF_INET: {
int i;
uint32_t hcaddr4 = 0, mask4;
if (claddr4 == 0) {
claddr4 = ntohl(
((struct sockaddr_in *)sa)->sin_addr.s_addr);
}
for (i = 0; i < 4; i++) {
long qaddr4;
errno = 0;
qaddr4 = strtol(p, &ep, 10);
if (errno != 0 || qaddr4 < 0 || qaddr4 > 255 ||
(*ep != '.' && *ep != '\0')) {
errno = EINVAL;
break;
}
hcaddr4 |= qaddr4 << ((3 - i) * 8);
if (*ep == '\0')
break;
p = ep + 1;
}
if (errno != 0)
break;
if (mp != NULL) {
/* Mask is specified explicitly */
long mb;
errno = 0;
mb = strtol(mp, &ep, 10);
if (errno != 0 || mb < 0 || mb > 32 || *ep != '\0') {
errno = EINVAL;
break;
}
mask4 = mb ? ~0 << ((sizeof (struct in_addr) * NBBY)
- mb) : 0;
hcaddr4 &= mask4;
} else {
/*
* Use old-fashioned implicit netmasking by checking
* for lower-end zeroes. On the off chance we don't
* match any well-known prefixes, return an exact-
* match prefix which is misleadingly labelled as
* IN_CLASSE_NET.
*/
if ((hcaddr4 & IN_CLASSA_HOST) == 0)
mask4 = IN_CLASSA_NET;
else if ((hcaddr4 & IN_CLASSB_HOST) == 0)
mask4 = IN_CLASSB_NET;
else if ((hcaddr4 & IN_CLASSC_HOST) == 0)
mask4 = IN_CLASSC_NET;
else
mask4 = IN_CLASSE_NET;
}
ret = ((claddr4 & mask4) == hcaddr4) ? 1 : 0;
break;
}
}
free(lname);
if (ret != -1)
errno = serrno;
return (ret);
}
|