1 /* $OpenBSD: addrmatch.c,v 1.9 2014/01/19 11:21:51 dtucker Exp $ */
4 * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
43 } xa
; /* 128-bit address */
44 u_int32_t scope_id
; /* iface scope id for v6 */
47 #define addr8 xa.addr8
48 #define addr32 xa.addr32
52 addr_unicast_masklen(int af
)
65 masklen_valid(int af
, u_int masklen
)
69 return masklen
<= 32 ? 0 : -1;
71 return masklen
<= 128 ? 0 : -1;
78 * Convert struct sockaddr to struct xaddr
79 * Returns 0 on success, -1 on failure.
82 addr_sa_to_xaddr(struct sockaddr
*sa
, socklen_t slen
, struct xaddr
*xa
)
84 struct sockaddr_in
*in4
= (struct sockaddr_in
*)sa
;
85 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)sa
;
87 memset(xa
, '\0', sizeof(*xa
));
89 switch (sa
->sa_family
) {
91 if (slen
< (socklen_t
)sizeof(*in4
))
94 memcpy(&xa
->v4
, &in4
->sin_addr
, sizeof(xa
->v4
));
97 if (slen
< (socklen_t
)sizeof(*in6
))
100 memcpy(&xa
->v6
, &in6
->sin6_addr
, sizeof(xa
->v6
));
101 #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
102 xa
->scope_id
= in6
->sin6_scope_id
;
113 * Calculate a netmask of length 'l' for address family 'af' and
115 * Returns 0 on success, -1 on failure.
118 addr_netmask(int af
, u_int l
, struct xaddr
*n
)
122 if (masklen_valid(af
, l
) != 0 || n
== NULL
)
125 memset(n
, '\0', sizeof(*n
));
131 n
->v4
.s_addr
= htonl((0xffffffff << (32 - l
)) & 0xffffffff);
135 for (i
= 0; i
< 4 && l
>= 32; i
++, l
-= 32)
136 n
->addr32
[i
] = 0xffffffffU
;
138 n
->addr32
[i
] = htonl((0xffffffff << (32 - l
)) &
147 * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
148 * Returns 0 on success, -1 on failure.
151 addr_and(struct xaddr
*dst
, const struct xaddr
*a
, const struct xaddr
*b
)
155 if (dst
== NULL
|| a
== NULL
|| b
== NULL
|| a
->af
!= b
->af
)
158 memcpy(dst
, a
, sizeof(*dst
));
161 dst
->v4
.s_addr
&= b
->v4
.s_addr
;
164 dst
->scope_id
= a
->scope_id
;
165 for (i
= 0; i
< 4; i
++)
166 dst
->addr32
[i
] &= b
->addr32
[i
];
174 * Compare addresses 'a' and 'b'
175 * Return 0 if addresses are identical, -1 if (a < b) or 1 if (a > b)
178 addr_cmp(const struct xaddr
*a
, const struct xaddr
*b
)
183 return a
->af
== AF_INET6
? 1 : -1;
187 if (a
->v4
.s_addr
== b
->v4
.s_addr
)
189 return ntohl(a
->v4
.s_addr
) > ntohl(b
->v4
.s_addr
) ? 1 : -1;
191 for (i
= 0; i
< 16; i
++)
192 if (a
->addr8
[i
] - b
->addr8
[i
] != 0)
193 return a
->addr8
[i
] > b
->addr8
[i
] ? 1 : -1;
194 if (a
->scope_id
== b
->scope_id
)
196 return a
->scope_id
> b
->scope_id
? 1 : -1;
203 * Parse string address 'p' into 'n'
204 * Returns 0 on success, -1 on failure.
207 addr_pton(const char *p
, struct xaddr
*n
)
209 struct addrinfo hints
, *ai
;
211 memset(&hints
, '\0', sizeof(hints
));
212 hints
.ai_flags
= AI_NUMERICHOST
;
214 if (p
== NULL
|| getaddrinfo(p
, NULL
, &hints
, &ai
) != 0)
217 if (ai
== NULL
|| ai
->ai_addr
== NULL
)
221 addr_sa_to_xaddr(ai
->ai_addr
, ai
->ai_addrlen
, n
) == -1) {
231 * Perform bitwise negation of address
232 * Returns 0 on success, -1 on failure.
235 addr_invert(struct xaddr
*n
)
244 n
->v4
.s_addr
= ~n
->v4
.s_addr
;
247 for (i
= 0; i
< 4; i
++)
248 n
->addr32
[i
] = ~n
->addr32
[i
];
256 * Calculate a netmask of length 'l' for address family 'af' and
258 * Returns 0 on success, -1 on failure.
261 addr_hostmask(int af
, u_int l
, struct xaddr
*n
)
263 if (addr_netmask(af
, l
, n
) == -1 || addr_invert(n
) == -1)
269 * Test whether address 'a' is all zeros (i.e. 0.0.0.0 or ::)
270 * Returns 0 on if address is all-zeros, -1 if not all zeros or on failure.
273 addr_is_all0s(const struct xaddr
*a
)
279 return (a
->v4
.s_addr
== 0 ? 0 : -1);
281 for (i
= 0; i
< 4; i
++)
282 if (a
->addr32
[i
] != 0)
291 * Test whether host portion of address 'a', as determined by 'masklen'
293 * Returns 0 on if host portion of address is all-zeros,
294 * -1 if not all zeros or on failure.
297 addr_host_is_all0s(const struct xaddr
*a
, u_int masklen
)
299 struct xaddr tmp_addr
, tmp_mask
, tmp_result
;
301 memcpy(&tmp_addr
, a
, sizeof(tmp_addr
));
302 if (addr_hostmask(a
->af
, masklen
, &tmp_mask
) == -1)
304 if (addr_and(&tmp_result
, &tmp_addr
, &tmp_mask
) == -1)
306 return (addr_is_all0s(&tmp_result
));
310 * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
311 * Return -1 on parse error, -2 on inconsistency or 0 on success.
314 addr_pton_cidr(const char *p
, struct xaddr
*n
, u_int
*l
)
317 long unsigned int masklen
= 999;
318 char addrbuf
[64], *mp
, *cp
;
320 /* Don't modify argument */
321 if (p
== NULL
|| strlcpy(addrbuf
, p
, sizeof(addrbuf
)) >= sizeof(addrbuf
))
324 if ((mp
= strchr(addrbuf
, '/')) != NULL
) {
327 masklen
= strtoul(mp
, &cp
, 10);
328 if (*mp
== '\0' || *cp
!= '\0' || masklen
> 128)
332 if (addr_pton(addrbuf
, &tmp
) == -1)
336 masklen
= addr_unicast_masklen(tmp
.af
);
337 if (masklen_valid(tmp
.af
, masklen
) == -1)
339 if (addr_host_is_all0s(&tmp
, masklen
) != 0)
343 memcpy(n
, &tmp
, sizeof(*n
));
351 addr_netmatch(const struct xaddr
*host
, const struct xaddr
*net
, u_int masklen
)
353 struct xaddr tmp_mask
, tmp_result
;
355 if (host
->af
!= net
->af
)
358 if (addr_netmask(host
->af
, masklen
, &tmp_mask
) == -1)
360 if (addr_and(&tmp_result
, host
, &tmp_mask
) == -1)
362 return addr_cmp(&tmp_result
, net
);
366 * Match "addr" against list pattern list "_list", which may contain a
367 * mix of CIDR addresses and old-school wildcards.
369 * If addr is NULL, then no matching is performed, but _list is parsed
370 * and checked for well-formedness.
372 * Returns 1 on match found (never returned when addr == NULL).
373 * Returns 0 on if no match found, or no errors found when addr == NULL.
374 * Returns -1 on negated match found (never returned when addr == NULL).
375 * Returns -2 on invalid list entry.
378 addr_match_list(const char *addr
, const char *_list
)
381 struct xaddr try_addr
, match_addr
;
385 if (addr
!= NULL
&& addr_pton(addr
, &try_addr
) != 0) {
386 debug2("%s: couldn't parse address %.100s", __func__
, addr
);
389 if ((o
= list
= strdup(_list
)) == NULL
)
391 while ((cp
= strsep(&list
, ",")) != NULL
) {
399 /* Prefer CIDR address matching */
400 r
= addr_pton_cidr(cp
, &match_addr
, &masklen
);
402 error("Inconsistent mask length for "
403 "network \"%.100s\"", cp
);
407 if (addr
!= NULL
&& addr_netmatch(&try_addr
,
408 &match_addr
, masklen
) == 0) {
418 /* If CIDR parse failed, try wildcard string match */
419 if (addr
!= NULL
&& match_pattern(addr
, cp
) == 1)
429 * Match "addr" against list CIDR list "_list". Lexical wildcards and
430 * negation are not supported. If "addr" == NULL, will verify structure
433 * Returns 1 on match found (never returned when addr == NULL).
434 * Returns 0 on if no match found, or no errors found when addr == NULL.
435 * Returns -1 on error
438 addr_match_cidr_list(const char *addr
, const char *_list
)
441 struct xaddr try_addr
, match_addr
;
445 if (addr
!= NULL
&& addr_pton(addr
, &try_addr
) != 0) {
446 debug2("%s: couldn't parse address %.100s", __func__
, addr
);
449 if ((o
= list
= strdup(_list
)) == NULL
)
451 while ((cp
= strsep(&list
, ",")) != NULL
) {
453 error("%s: empty entry in list \"%.100s\"",
460 * NB. This function is called in pre-auth with untrusted data,
461 * so be extra paranoid about junk reaching getaddrino (via
465 /* Stop junk from reaching getaddrinfo. +3 is for masklen */
466 if (strlen(cp
) > INET6_ADDRSTRLEN
+ 3) {
467 error("%s: list entry \"%.100s\" too long",
472 #define VALID_CIDR_CHARS "0123456789abcdefABCDEF.:/"
473 if (strspn(cp
, VALID_CIDR_CHARS
) != strlen(cp
)) {
474 error("%s: list entry \"%.100s\" contains invalid "
475 "characters", __func__
, cp
);
479 /* Prefer CIDR address matching */
480 r
= addr_pton_cidr(cp
, &match_addr
, &masklen
);
482 error("Invalid network entry \"%.100s\"", cp
);
485 } else if (r
== -2) {
486 error("Inconsistent mask length for "
487 "network \"%.100s\"", cp
);
490 } else if (r
== 0 && addr
!= NULL
) {
491 if (addr_netmatch(&try_addr
, &match_addr
,