xref: /netbsd-src/external/bsd/iscsi/dist/src/lib/netmask.c (revision 5e01dafb5c1f3a68ee2f80136c3b6043ad89a0f3)
1*5e01dafbSagc /* $NetBSD: netmask.c,v 1.2 2009/06/30 02:44:52 agc Exp $ */
2b6364952Sagc 
3b6364952Sagc /*
4b6364952Sagc  * Copyright � 2006 Alistair Crooks.  All rights reserved.
5b6364952Sagc  *
6b6364952Sagc  * Redistribution and use in source and binary forms, with or without
7b6364952Sagc  * modification, are permitted provided that the following conditions
8b6364952Sagc  * are met:
9b6364952Sagc  * 1. Redistributions of source code must retain the above copyright
10b6364952Sagc  *    notice, this list of conditions and the following disclaimer.
11b6364952Sagc  * 2. Redistributions in binary form must reproduce the above copyright
12b6364952Sagc  *    notice, this list of conditions and the following disclaimer in the
13b6364952Sagc  *    documentation and/or other materials provided with the distribution.
14b6364952Sagc  * 3. The name of the author may not be used to endorse or promote
15b6364952Sagc  *    products derived from this software without specific prior written
16b6364952Sagc  *    permission.
17b6364952Sagc  *
18b6364952Sagc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19b6364952Sagc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20b6364952Sagc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b6364952Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22b6364952Sagc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b6364952Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24b6364952Sagc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b6364952Sagc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26b6364952Sagc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27b6364952Sagc  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28b6364952Sagc  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b6364952Sagc  */
30b6364952Sagc #include "config.h"
31b6364952Sagc 
32b6364952Sagc #include <sys/types.h>
33b6364952Sagc #include <sys/param.h>
34b6364952Sagc 
35b6364952Sagc #include <stdio.h>
36b6364952Sagc #include <stdlib.h>
37b6364952Sagc #include <string.h>
38b6364952Sagc #include <unistd.h>
39b6364952Sagc 
40b6364952Sagc #ifdef HAVE_NETINET_IN_H
41b6364952Sagc #include <netinet/in.h>
42b6364952Sagc #endif
43b6364952Sagc 
44b6364952Sagc #ifdef HAVE_ARPA_INET_H
45b6364952Sagc #include <arpa/inet.h>
46b6364952Sagc #endif
47b6364952Sagc 
48b6364952Sagc #include "iscsiutil.h"
49b6364952Sagc 
50b6364952Sagc enum {
51b6364952Sagc 	NETMASK_BUFFER_SIZE = 256
52b6364952Sagc };
53b6364952Sagc 
54b6364952Sagc /* this struct is used to define a magic netmask value */
55b6364952Sagc typedef struct magic_t {
56b6364952Sagc 	const char	*magic;	/* string to match */
57b6364952Sagc 	const char	*xform;	/* string to transform it into */
58b6364952Sagc } magic_t;
59b6364952Sagc 
60b6364952Sagc 
61b6364952Sagc static magic_t	magics[] = {
62b6364952Sagc 	{	"any",	"0/0"	},
63b6364952Sagc 	{	"all",	"0/0"	},
64b6364952Sagc 	{	"none",	"0/32"	},
65b6364952Sagc 	{	NULL,	NULL	},
66b6364952Sagc };
67b6364952Sagc 
68b6364952Sagc #ifndef ISCSI_HTONL
69b6364952Sagc #define ISCSI_HTONL(x)	htonl(x)
70b6364952Sagc #endif
71b6364952Sagc 
72b6364952Sagc /* return 1 if address is in netmask's range */
73b6364952Sagc int
allow_netmask(const char * netmaskarg,const char * addr)74b6364952Sagc allow_netmask(const char *netmaskarg, const char *addr)
75b6364952Sagc {
76b6364952Sagc 	struct in_addr	 a;
77b6364952Sagc 	struct in_addr	 m;
78b6364952Sagc 	const char	*netmask;
79b6364952Sagc 	magic_t		*mp;
80b6364952Sagc 	char	 	 maskaddr[NETMASK_BUFFER_SIZE];
81b6364952Sagc 	char		*cp;
82b6364952Sagc 	int		 slash;
83b6364952Sagc 	int		 i;
84b6364952Sagc 
85b6364952Sagc 	/* firstly check for any magic values in the netmask */
86b6364952Sagc 	netmask = netmaskarg;
87b6364952Sagc 	for (mp = magics ; mp->magic ; mp++) {
88b6364952Sagc 		if (strcmp(netmask, mp->magic) == 0) {
89b6364952Sagc 			netmask = mp->xform;
90b6364952Sagc 			break;
91b6364952Sagc 		}
92b6364952Sagc 	}
93b6364952Sagc 
94b6364952Sagc 	/* find out if slash notation has been used */
95b6364952Sagc 	(void) memset(&a, 0x0, sizeof(a));
96b6364952Sagc 	if ((cp = strchr(netmask, '/')) == NULL) {
97b6364952Sagc 		(void) strlcpy(maskaddr, netmask, sizeof(maskaddr));
98b6364952Sagc 		slash = 32;
99b6364952Sagc 	} else {
100b6364952Sagc 		(void) strlcpy(maskaddr, netmask, MIN(sizeof(maskaddr), (size_t)(cp - netmask) + 1));
101b6364952Sagc 		slash = atoi(cp + 1);
102b6364952Sagc 	}
103b6364952Sagc 
104b6364952Sagc 	/* if we have a wildcard "slash" netmask, then we allow it */
105b6364952Sagc 	if (slash == 0) {
106b6364952Sagc 		return 1;
107b6364952Sagc 	}
108b6364952Sagc 
109b6364952Sagc 	/* canonicalise IPv4 address to dotted quad */
110b6364952Sagc 	for (i = 0, cp = maskaddr ; *cp ; cp++) {
111b6364952Sagc 		if (*cp == '.') {
112b6364952Sagc 			i += 1;
113b6364952Sagc 		}
114b6364952Sagc 	}
115b6364952Sagc 	for ( ; i < 3 ; i++) {
116b6364952Sagc 		(void) snprintf(cp, sizeof(maskaddr) - (int)(cp - maskaddr), ".0");
117b6364952Sagc 		cp += 2;
118b6364952Sagc 	}
119b6364952Sagc 
120b6364952Sagc 	/* translate netmask to in_addr */
121b6364952Sagc 	if (!inet_aton(maskaddr, &m)) {
122b6364952Sagc 		(void) fprintf(stderr, "allow_netmask: can't interpret mask `%s' as an IPv4 address\n", maskaddr);
123b6364952Sagc 		return 0;
124b6364952Sagc 	}
125b6364952Sagc 
126b6364952Sagc 	/* translate address to in_addr */
127b6364952Sagc 	if (!inet_aton(addr, &a)) {
128b6364952Sagc 		(void) fprintf(stderr, "allow_netmask: can't interpret address `%s' as an IPv4 address\n", addr);
129b6364952Sagc 		return 0;
130b6364952Sagc 	}
131b6364952Sagc 
132b6364952Sagc #ifdef ALLOW_NETMASK_DEBUG
133b6364952Sagc 	printf("addr %s %08x, mask %s %08x, slash %d\n", addr, (ISCSI_HTONL(a.s_addr) >> (32 - slash)), maskaddr, (ISCSI_HTONL(m.s_addr) >> (32 - slash)), slash);
134b6364952Sagc #endif
135b6364952Sagc 
136b6364952Sagc 	/* and return 1 if address is in netmask */
137b6364952Sagc 	return (ISCSI_HTONL(a.s_addr) >> (32 - slash)) == (ISCSI_HTONL(m.s_addr) >> (32 - slash));
138b6364952Sagc }
139b6364952Sagc 
140b6364952Sagc #ifdef ALLOW_NETMASK_DEBUG
141b6364952Sagc int
main(int argc,char ** argv)142b6364952Sagc main(int argc, char **argv)
143b6364952Sagc {
144b6364952Sagc 	int	i;
145b6364952Sagc 
146b6364952Sagc 	for (i = 1 ; i < argc ; i+= 2) {
147b6364952Sagc 		if (allow_netmask(argv[i], argv[i + 1])) {
148b6364952Sagc 			printf("mask %s matches addr %s\n\n", argv[i], argv[i + 1]);
149b6364952Sagc 		} else {
150b6364952Sagc 			printf("No match for mask %s from addr %s\n\n", argv[i], argv[i + 1]);
151b6364952Sagc 		}
152b6364952Sagc 	}
153b6364952Sagc 	exit(EXIT_SUCCESS);
154b6364952Sagc }
155b6364952Sagc #endif
156b6364952Sagc 
157b6364952Sagc #if 0
158b6364952Sagc [11:33:02] agc@sys3 ...local/src/netmask 248 > ./n 10.4/16 10.4.0.29 10.4/16 10.5.0.29 10.4/0 10.4.0.19 10.4 10.4.0.19 10.4.3/8 10.4.3.7 10.4.3/24 10.4.3.7
159b6364952Sagc mask 10.4/16 matches addr 10.4.0.29
160b6364952Sagc 
161b6364952Sagc No match for mask 10.4/16 from addr 10.5.0.29
162b6364952Sagc 
163b6364952Sagc mask 10.4/0 matches addr 10.4.0.19
164b6364952Sagc 
165b6364952Sagc No match for mask 10.4 from addr 10.4.0.19
166b6364952Sagc 
167b6364952Sagc mask 10.4.3/8 matches addr 10.4.3.7
168b6364952Sagc 
169b6364952Sagc mask 10.4.3/24 matches addr 10.4.3.7
170b6364952Sagc 
171b6364952Sagc [14:44:52] agc@sys3 ...local/src/netmask 249 >
172b6364952Sagc #endif
173