xref: /freebsd/crypto/openssh/addrmatch.c (revision d4af9e693f15f5155095f38c7650b24fe74ae351)
1d4af9e69SDag-Erling Smørgrav /*	$OpenBSD: addrmatch.c,v 1.3 2008/06/10 23:06:19 djm Exp $ */
2d4af9e69SDag-Erling Smørgrav 
3d4af9e69SDag-Erling Smørgrav /*
4d4af9e69SDag-Erling Smørgrav  * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
5d4af9e69SDag-Erling Smørgrav  *
6d4af9e69SDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
7d4af9e69SDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
8d4af9e69SDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
9d4af9e69SDag-Erling Smørgrav  *
10d4af9e69SDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11d4af9e69SDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12d4af9e69SDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13d4af9e69SDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14d4af9e69SDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15d4af9e69SDag-Erling Smørgrav  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16d4af9e69SDag-Erling Smørgrav  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17d4af9e69SDag-Erling Smørgrav  */
18d4af9e69SDag-Erling Smørgrav 
19d4af9e69SDag-Erling Smørgrav #include "includes.h"
20d4af9e69SDag-Erling Smørgrav 
21d4af9e69SDag-Erling Smørgrav #include <sys/types.h>
22d4af9e69SDag-Erling Smørgrav #include <sys/socket.h>
23d4af9e69SDag-Erling Smørgrav #include <netinet/in.h>
24d4af9e69SDag-Erling Smørgrav #include <arpa/inet.h>
25d4af9e69SDag-Erling Smørgrav 
26d4af9e69SDag-Erling Smørgrav #include <netdb.h>
27d4af9e69SDag-Erling Smørgrav #include <string.h>
28d4af9e69SDag-Erling Smørgrav #include <stdlib.h>
29d4af9e69SDag-Erling Smørgrav #include <stdio.h>
30d4af9e69SDag-Erling Smørgrav #include <stdarg.h>
31d4af9e69SDag-Erling Smørgrav 
32d4af9e69SDag-Erling Smørgrav #include "match.h"
33d4af9e69SDag-Erling Smørgrav #include "log.h"
34d4af9e69SDag-Erling Smørgrav 
35d4af9e69SDag-Erling Smørgrav struct xaddr {
36d4af9e69SDag-Erling Smørgrav 	sa_family_t	af;
37d4af9e69SDag-Erling Smørgrav 	union {
38d4af9e69SDag-Erling Smørgrav 		struct in_addr		v4;
39d4af9e69SDag-Erling Smørgrav 		struct in6_addr		v6;
40d4af9e69SDag-Erling Smørgrav 		u_int8_t		addr8[16];
41d4af9e69SDag-Erling Smørgrav 		u_int32_t		addr32[4];
42d4af9e69SDag-Erling Smørgrav 	} xa;		    /* 128-bit address */
43d4af9e69SDag-Erling Smørgrav 	u_int32_t	scope_id;	/* iface scope id for v6 */
44d4af9e69SDag-Erling Smørgrav #define v4	xa.v4
45d4af9e69SDag-Erling Smørgrav #define v6	xa.v6
46d4af9e69SDag-Erling Smørgrav #define addr8	xa.addr8
47d4af9e69SDag-Erling Smørgrav #define addr32	xa.addr32
48d4af9e69SDag-Erling Smørgrav };
49d4af9e69SDag-Erling Smørgrav 
50d4af9e69SDag-Erling Smørgrav static int
51d4af9e69SDag-Erling Smørgrav addr_unicast_masklen(int af)
52d4af9e69SDag-Erling Smørgrav {
53d4af9e69SDag-Erling Smørgrav 	switch (af) {
54d4af9e69SDag-Erling Smørgrav 	case AF_INET:
55d4af9e69SDag-Erling Smørgrav 		return 32;
56d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
57d4af9e69SDag-Erling Smørgrav 		return 128;
58d4af9e69SDag-Erling Smørgrav 	default:
59d4af9e69SDag-Erling Smørgrav 		return -1;
60d4af9e69SDag-Erling Smørgrav 	}
61d4af9e69SDag-Erling Smørgrav }
62d4af9e69SDag-Erling Smørgrav 
63d4af9e69SDag-Erling Smørgrav static inline int
64d4af9e69SDag-Erling Smørgrav masklen_valid(int af, u_int masklen)
65d4af9e69SDag-Erling Smørgrav {
66d4af9e69SDag-Erling Smørgrav 	switch (af) {
67d4af9e69SDag-Erling Smørgrav 	case AF_INET:
68d4af9e69SDag-Erling Smørgrav 		return masklen <= 32 ? 0 : -1;
69d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
70d4af9e69SDag-Erling Smørgrav 		return masklen <= 128 ? 0 : -1;
71d4af9e69SDag-Erling Smørgrav 	default:
72d4af9e69SDag-Erling Smørgrav 		return -1;
73d4af9e69SDag-Erling Smørgrav 	}
74d4af9e69SDag-Erling Smørgrav }
75d4af9e69SDag-Erling Smørgrav 
76d4af9e69SDag-Erling Smørgrav /*
77d4af9e69SDag-Erling Smørgrav  * Convert struct sockaddr to struct xaddr
78d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
79d4af9e69SDag-Erling Smørgrav  */
80d4af9e69SDag-Erling Smørgrav static int
81d4af9e69SDag-Erling Smørgrav addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa)
82d4af9e69SDag-Erling Smørgrav {
83d4af9e69SDag-Erling Smørgrav 	struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
84d4af9e69SDag-Erling Smørgrav 	struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
85d4af9e69SDag-Erling Smørgrav 
86d4af9e69SDag-Erling Smørgrav 	memset(xa, '\0', sizeof(*xa));
87d4af9e69SDag-Erling Smørgrav 
88d4af9e69SDag-Erling Smørgrav 	switch (sa->sa_family) {
89d4af9e69SDag-Erling Smørgrav 	case AF_INET:
90d4af9e69SDag-Erling Smørgrav 		if (slen < sizeof(*in4))
91d4af9e69SDag-Erling Smørgrav 			return -1;
92d4af9e69SDag-Erling Smørgrav 		xa->af = AF_INET;
93d4af9e69SDag-Erling Smørgrav 		memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4));
94d4af9e69SDag-Erling Smørgrav 		break;
95d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
96d4af9e69SDag-Erling Smørgrav 		if (slen < sizeof(*in6))
97d4af9e69SDag-Erling Smørgrav 			return -1;
98d4af9e69SDag-Erling Smørgrav 		xa->af = AF_INET6;
99d4af9e69SDag-Erling Smørgrav 		memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6));
100d4af9e69SDag-Erling Smørgrav 		xa->scope_id = in6->sin6_scope_id;
101d4af9e69SDag-Erling Smørgrav 		break;
102d4af9e69SDag-Erling Smørgrav 	default:
103d4af9e69SDag-Erling Smørgrav 		return -1;
104d4af9e69SDag-Erling Smørgrav 	}
105d4af9e69SDag-Erling Smørgrav 
106d4af9e69SDag-Erling Smørgrav 	return 0;
107d4af9e69SDag-Erling Smørgrav }
108d4af9e69SDag-Erling Smørgrav 
109d4af9e69SDag-Erling Smørgrav /*
110d4af9e69SDag-Erling Smørgrav  * Calculate a netmask of length 'l' for address family 'af' and
111d4af9e69SDag-Erling Smørgrav  * store it in 'n'.
112d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
113d4af9e69SDag-Erling Smørgrav  */
114d4af9e69SDag-Erling Smørgrav static int
115d4af9e69SDag-Erling Smørgrav addr_netmask(int af, u_int l, struct xaddr *n)
116d4af9e69SDag-Erling Smørgrav {
117d4af9e69SDag-Erling Smørgrav 	int i;
118d4af9e69SDag-Erling Smørgrav 
119d4af9e69SDag-Erling Smørgrav 	if (masklen_valid(af, l) != 0 || n == NULL)
120d4af9e69SDag-Erling Smørgrav 		return -1;
121d4af9e69SDag-Erling Smørgrav 
122d4af9e69SDag-Erling Smørgrav 	memset(n, '\0', sizeof(*n));
123d4af9e69SDag-Erling Smørgrav 	switch (af) {
124d4af9e69SDag-Erling Smørgrav 	case AF_INET:
125d4af9e69SDag-Erling Smørgrav 		n->af = AF_INET;
126d4af9e69SDag-Erling Smørgrav 		n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff);
127d4af9e69SDag-Erling Smørgrav 		return 0;
128d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
129d4af9e69SDag-Erling Smørgrav 		n->af = AF_INET6;
130d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4 && l >= 32; i++, l -= 32)
131d4af9e69SDag-Erling Smørgrav 			n->addr32[i] = 0xffffffffU;
132d4af9e69SDag-Erling Smørgrav 		if (i < 4 && l != 0)
133d4af9e69SDag-Erling Smørgrav 			n->addr32[i] = htonl((0xffffffff << (32 - l)) &
134d4af9e69SDag-Erling Smørgrav 			    0xffffffff);
135d4af9e69SDag-Erling Smørgrav 		return 0;
136d4af9e69SDag-Erling Smørgrav 	default:
137d4af9e69SDag-Erling Smørgrav 		return -1;
138d4af9e69SDag-Erling Smørgrav 	}
139d4af9e69SDag-Erling Smørgrav }
140d4af9e69SDag-Erling Smørgrav 
141d4af9e69SDag-Erling Smørgrav /*
142d4af9e69SDag-Erling Smørgrav  * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
143d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
144d4af9e69SDag-Erling Smørgrav  */
145d4af9e69SDag-Erling Smørgrav static int
146d4af9e69SDag-Erling Smørgrav addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
147d4af9e69SDag-Erling Smørgrav {
148d4af9e69SDag-Erling Smørgrav 	int i;
149d4af9e69SDag-Erling Smørgrav 
150d4af9e69SDag-Erling Smørgrav 	if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
151d4af9e69SDag-Erling Smørgrav 		return -1;
152d4af9e69SDag-Erling Smørgrav 
153d4af9e69SDag-Erling Smørgrav 	memcpy(dst, a, sizeof(*dst));
154d4af9e69SDag-Erling Smørgrav 	switch (a->af) {
155d4af9e69SDag-Erling Smørgrav 	case AF_INET:
156d4af9e69SDag-Erling Smørgrav 		dst->v4.s_addr &= b->v4.s_addr;
157d4af9e69SDag-Erling Smørgrav 		return 0;
158d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
159d4af9e69SDag-Erling Smørgrav 		dst->scope_id = a->scope_id;
160d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4; i++)
161d4af9e69SDag-Erling Smørgrav 			dst->addr32[i] &= b->addr32[i];
162d4af9e69SDag-Erling Smørgrav 		return 0;
163d4af9e69SDag-Erling Smørgrav 	default:
164d4af9e69SDag-Erling Smørgrav 		return -1;
165d4af9e69SDag-Erling Smørgrav 	}
166d4af9e69SDag-Erling Smørgrav }
167d4af9e69SDag-Erling Smørgrav 
168d4af9e69SDag-Erling Smørgrav /*
169d4af9e69SDag-Erling Smørgrav  * Compare addresses 'a' and 'b'
170d4af9e69SDag-Erling Smørgrav  * Return 0 if addresses are identical, -1 if (a < b) or 1 if (a > b)
171d4af9e69SDag-Erling Smørgrav  */
172d4af9e69SDag-Erling Smørgrav static int
173d4af9e69SDag-Erling Smørgrav addr_cmp(const struct xaddr *a, const struct xaddr *b)
174d4af9e69SDag-Erling Smørgrav {
175d4af9e69SDag-Erling Smørgrav 	int i;
176d4af9e69SDag-Erling Smørgrav 
177d4af9e69SDag-Erling Smørgrav 	if (a->af != b->af)
178d4af9e69SDag-Erling Smørgrav 		return a->af == AF_INET6 ? 1 : -1;
179d4af9e69SDag-Erling Smørgrav 
180d4af9e69SDag-Erling Smørgrav 	switch (a->af) {
181d4af9e69SDag-Erling Smørgrav 	case AF_INET:
182d4af9e69SDag-Erling Smørgrav 		if (a->v4.s_addr == b->v4.s_addr)
183d4af9e69SDag-Erling Smørgrav 			return 0;
184d4af9e69SDag-Erling Smørgrav 		return ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1;
185d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
186d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 16; i++)
187d4af9e69SDag-Erling Smørgrav 			if (a->addr8[i] - b->addr8[i] != 0)
188d4af9e69SDag-Erling Smørgrav 				return a->addr8[i] > b->addr8[i] ? 1 : -1;
189d4af9e69SDag-Erling Smørgrav 		if (a->scope_id == b->scope_id)
190d4af9e69SDag-Erling Smørgrav 			return 0;
191d4af9e69SDag-Erling Smørgrav 		return a->scope_id > b->scope_id ? 1 : -1;
192d4af9e69SDag-Erling Smørgrav 	default:
193d4af9e69SDag-Erling Smørgrav 		return -1;
194d4af9e69SDag-Erling Smørgrav 	}
195d4af9e69SDag-Erling Smørgrav }
196d4af9e69SDag-Erling Smørgrav 
197d4af9e69SDag-Erling Smørgrav /*
198d4af9e69SDag-Erling Smørgrav  * Parse string address 'p' into 'n'
199d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
200d4af9e69SDag-Erling Smørgrav  */
201d4af9e69SDag-Erling Smørgrav static int
202d4af9e69SDag-Erling Smørgrav addr_pton(const char *p, struct xaddr *n)
203d4af9e69SDag-Erling Smørgrav {
204d4af9e69SDag-Erling Smørgrav 	struct addrinfo hints, *ai;
205d4af9e69SDag-Erling Smørgrav 
206d4af9e69SDag-Erling Smørgrav 	memset(&hints, '\0', sizeof(hints));
207d4af9e69SDag-Erling Smørgrav 	hints.ai_flags = AI_NUMERICHOST;
208d4af9e69SDag-Erling Smørgrav 
209d4af9e69SDag-Erling Smørgrav 	if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
210d4af9e69SDag-Erling Smørgrav 		return -1;
211d4af9e69SDag-Erling Smørgrav 
212d4af9e69SDag-Erling Smørgrav 	if (ai == NULL || ai->ai_addr == NULL)
213d4af9e69SDag-Erling Smørgrav 		return -1;
214d4af9e69SDag-Erling Smørgrav 
215d4af9e69SDag-Erling Smørgrav 	if (n != NULL &&
216d4af9e69SDag-Erling Smørgrav 	    addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen, n) == -1) {
217d4af9e69SDag-Erling Smørgrav 		freeaddrinfo(ai);
218d4af9e69SDag-Erling Smørgrav 		return -1;
219d4af9e69SDag-Erling Smørgrav 	}
220d4af9e69SDag-Erling Smørgrav 
221d4af9e69SDag-Erling Smørgrav 	freeaddrinfo(ai);
222d4af9e69SDag-Erling Smørgrav 	return 0;
223d4af9e69SDag-Erling Smørgrav }
224d4af9e69SDag-Erling Smørgrav 
225d4af9e69SDag-Erling Smørgrav /*
226d4af9e69SDag-Erling Smørgrav  * Perform bitwise negation of address
227d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
228d4af9e69SDag-Erling Smørgrav  */
229d4af9e69SDag-Erling Smørgrav static int
230d4af9e69SDag-Erling Smørgrav addr_invert(struct xaddr *n)
231d4af9e69SDag-Erling Smørgrav {
232d4af9e69SDag-Erling Smørgrav 	int i;
233d4af9e69SDag-Erling Smørgrav 
234d4af9e69SDag-Erling Smørgrav 	if (n == NULL)
235d4af9e69SDag-Erling Smørgrav 		return (-1);
236d4af9e69SDag-Erling Smørgrav 
237d4af9e69SDag-Erling Smørgrav 	switch (n->af) {
238d4af9e69SDag-Erling Smørgrav 	case AF_INET:
239d4af9e69SDag-Erling Smørgrav 		n->v4.s_addr = ~n->v4.s_addr;
240d4af9e69SDag-Erling Smørgrav 		return (0);
241d4af9e69SDag-Erling Smørgrav 	case AF_INET6:
242d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4; i++)
243d4af9e69SDag-Erling Smørgrav 			n->addr32[i] = ~n->addr32[i];
244d4af9e69SDag-Erling Smørgrav 		return (0);
245d4af9e69SDag-Erling Smørgrav 	default:
246d4af9e69SDag-Erling Smørgrav 		return (-1);
247d4af9e69SDag-Erling Smørgrav 	}
248d4af9e69SDag-Erling Smørgrav }
249d4af9e69SDag-Erling Smørgrav 
250d4af9e69SDag-Erling Smørgrav /*
251d4af9e69SDag-Erling Smørgrav  * Calculate a netmask of length 'l' for address family 'af' and
252d4af9e69SDag-Erling Smørgrav  * store it in 'n'.
253d4af9e69SDag-Erling Smørgrav  * Returns 0 on success, -1 on failure.
254d4af9e69SDag-Erling Smørgrav  */
255d4af9e69SDag-Erling Smørgrav static int
256d4af9e69SDag-Erling Smørgrav addr_hostmask(int af, u_int l, struct xaddr *n)
257d4af9e69SDag-Erling Smørgrav {
258d4af9e69SDag-Erling Smørgrav 	if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1)
259d4af9e69SDag-Erling Smørgrav 		return (-1);
260d4af9e69SDag-Erling Smørgrav 	return (0);
261d4af9e69SDag-Erling Smørgrav }
262d4af9e69SDag-Erling Smørgrav 
263d4af9e69SDag-Erling Smørgrav /*
264d4af9e69SDag-Erling Smørgrav  * Test whether address 'a' is all zeros (i.e. 0.0.0.0 or ::)
265d4af9e69SDag-Erling Smørgrav  * Returns 0 on if address is all-zeros, -1 if not all zeros or on failure.
266d4af9e69SDag-Erling Smørgrav  */
267d4af9e69SDag-Erling Smørgrav static int
268d4af9e69SDag-Erling Smørgrav addr_is_all0s(const struct xaddr *a)
269d4af9e69SDag-Erling Smørgrav {
270d4af9e69SDag-Erling Smørgrav 	int i;
271d4af9e69SDag-Erling Smørgrav 
272d4af9e69SDag-Erling Smørgrav 	switch (a->af) {
273d4af9e69SDag-Erling Smørgrav 	case AF_INET:
274d4af9e69SDag-Erling Smørgrav 		return (a->v4.s_addr == 0 ? 0 : -1);
275d4af9e69SDag-Erling Smørgrav 	case AF_INET6:;
276d4af9e69SDag-Erling Smørgrav 		for (i = 0; i < 4; i++)
277d4af9e69SDag-Erling Smørgrav 			if (a->addr32[i] != 0)
278d4af9e69SDag-Erling Smørgrav 				return (-1);
279d4af9e69SDag-Erling Smørgrav 		return (0);
280d4af9e69SDag-Erling Smørgrav 	default:
281d4af9e69SDag-Erling Smørgrav 		return (-1);
282d4af9e69SDag-Erling Smørgrav 	}
283d4af9e69SDag-Erling Smørgrav }
284d4af9e69SDag-Erling Smørgrav 
285d4af9e69SDag-Erling Smørgrav /*
286d4af9e69SDag-Erling Smørgrav  * Test whether host portion of address 'a', as determined by 'masklen'
287d4af9e69SDag-Erling Smørgrav  * is all zeros.
288d4af9e69SDag-Erling Smørgrav  * Returns 0 on if host portion of address is all-zeros,
289d4af9e69SDag-Erling Smørgrav  * -1 if not all zeros or on failure.
290d4af9e69SDag-Erling Smørgrav  */
291d4af9e69SDag-Erling Smørgrav static int
292d4af9e69SDag-Erling Smørgrav addr_host_is_all0s(const struct xaddr *a, u_int masklen)
293d4af9e69SDag-Erling Smørgrav {
294d4af9e69SDag-Erling Smørgrav 	struct xaddr tmp_addr, tmp_mask, tmp_result;
295d4af9e69SDag-Erling Smørgrav 
296d4af9e69SDag-Erling Smørgrav 	memcpy(&tmp_addr, a, sizeof(tmp_addr));
297d4af9e69SDag-Erling Smørgrav 	if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
298d4af9e69SDag-Erling Smørgrav 		return (-1);
299d4af9e69SDag-Erling Smørgrav 	if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1)
300d4af9e69SDag-Erling Smørgrav 		return (-1);
301d4af9e69SDag-Erling Smørgrav 	return (addr_is_all0s(&tmp_result));
302d4af9e69SDag-Erling Smørgrav }
303d4af9e69SDag-Erling Smørgrav 
304d4af9e69SDag-Erling Smørgrav /*
305d4af9e69SDag-Erling Smørgrav  * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
306d4af9e69SDag-Erling Smørgrav  * Return -1 on parse error, -2 on inconsistency or 0 on success.
307d4af9e69SDag-Erling Smørgrav  */
308d4af9e69SDag-Erling Smørgrav static int
309d4af9e69SDag-Erling Smørgrav addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
310d4af9e69SDag-Erling Smørgrav {
311d4af9e69SDag-Erling Smørgrav 	struct xaddr tmp;
312d4af9e69SDag-Erling Smørgrav 	long unsigned int masklen = 999;
313d4af9e69SDag-Erling Smørgrav 	char addrbuf[64], *mp, *cp;
314d4af9e69SDag-Erling Smørgrav 
315d4af9e69SDag-Erling Smørgrav 	/* Don't modify argument */
316d4af9e69SDag-Erling Smørgrav 	if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) > sizeof(addrbuf))
317d4af9e69SDag-Erling Smørgrav 		return -1;
318d4af9e69SDag-Erling Smørgrav 
319d4af9e69SDag-Erling Smørgrav 	if ((mp = strchr(addrbuf, '/')) != NULL) {
320d4af9e69SDag-Erling Smørgrav 		*mp = '\0';
321d4af9e69SDag-Erling Smørgrav 		mp++;
322d4af9e69SDag-Erling Smørgrav 		masklen = strtoul(mp, &cp, 10);
323d4af9e69SDag-Erling Smørgrav 		if (*mp == '\0' || *cp != '\0' || masklen > 128)
324d4af9e69SDag-Erling Smørgrav 			return -1;
325d4af9e69SDag-Erling Smørgrav 	}
326d4af9e69SDag-Erling Smørgrav 
327d4af9e69SDag-Erling Smørgrav 	if (addr_pton(addrbuf, &tmp) == -1)
328d4af9e69SDag-Erling Smørgrav 		return -1;
329d4af9e69SDag-Erling Smørgrav 
330d4af9e69SDag-Erling Smørgrav 	if (mp == NULL)
331d4af9e69SDag-Erling Smørgrav 		masklen = addr_unicast_masklen(tmp.af);
332d4af9e69SDag-Erling Smørgrav 	if (masklen_valid(tmp.af, masklen) == -1)
333d4af9e69SDag-Erling Smørgrav 		return -2;
334d4af9e69SDag-Erling Smørgrav 	if (addr_host_is_all0s(&tmp, masklen) != 0)
335d4af9e69SDag-Erling Smørgrav 		return -2;
336d4af9e69SDag-Erling Smørgrav 
337d4af9e69SDag-Erling Smørgrav 	if (n != NULL)
338d4af9e69SDag-Erling Smørgrav 		memcpy(n, &tmp, sizeof(*n));
339d4af9e69SDag-Erling Smørgrav 	if (l != NULL)
340d4af9e69SDag-Erling Smørgrav 		*l = masklen;
341d4af9e69SDag-Erling Smørgrav 
342d4af9e69SDag-Erling Smørgrav 	return 0;
343d4af9e69SDag-Erling Smørgrav }
344d4af9e69SDag-Erling Smørgrav 
345d4af9e69SDag-Erling Smørgrav static int
346d4af9e69SDag-Erling Smørgrav addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen)
347d4af9e69SDag-Erling Smørgrav {
348d4af9e69SDag-Erling Smørgrav 	struct xaddr tmp_mask, tmp_result;
349d4af9e69SDag-Erling Smørgrav 
350d4af9e69SDag-Erling Smørgrav 	if (host->af != net->af)
351d4af9e69SDag-Erling Smørgrav 		return -1;
352d4af9e69SDag-Erling Smørgrav 
353d4af9e69SDag-Erling Smørgrav 	if (addr_netmask(host->af, masklen, &tmp_mask) == -1)
354d4af9e69SDag-Erling Smørgrav 		return -1;
355d4af9e69SDag-Erling Smørgrav 	if (addr_and(&tmp_result, host, &tmp_mask) == -1)
356d4af9e69SDag-Erling Smørgrav 		return -1;
357d4af9e69SDag-Erling Smørgrav 	return addr_cmp(&tmp_result, net);
358d4af9e69SDag-Erling Smørgrav }
359d4af9e69SDag-Erling Smørgrav 
360d4af9e69SDag-Erling Smørgrav /*
361d4af9e69SDag-Erling Smørgrav  * Match "addr" against list pattern list "_list", which may contain a
362d4af9e69SDag-Erling Smørgrav  * mix of CIDR addresses and old-school wildcards.
363d4af9e69SDag-Erling Smørgrav  *
364d4af9e69SDag-Erling Smørgrav  * If addr is NULL, then no matching is performed, but _list is parsed
365d4af9e69SDag-Erling Smørgrav  * and checked for well-formedness.
366d4af9e69SDag-Erling Smørgrav  *
367d4af9e69SDag-Erling Smørgrav  * Returns 1 on match found (never returned when addr == NULL).
368d4af9e69SDag-Erling Smørgrav  * Returns 0 on if no match found, or no errors found when addr == NULL.
369d4af9e69SDag-Erling Smørgrav  * Returns -1 on negated match found (never returned when addr == NULL).
370d4af9e69SDag-Erling Smørgrav  * Returns -2 on invalid list entry.
371d4af9e69SDag-Erling Smørgrav  */
372d4af9e69SDag-Erling Smørgrav int
373d4af9e69SDag-Erling Smørgrav addr_match_list(const char *addr, const char *_list)
374d4af9e69SDag-Erling Smørgrav {
375d4af9e69SDag-Erling Smørgrav 	char *list, *cp, *o;
376d4af9e69SDag-Erling Smørgrav 	struct xaddr try_addr, match_addr;
377d4af9e69SDag-Erling Smørgrav 	u_int masklen, neg;
378d4af9e69SDag-Erling Smørgrav 	int ret = 0, r;
379d4af9e69SDag-Erling Smørgrav 
380d4af9e69SDag-Erling Smørgrav 	if (addr != NULL && addr_pton(addr, &try_addr) != 0) {
381d4af9e69SDag-Erling Smørgrav 		debug2("%s: couldn't parse address %.100s", __func__, addr);
382d4af9e69SDag-Erling Smørgrav 		return 0;
383d4af9e69SDag-Erling Smørgrav 	}
384d4af9e69SDag-Erling Smørgrav 	if ((o = list = strdup(_list)) == NULL)
385d4af9e69SDag-Erling Smørgrav 		return -1;
386d4af9e69SDag-Erling Smørgrav 	while ((cp = strsep(&list, ",")) != NULL) {
387d4af9e69SDag-Erling Smørgrav 		neg = *cp == '!';
388d4af9e69SDag-Erling Smørgrav 		if (neg)
389d4af9e69SDag-Erling Smørgrav 			cp++;
390d4af9e69SDag-Erling Smørgrav 		if (*cp == '\0') {
391d4af9e69SDag-Erling Smørgrav 			ret = -2;
392d4af9e69SDag-Erling Smørgrav 			break;
393d4af9e69SDag-Erling Smørgrav 		}
394d4af9e69SDag-Erling Smørgrav 		/* Prefer CIDR address matching */
395d4af9e69SDag-Erling Smørgrav 		r = addr_pton_cidr(cp, &match_addr, &masklen);
396d4af9e69SDag-Erling Smørgrav 		if (r == -2) {
397d4af9e69SDag-Erling Smørgrav 			error("Inconsistent mask length for "
398d4af9e69SDag-Erling Smørgrav 			    "network \"%.100s\"", cp);
399d4af9e69SDag-Erling Smørgrav 			ret = -2;
400d4af9e69SDag-Erling Smørgrav 			break;
401d4af9e69SDag-Erling Smørgrav 		} else if (r == 0) {
402d4af9e69SDag-Erling Smørgrav 			if (addr != NULL && addr_netmatch(&try_addr,
403d4af9e69SDag-Erling Smørgrav                            &match_addr, masklen) == 0) {
404d4af9e69SDag-Erling Smørgrav  foundit:
405d4af9e69SDag-Erling Smørgrav 				if (neg) {
406d4af9e69SDag-Erling Smørgrav 					ret = -1;
407d4af9e69SDag-Erling Smørgrav 					break;
408d4af9e69SDag-Erling Smørgrav 				}
409d4af9e69SDag-Erling Smørgrav 				ret = 1;
410d4af9e69SDag-Erling Smørgrav 			}
411d4af9e69SDag-Erling Smørgrav 			continue;
412d4af9e69SDag-Erling Smørgrav 		} else {
413d4af9e69SDag-Erling Smørgrav 			/* If CIDR parse failed, try wildcard string match */
414d4af9e69SDag-Erling Smørgrav 			if (addr != NULL && match_pattern(addr, cp) == 1)
415d4af9e69SDag-Erling Smørgrav 				goto foundit;
416d4af9e69SDag-Erling Smørgrav 		}
417d4af9e69SDag-Erling Smørgrav 	}
418d4af9e69SDag-Erling Smørgrav 	free(o);
419d4af9e69SDag-Erling Smørgrav 
420d4af9e69SDag-Erling Smørgrav 	return ret;
421d4af9e69SDag-Erling Smørgrav }
422