summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/nss/inet_matchaddr.c
blob: 566c1ae1ad6e57d387ef76cf4d35708d796a0e03 (plain)
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
/*
 * 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 2011 Nexenta Systems, Inc. All rights reserved.
 */

/*
 * 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
 */

#include <sys/socket.h>
#include <sys/types.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <ctype.h>
#include <err.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>


boolean_t
inet_matchaddr(const void *sa, const char *name)
{
	boolean_t ret = B_FALSE;
	char *lname, *mp, *p;
	uint32_t claddr4 = 0;

	if ((p = lname = strdup(name)) == NULL)
		err(1, "strdup");

	if ((mp = strchr(p, '/')) != NULL)
		*mp++ = '\0';

	switch (((struct sockaddr_in *)sa)->sin_family) {
	case AF_INET6: {
		char *pp;
		int prefix6;
		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 = strchr(p, '[')) == NULL)
				break;
			p++;

			if ((pp = strchr(p, ']')) == NULL)
				break;
			*pp = '\0';

			if (inet_pton(AF_INET6, p, &hcaddr6) != 1)
				break;

			if (mp != NULL) {
				/* Match only first prefix bits */
				if ((prefix6 = (int)strtol(mp,
				    (char **)NULL, 10)) == 0)
					break;
				ret = IN6_ARE_PREFIXEDADDR_EQUAL(claddr6,
				    &hcaddr6, prefix6);
				break;
			} else {
				/* No prefix, exact match */
				ret = IN6_ARE_ADDR_EQUAL(claddr6, &hcaddr6);
				break;
			}
		} else {
			/* IPv4-mapped IPv6 address, fallthrough to IPv4 */
			IN6_V4MAPPED_TO_IPADDR(claddr6, ipaddr4);
			claddr4 = ntohl(ipaddr4);
		}
		/*FALLTHROUGH*/
	}
	case AF_INET: {
		int bits, 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++) {
			hcaddr4 |= (int)strtol(p, (char **)NULL, 10) <<
			    ((3 - i) * 8);
			if ((p = strchr(p, '.')) == NULL)
				break;
			p++;
		}

		if (hcaddr4 == 0)
			break;

		if (mp != NULL) {
			/* Mask is specified explicitly */
			if ((bits = (int)strtol(mp, (char **)NULL, 10)) == 0)
				break;
			mask4 = bits ? ~0 << ((sizeof (struct in_addr) * NBBY)
			    - bits) : 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);
		break;
	}
	}

	free(lname);

	return (ret);
}