2 /* $OpenBSD: addrmatch.c,v 1.4 2008/12/10 03:55:20 stevesk Exp $ */
5 * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 __RCSID("$NetBSD: addrmatch.c,v 1.2 2009/02/16 20:53:54 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
44 } xa
; /* 128-bit address */
45 u_int32_t scope_id
; /* iface scope id for v6 */
48 #define addr8 xa.addr8
49 #define addr32 xa.addr32
53 addr_unicast_masklen(int af
)
66 masklen_valid(int af
, u_int masklen
)
70 return masklen
<= 32 ? 0 : -1;
72 return masklen
<= 128 ? 0 : -1;
79 * Convert struct sockaddr to struct xaddr
80 * Returns 0 on success, -1 on failure.
83 addr_sa_to_xaddr(struct sockaddr
*sa
, socklen_t slen
, struct xaddr
*xa
)
85 struct sockaddr_in
*in4
= (struct sockaddr_in
*)sa
;
86 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)sa
;
88 memset(xa
, '\0', sizeof(*xa
));
90 switch (sa
->sa_family
) {
92 if (slen
< sizeof(*in4
))
95 memcpy(&xa
->v4
, &in4
->sin_addr
, sizeof(xa
->v4
));
98 if (slen
< sizeof(*in6
))
101 memcpy(&xa
->v6
, &in6
->sin6_addr
, sizeof(xa
->v6
));
102 xa
->scope_id
= in6
->sin6_scope_id
;
112 * Calculate a netmask of length 'l' for address family 'af' and
114 * Returns 0 on success, -1 on failure.
117 addr_netmask(int af
, u_int l
, struct xaddr
*n
)
121 if (masklen_valid(af
, l
) != 0 || n
== NULL
)
124 memset(n
, '\0', sizeof(*n
));
128 n
->v4
.s_addr
= htonl((0xffffffff << (32 - l
)) & 0xffffffff);
132 for (i
= 0; i
< 4 && l
>= 32; i
++, l
-= 32)
133 n
->addr32
[i
] = 0xffffffffU
;
135 n
->addr32
[i
] = htonl((0xffffffff << (32 - l
)) &
144 * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
145 * Returns 0 on success, -1 on failure.
148 addr_and(struct xaddr
*dst
, const struct xaddr
*a
, const struct xaddr
*b
)
152 if (dst
== NULL
|| a
== NULL
|| b
== NULL
|| a
->af
!= b
->af
)
155 memcpy(dst
, a
, sizeof(*dst
));
158 dst
->v4
.s_addr
&= b
->v4
.s_addr
;
161 dst
->scope_id
= a
->scope_id
;
162 for (i
= 0; i
< 4; i
++)
163 dst
->addr32
[i
] &= b
->addr32
[i
];
171 * Compare addresses 'a' and 'b'
172 * Return 0 if addresses are identical, -1 if (a < b) or 1 if (a > b)
175 addr_cmp(const struct xaddr
*a
, const struct xaddr
*b
)
180 return a
->af
== AF_INET6
? 1 : -1;
184 if (a
->v4
.s_addr
== b
->v4
.s_addr
)
186 return ntohl(a
->v4
.s_addr
) > ntohl(b
->v4
.s_addr
) ? 1 : -1;
188 for (i
= 0; i
< 16; i
++)
189 if (a
->addr8
[i
] - b
->addr8
[i
] != 0)
190 return a
->addr8
[i
] > b
->addr8
[i
] ? 1 : -1;
191 if (a
->scope_id
== b
->scope_id
)
193 return a
->scope_id
> b
->scope_id
? 1 : -1;
200 * Parse string address 'p' into 'n'
201 * Returns 0 on success, -1 on failure.
204 addr_pton(const char *p
, struct xaddr
*n
)
206 struct addrinfo hints
, *ai
;
208 memset(&hints
, '\0', sizeof(hints
));
209 hints
.ai_flags
= AI_NUMERICHOST
;
211 if (p
== NULL
|| getaddrinfo(p
, NULL
, &hints
, &ai
) != 0)
214 if (ai
== NULL
|| ai
->ai_addr
== NULL
)
218 addr_sa_to_xaddr(ai
->ai_addr
, ai
->ai_addrlen
, n
) == -1) {
228 * Perform bitwise negation of address
229 * Returns 0 on success, -1 on failure.
232 addr_invert(struct xaddr
*n
)
241 n
->v4
.s_addr
= ~n
->v4
.s_addr
;
244 for (i
= 0; i
< 4; i
++)
245 n
->addr32
[i
] = ~n
->addr32
[i
];
253 * Calculate a netmask of length 'l' for address family 'af' and
255 * Returns 0 on success, -1 on failure.
258 addr_hostmask(int af
, u_int l
, struct xaddr
*n
)
260 if (addr_netmask(af
, l
, n
) == -1 || addr_invert(n
) == -1)
266 * Test whether address 'a' is all zeros (i.e. 0.0.0.0 or ::)
267 * Returns 0 on if address is all-zeros, -1 if not all zeros or on failure.
270 addr_is_all0s(const struct xaddr
*a
)
276 return (a
->v4
.s_addr
== 0 ? 0 : -1);
278 for (i
= 0; i
< 4; i
++)
279 if (a
->addr32
[i
] != 0)
288 * Test whether host portion of address 'a', as determined by 'masklen'
290 * Returns 0 on if host portion of address is all-zeros,
291 * -1 if not all zeros or on failure.
294 addr_host_is_all0s(const struct xaddr
*a
, u_int masklen
)
296 struct xaddr tmp_addr
, tmp_mask
, tmp_result
;
298 memcpy(&tmp_addr
, a
, sizeof(tmp_addr
));
299 if (addr_hostmask(a
->af
, masklen
, &tmp_mask
) == -1)
301 if (addr_and(&tmp_result
, &tmp_addr
, &tmp_mask
) == -1)
303 return (addr_is_all0s(&tmp_result
));
307 * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
308 * Return -1 on parse error, -2 on inconsistency or 0 on success.
311 addr_pton_cidr(const char *p
, struct xaddr
*n
, u_int
*l
)
314 long unsigned int masklen
= 999;
315 char addrbuf
[64], *mp
, *cp
;
317 /* Don't modify argument */
318 if (p
== NULL
|| strlcpy(addrbuf
, p
, sizeof(addrbuf
)) > sizeof(addrbuf
))
321 if ((mp
= strchr(addrbuf
, '/')) != NULL
) {
324 masklen
= strtoul(mp
, &cp
, 10);
325 if (*mp
== '\0' || *cp
!= '\0' || masklen
> 128)
329 if (addr_pton(addrbuf
, &tmp
) == -1)
333 masklen
= addr_unicast_masklen(tmp
.af
);
334 if (masklen_valid(tmp
.af
, masklen
) == -1)
336 if (addr_host_is_all0s(&tmp
, masklen
) != 0)
340 memcpy(n
, &tmp
, sizeof(*n
));
348 addr_netmatch(const struct xaddr
*host
, const struct xaddr
*net
, u_int masklen
)
350 struct xaddr tmp_mask
, tmp_result
;
352 if (host
->af
!= net
->af
)
355 if (addr_netmask(host
->af
, masklen
, &tmp_mask
) == -1)
357 if (addr_and(&tmp_result
, host
, &tmp_mask
) == -1)
359 return addr_cmp(&tmp_result
, net
);
363 * Match "addr" against list pattern list "_list", which may contain a
364 * mix of CIDR addresses and old-school wildcards.
366 * If addr is NULL, then no matching is performed, but _list is parsed
367 * and checked for well-formedness.
369 * Returns 1 on match found (never returned when addr == NULL).
370 * Returns 0 on if no match found, or no errors found when addr == NULL.
371 * Returns -1 on negated match found (never returned when addr == NULL).
372 * Returns -2 on invalid list entry.
375 addr_match_list(const char *addr
, const char *_list
)
378 struct xaddr try_addr
, match_addr
;
382 if (addr
!= NULL
&& addr_pton(addr
, &try_addr
) != 0) {
383 debug2("%s: couldn't parse address %.100s", __func__
, addr
);
386 if ((o
= list
= strdup(_list
)) == NULL
)
388 while ((cp
= strsep(&list
, ",")) != NULL
) {
396 /* Prefer CIDR address matching */
397 r
= addr_pton_cidr(cp
, &match_addr
, &masklen
);
399 error("Inconsistent mask length for "
400 "network \"%.100s\"", cp
);
404 if (addr
!= NULL
&& addr_netmatch(&try_addr
,
405 &match_addr
, masklen
) == 0) {
415 /* If CIDR parse failed, try wildcard string match */
416 if (addr
!= NULL
&& match_pattern(addr
, cp
) == 1)