File: ip_info.c

package info (click to toggle)
libreswan 5.2-2.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,656 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 (291 lines) | stat: -rw-r--r-- 8,151 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
/* AF Information, 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-2017 Andrew Cagney
 * 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 <sys/socket.h>		/* for AF_INET/AF_INET6/AF_UNSPEC */

#include "ietf_constants.h"
#include "ip_info.h"
#include "passert.h"
#include "lswlog.h"		/* for bad_case() */

const struct ip_info unspec_ip_info = {
	.af = AF_UNSPEC,
};

/*
 * Implement https://tools.ietf.org/html/rfc5952
 */

static size_t jam_ipv4_address(struct jambuf *buf, const struct ip_info *afi, const struct ip_bytes *bytes)
{
	const char *sep = "";
	size_t s = 0;
	for (size_t i = 0; i < afi->ip_size; i++) {
		s += jam(buf, "%s%"PRIu8, sep, bytes->byte[i]);
		sep = ".";
	}
	return s;
}

/*
 * Find longest run of zero pairs that should be suppressed (need at
 * least two).
 */
static shunk_t zeros_to_skip(const struct ip_info *afi, const struct ip_bytes *bytes)
{
	shunk_t zero = null_shunk;
	const uint8_t *ptr = bytes->byte;
	/* stop at or before last pair; ensure ptr[1] safe */
	const uint8_t *last = ptr + afi->ip_size - 2;
	while (ptr <= last) {
		/*
		 * Set L to the the number of paired zero bytes
		 * starting at PTR (could be zero).
		 */
		unsigned l = 0;
		for (l = 0; ptr + l <= last; l += 2) {
			/* need both bytes zero */
			if (ptr[l+0] != 0 || ptr[l+1] != 0) {
				break;
			}
		}
		/*
		 * Save longer run, but only when more than one pair.
		 */
		if (l > 2 && l > zero.len) {
			zero = shunk2(ptr, l);
			ptr += l;
		} else {
			ptr += 2;
		}
	}
	return zero;
}

static size_t jam_ipv6_address(struct jambuf *buf, const struct ip_info *afi, const struct ip_bytes *bytes)
{
	size_t s = 0;
	shunk_t zeros = zeros_to_skip(afi, bytes);
	const char *sep = "";
	const uint8_t *ptr = bytes->byte;
	/* stop at or before last pair; ensure ptr[1] safe */
	const uint8_t *last = ptr + afi->ip_size - 2;
	while (ptr <= last) {
		if (ptr == zeros.ptr) {
			/* skip zero run */
			s += jam(buf, "::");
			sep = "";
			ptr += zeros.len;
		} else {
			/* print pair of bytes in hex, suppress leading zeros */
			unsigned ia = (ptr[0] << 8) + ptr[1];
			s += jam(buf, "%s%x", sep, ia);
			sep = ":";
			ptr += 2;
		}
	}
	return s;
}

static size_t jam_ipv6_address_wrapped(struct jambuf *buf, const struct ip_info *afi, const struct ip_bytes *bytes)
{
	size_t s = 0;
	s += jam_string(buf, "[");
	s += jam_ipv6_address(buf, afi, bytes);
	s += jam_string(buf, "]");
	return s;
}

static ip_address address_from_ipv4_sockaddr(const ip_sockaddr sa)
{
	passert(sa.sa.sa.sa_family == AF_INET);
	return address_from_in_addr(&sa.sa.sin.sin_addr);
}

static ip_address address_from_ipv6_sockaddr(const ip_sockaddr sa)
{
	passert(sa.sa.sa.sa_family == AF_INET6);
	return address_from_in6_addr(&sa.sa.sin6.sin6_addr);
}

static ip_port port_from_ipv4_sockaddr(const ip_sockaddr sa)
{
	passert(sa.sa.sa.sa_family == AF_INET);
	return ip_nport(sa.sa.sin.sin_port);
}

static ip_port port_from_ipv6_sockaddr(const ip_sockaddr sa)
{
	passert(sa.sa.sa.sa_family == AF_INET6);
	return ip_nport(sa.sa.sin6.sin6_port);
}

/*
 * Construct well known addresses.
 */

#define IPv4_FF { { 255, 255, 255, 255, }, }

const struct ip_info ip_families[IP_INDEX_ROOF] = {

	[IPv4_INDEX] = {

		.ip_version = IPv4,
		.ip_index = IPv4_INDEX,
		.ip_size = sizeof(struct in_addr),
		.ip_name = "IPv4",
		.inet_name = "inet",
		.mask_cnt = 32,

		/* formatting */
		.jam.address = jam_ipv4_address,
		.jam.address_wrapped = jam_ipv4_address,

		/* ip_address - .address.any matches grep */
		.address.unspec = { .is_set = true, .version = IPv4, }, /* 0.0.0.0 */
		.address.loopback = { .is_set = true, .version = IPv4, .bytes = { { 127, 0, 0, 1, }, }, },

		/* ip_subnet - .subnet.any matches grep */
		.subnet.zero = { .is_set = true, .version = IPv4, .maskbits = 32, }, /* 0.0.0.0/32 */
		.subnet.all = { .is_set = true, .version = IPv4, .maskbits = 0, }, /* 0.0.0.0/0 */

		/* ip_range - .range.any matches grep */
		.range.zero = { .is_set = true, .version = IPv4, },
		.range.all = { .is_set = true, .version = IPv4, .hi = IPv4_FF, },

		/* ip_selector - .selector.any matches grep */
		.selector.zero = { .is_set = true, .version = IPv4, }, /* 0.0.0.0/0 */
		.selector.all = { .is_set = true, .version = IPv4, .hi = IPv4_FF, }, /* 0.0.0.0/0 */

		/* ike */
		.ikev1_max_fragment_size = ISAKMP_V1_FRAG_MAXLEN_IPv4,
		.ikev2_max_fragment_size = ISAKMP_V2_FRAG_MAXLEN_IPv4,

		/* socket() */
		.socket = {
			.domain = PF_INET,
			.domain_name = "PF_INET",
		},

		/* sockaddr */
		.af = AF_INET,
		.af_name = "AF_INET",
		.sockaddr_size = sizeof(struct sockaddr_in),
		.address_from_sockaddr = address_from_ipv4_sockaddr,
		.port_from_sockaddr = port_from_ipv4_sockaddr,

		/* IKEv2 Traffic Selector */
		.ikev2_ts_addr_range_type = IKEv2_TS_IPV4_ADDR_RANGE,
		.ikev2_internal_address = IKEv2_INTERNAL_IP4_ADDRESS,
		.ikev2_internal_dns = IKEv2_INTERNAL_IP4_DNS,

		/* id */
		.id_ip_addr = ID_IPV4_ADDR,
		.id_ip_addr_subnet = ID_IPV4_ADDR_SUBNET,
		.id_ip_addr_range = ID_IPV4_ADDR_RANGE,
	},

#define IPv6_FF { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }, }

	[IPv6_INDEX] = {

		.ip_version = IPv6,
		.ip_index = IPv6_INDEX,
		.ip_size = sizeof(struct in6_addr),
		.ip_name = "IPv6",
		.inet_name = "inet6",
		.mask_cnt = 128,

		/* formatting */
		.jam.address = jam_ipv6_address,
		.jam.address_wrapped = jam_ipv6_address_wrapped,

		/* ip_address - .address.any matches grep */
		.address.unspec = { .is_set = true, .version = IPv6, }, /* :: */
		.address.loopback = { .is_set = true, .version = IPv6, .bytes = { { [15] = 1, }, }, }, /* ::1 */

		/* ip_subnet - .subnet.any matches grep */
		.subnet.zero = { .is_set = true, .version = IPv6, .maskbits = 128, }, /* ::/128 */
		.subnet.all = { .is_set = true, .version = IPv6, .maskbits = 0, }, /* ::/0 */

		/* ip_range - .range.any matches grep */
		.range.zero = { .is_set = true, .version = IPv6, },
		.range.all = { .is_set = true, .version = IPv6, .hi = IPv6_FF, },

		/* ip_selector - .selector.any matches grep */
		.selector.zero = { .is_set = true, .version = IPv6, }, /* ::/0 */
		.selector.all = { .is_set = true, .version = IPv6, .hi = IPv6_FF, }, /* ::/0 */

		/* ike */
		.ikev1_max_fragment_size = ISAKMP_V1_FRAG_MAXLEN_IPv6,
		.ikev2_max_fragment_size = ISAKMP_V2_FRAG_MAXLEN_IPv6,

		/* socket() */
		.socket = {
			.domain = PF_INET6,
			.domain_name = "PF_INET6",
		},

		/* sockaddr */
		.af = AF_INET6,
		.af_name = "AF_INET6",
		.sockaddr_size = sizeof(struct sockaddr_in6),
		.address_from_sockaddr = address_from_ipv6_sockaddr,
		.port_from_sockaddr = port_from_ipv6_sockaddr,

		/* IKEv2 Traffic Selector */
		.ikev2_ts_addr_range_type = IKEv2_TS_IPV6_ADDR_RANGE,
		.ikev2_internal_address = IKEv2_INTERNAL_IP6_ADDRESS,
		.ikev2_internal_dns = IKEv2_INTERNAL_IP6_DNS,

		/* id */
		.id_ip_addr = ID_IPV6_ADDR,
		.id_ip_addr_subnet = ID_IPV6_ADDR_SUBNET,
		.id_ip_addr_range = ID_IPV6_ADDR_RANGE,
	},
};

const struct ip_info *aftoinfo(int af)
{
	switch (af) {
	case AF_INET:
		return &ipv4_info;
	case AF_INET6:
		return &ipv6_info;
	case AF_UNSPEC:
#if 0
		return &unspec_info;
#else
		return NULL;
#endif
	default:
		return NULL;
	}
}

const struct ip_info *ip_version_info(enum ip_version version)
{
	static const struct ip_info *ip_types[] = {
		[0] = NULL,
		[IPv4] = &ipv4_info,
		[IPv6] = &ipv6_info,
	};
	passert(version < elemsof(ip_types));
	return ip_types[version];
}