File: ip_subnet.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (329 lines) | stat: -rw-r--r-- 7,580 bytes parent folder | download | duplicates (2)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* ip subnet, for libreswan
 *
 * Copyright (C) 2012-2017 Paul Wouters <pwouters@redhat.com>
 * Copyright (C) 2012 Avesh Agarwal <avagarwa@redhat.com>
 * Copyright (C) 1998-2002,2015  D. Hugh Redelmeier.
 * Copyright (C) 2016-2020 Andrew Cagney <cagney@gnu.org>
 * Copyright (C) 2017 Vukasin Karadzic <vukasin.karadzic@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "jambuf.h"
#include "ip_subnet.h"
#include "passert.h"
#include "lswlog.h"	/* for pexpect() */
#include "ip_info.h"

const ip_subnet unset_subnet; /* all zeros */

ip_subnet subnet_from_raw(where_t where,
			  const struct ip_info *afi,
			  const struct ip_bytes bytes,
			  unsigned prefix_len)
{
	ip_subnet s = {
		.is_set = true,
		.version = afi->ip_version,
		.bytes = bytes,
		.maskbits = prefix_len,
	};
	pexpect_subnet(&s, where);
	return s;
}

ip_subnet subnet_from_address(const ip_address address)
{
	const struct ip_info *afi = address_info(address);
	if (afi == NULL) {
		return unset_subnet;
	}

	return subnet_from_raw(HERE, afi, address.bytes, afi->mask_cnt);
}

ip_subnet subnet_from_cidr(const ip_cidr cidr)
{
	const struct ip_info *afi = cidr_info(cidr);
	if (afi == NULL) {
		return unset_subnet;
	}

	return subnet_from_raw(HERE, afi,
			       ip_bytes_blit(afi, cidr.bytes,
					     &keep_routing_prefix,
					     &clear_host_identifier,
					     cidr.prefix_len),
			       cidr.prefix_len);
}

err_t address_mask_to_subnet(const ip_address address,
			     const ip_address mask,
			     ip_subnet *subnet)
{
	*subnet = unset_subnet;
	const struct ip_info *afi = address_info(address);
	if (afi == NULL) {
		return "invalid address";
	}

	if (address_info(mask) != afi) {
		return "invalid mask";
	}

	int prefix_len =  ip_bytes_mask_len(afi, mask.bytes);
	if (prefix_len < 0) {
		return "invalid mask";
	}

	struct ip_bytes prefix = ip_bytes_blit(afi, address.bytes,
					       &keep_routing_prefix,
					       &clear_host_identifier,
					       prefix_len);
	*subnet = subnet_from_raw(HERE, afi, prefix, prefix_len);
	return NULL;
}

ip_address subnet_prefix(const ip_subnet subnet)
{
	const struct ip_info *afi = subnet_info(subnet);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return unset_address;
	}

	struct ip_bytes prefix = ip_bytes_blit(afi, subnet.bytes,
					       &keep_routing_prefix,
					       &clear_host_identifier,
					       subnet.maskbits);
	return address_from_raw(HERE, afi, prefix);
}

const struct ip_info *subnet_type(const ip_subnet *subnet)
{
	if (subnet == NULL) {
		return NULL;
	}

	/* may return NULL */
	return subnet_info(*subnet);
}

const struct ip_info *subnet_info(const ip_subnet subnet)
{
	if (!subnet.is_set) {
		return NULL;
	}

	/* may return NULL */
	return ip_version_info(subnet.version);
}

bool subnet_is_unset(const ip_subnet *subnet)
{
	if (subnet == NULL) {
		return true;
	}

	return !subnet->is_set;
}

bool subnet_is_zero(const ip_subnet subnet)
{
	const struct ip_info *afi = subnet_info(subnet);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return false;
	}

	return subnet_eq_subnet(subnet, afi->subnet.zero);
}

bool subnet_is_all(const ip_subnet subnet)
{
	const struct ip_info *afi = subnet_info(subnet);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return false;
	}

	return subnet_eq_subnet(subnet, afi->subnet.all);
}

uintmax_t subnet_size(const ip_subnet subnet)
{
	ip_range range = range_from_subnet(subnet);
	return range_size(range);
}

/*
 * subnet mask - get the mask of a subnet, as an address
 *
 * For instance 1.2.3.4/24 -> 255.255.255.0.
 */

ip_address subnet_prefix_mask(const ip_subnet subnet)
{
	const struct ip_info *afi = subnet_info(subnet);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return unset_address;
	}

	struct ip_bytes mask = ip_bytes_blit(afi, subnet.bytes,
					     &set_routing_prefix,
					     &clear_host_identifier,
					     subnet.maskbits);
	return address_from_raw(HERE, afi, mask);
}

unsigned subnet_prefix_bits(const ip_subnet subnet)
{
	return subnet.maskbits;
}

size_t jam_subnet(struct jambuf *buf, const ip_subnet *subnet)
{
	const struct ip_info *afi = subnet_type(subnet);
	if (afi == NULL) {
		return jam_string(buf, "<unset-subnet>");
	}

	size_t s = 0;
	ip_address sa = subnet_prefix(*subnet);
	s += jam_address(buf, &sa); /* sensitive? */
	s += jam(buf, "/%u", subnet->maskbits);
	return s;
}

const char *str_subnet(const ip_subnet *subnet, subnet_buf *out)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(out->buf);
	jam_subnet(&buf, subnet);
	return out->buf;
}

size_t jam_subnets(struct jambuf *buf, ip_subnets subnets)
{
	size_t s = 0;
	const char *sep = "";
	for (unsigned i = 0; i < subnets.len; i++) {
		s += jam_string(buf, sep);
		sep = ",";
		s += jam_subnet(buf, &subnets.list[i]);
	}
	return s;
}

void pexpect_subnet(const ip_subnet *s, where_t where)
{
	if (s == NULL) {
		return;
	}

	/* more strict than is_unset() */
	if (subnet_eq_subnet(*s, unset_subnet)) {
		return;
	}

	if (s->is_set == false ||
	    s->version == 0) {
		llog_pexpect(&global_logger, where, "invalid subnet: "PRI_SUBNET, pri_subnet(s));
	}
}

bool subnet_eq_subnet(const ip_subnet l, const ip_subnet r)
{
	if (subnet_is_unset(&l) && subnet_is_unset(&r)) {
		/* NULL/unset subnets are equal */
		return true;
	}

	if (subnet_is_unset(&l) || subnet_is_unset(&r)) {
		return false;
	}

	/* must compare individual fields */
	return (l.version == r.version &&
		thingeq(l.bytes, r.bytes) &&
		l.maskbits == r.maskbits);
}

bool subnet_eq_address(const ip_subnet subnet, const ip_address address)
{
	const struct ip_info *afi = subnet_info(subnet);
	if (afi == NULL) {
		return false;
	}

	/* XXX: reject any? */
	/* must compare individual fields */
	return (subnet.version == address.version &&
		thingeq(subnet.bytes, address.bytes) &&
		subnet.maskbits == afi->mask_cnt);
}

bool subnet_in_subnet(const ip_subnet l, const ip_subnet r)
{
	const struct ip_info *afi = subnet_info(l);
	if (afi == NULL) {
		return false;
	}

	if (subnet_info(r) != afi) {
		return false;
	}

	/* l's prefix needs to be longer than r's */
	if (l.maskbits < r.maskbits) {
		return false;
	}

	/* L.prefix[0 .. R.bits] == R.prefix[0.. R.bits] */
	struct ip_bytes lb = ip_bytes_blit(afi,
					   /*LEFT*/l.bytes,
					   &keep_routing_prefix,
					   &clear_host_identifier,
					   /*RIGHT*/r.maskbits);
	return thingeq(lb, r.bytes);
}

bool address_in_subnet(const ip_address l, const ip_subnet r)
{
	const struct ip_info *afi = address_info(l);
	if (afi == NULL) {
		return false;
	}

	if (subnet_info(r) != afi) {
		return false;
	}

	/* L.prefix[0 .. R.bits] == R.prefix[0.. R.bits] */
	struct ip_bytes lb = ip_bytes_blit(afi,
					   /*LEFT*/l.bytes,
					   &keep_routing_prefix,
					   &clear_host_identifier,
					   /*RIGHT*/r.maskbits);
	return thingeq(lb, r.bytes);
}

err_t addresses_to_nonzero_subnet(const ip_address start, const ip_address end, ip_subnet *dst)
{
	*dst = unset_subnet;
	ip_range range;
	err_t err = addresses_to_nonzero_range(start, end, &range);
	if (err != NULL) {
		return err;
	}

	return range_to_subnet(range, dst);
}