File: ip_endpoint.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 (342 lines) | stat: -rw-r--r-- 8,848 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
330
331
332
333
334
335
336
337
338
339
340
341
342
/* ip endpoint (address + port), for libreswan
 *
 * Copyright (C) 2018-2019 Andrew Cagney <cagney@gnu.org>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library 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/lgpl-2.1.txt>.
 *
 * This library 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 Library General Public
 * License for more details.
 *
 */

#include <sys/socket.h>		/* for AF_INET/AF_INET6 */

#include "jambuf.h"
#include "ip_endpoint.h"
#include "constants.h"		/* for memeq() */
#include "ip_info.h"
#include "ip_protocol.h"
#include "lswlog.h"		/* for bad_case() */

const ip_endpoint unset_endpoint; /* all zeros */

ip_endpoint endpoint_from_raw(where_t where,
			      const struct ip_info *afi,
			      const struct ip_bytes bytes,
			      const struct ip_protocol *protocol,
			      ip_port port)
{
	ip_endpoint endpoint = {
		.is_set = true,
		.version = afi->ip_version,
		.bytes = bytes,
		.hport = port.hport,
		.ipproto = protocol->ipproto,
	};
	pexpect_endpoint(&endpoint, where);
	return endpoint;
}

ip_endpoint endpoint_from_address_protocol_port(const ip_address address,
						const struct ip_protocol *protocol,
						ip_port port)
{
	const struct ip_info *afi = address_info(address);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return unset_endpoint; /* empty_address? */
	}

	return endpoint_from_raw(HERE, afi, address.bytes,
				 protocol, port);
}

ip_address endpoint_address(const ip_endpoint endpoint)
{
	const struct ip_info *afi = endpoint_info(endpoint);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return unset_address; /* empty_address? */
	}

	return address_from_raw(HERE, afi, endpoint.bytes);
}

int endpoint_hport(const ip_endpoint endpoint)
{
	const struct ip_info *afi = endpoint_info(endpoint);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		/* not asserting, who knows what nonsense a user can generate */
		dbg("%s has unspecified type", __func__);
		return -1;
	}

	return endpoint.hport;
}

ip_port endpoint_port(const ip_endpoint endpoint)
{
	const struct ip_info *afi = endpoint_info(endpoint);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		/* not asserting, who knows what nonsense a user can generate */
		dbg("%s has unspecified type", __func__);
		return unset_port;
	}

	return ip_hport(endpoint.hport);
}

ip_endpoint set_endpoint_port(const ip_endpoint endpoint, ip_port port)
{
	const struct ip_info *afi = endpoint_info(endpoint);
	if (afi == NULL) {
		/* includes NULL+unset+unknown */
		/* not asserting, who knows what nonsense a user can generate */
		dbg("endpoint has unspecified type");
		return unset_endpoint;
	}

	ip_endpoint dst = endpoint;
	dst.hport = hport(port);
	pendpoint(&dst);
	return dst;
}

const struct ip_info *endpoint_type(const ip_endpoint *endpoint)
{
	if (endpoint == NULL) {
		return NULL;
	}

	/* may return NULL */
	return endpoint_info(*endpoint);
}

const struct ip_info *endpoint_info(const ip_endpoint endpoint)
{
	if (!endpoint.is_set) {
		return NULL;
	}

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

bool endpoint_is_unset(const ip_endpoint *endpoint)
{
	if (endpoint == NULL) {
		return true;
	}
	return !endpoint->is_set;
}

const struct ip_protocol *endpoint_protocol(const ip_endpoint endpoint)
{
	if (endpoint_is_unset(&endpoint)) {
		return NULL;
	}
	return protocol_from_ipproto(endpoint.ipproto);
}

bool endpoint_is_specified(const ip_endpoint endpoint)
{
	const struct ip_info *afi = endpoint_info(endpoint);
	if (afi == NULL) {
		/* NULL+unset+unknown */
		return false;
	}

	/* treat any 0 address as suspect */
	if (thingeq(endpoint.bytes, unset_ip_bytes)) {
		/* any address (but we know it is zero) */
		return false;
	}

	return true;
}

/*
 * Format an endpoint as ADDRESS:PORT.
 *
 * Either ADDRESS:PORT (IPv4) or [ADDRESS]:PORT, but when PORT is
 * invalid, just the ADDRESS is formatted.
 *
 * From wikipedia: For TCP, port number 0 is reserved and
 * cannot be used, while for UDP, the source port is optional
 * and a value of zero means no port.
 */

size_t jam_endpoint(struct jambuf *buf, const ip_endpoint *endpoint)
{
	const struct ip_info *afi = endpoint_type(endpoint);
	if (afi == NULL) {
		return jam_string(buf, "<unset-endpoint>");
	}

	size_t s = 0;
	s += afi->jam.address_wrapped(buf, afi, &endpoint->bytes);
	s += jam(buf, ":%d", endpoint->hport);
	return s;
}

const char *str_endpoint(const ip_endpoint *endpoint, endpoint_buf *dst)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(dst->buf);
	jam_endpoint(&buf, endpoint);
	return dst->buf;
}

size_t jam_endpoint_sensitive(struct jambuf *buf, const ip_endpoint *endpoint)
{
	if (!log_ip) {
		return jam_string(buf, "<endpoint>");
	}

	return jam_endpoint(buf, endpoint);
}

const char *str_endpoint_sensitive(const ip_endpoint *endpoint, endpoint_buf *dst)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(dst->buf);
	jam_endpoint_sensitive(&buf, endpoint);
	return dst->buf;
}

/*
 * Format an endpoint as ADDRESS:PROTOCOL/PORT.
 *
 * Either ADDRESS:PORT (IPv4) or [ADDRESS]:PORT, but when PORT is
 * invalid, just the ADDRESS is formatted.
 *
 * From wikipedia: For TCP, port number 0 is reserved and
 * cannot be used, while for UDP, the source port is optional
 * and a value of zero means no port.
 */

size_t jam_endpoint_address_protocol_port(struct jambuf *buf, const ip_endpoint *endpoint)
{
	const struct ip_info *afi = endpoint_type(endpoint);
	if (afi == NULL) {
		return jam_string(buf, "<unset-endpoint>");
	}

	size_t s = 0;
	s += afi->jam.address_wrapped(buf, afi, &endpoint->bytes);
	s += jam_string(buf, ":");
	s += jam_protocol(buf, endpoint_protocol(*endpoint));
	s += jam_string(buf, "/");
	s += jam_hport(buf, endpoint_port(*endpoint));
	return s;
}

const char *str_endpoint_address_protocol_port(const ip_endpoint *endpoint, endpoint_buf *dst)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(dst->buf);
	jam_endpoint_address_protocol_port(&buf, endpoint);
	return dst->buf;
}

size_t jam_endpoint_address_protocol_port_sensitive(struct jambuf *buf, const ip_endpoint *endpoint)
{
	if (!log_ip) {
		return jam_string(buf, "<endpoint>");
	}

	return jam_endpoint_address_protocol_port(buf, endpoint);
}

const char *str_endpoint_address_protocol_port_sensitive(const ip_endpoint *endpoint, endpoint_buf *dst)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(dst->buf);
	jam_endpoint_address_protocol_port_sensitive(&buf, endpoint);
	return dst->buf;
}

size_t jam_endpoint_pair(struct jambuf *buf, const ip_endpoint *src, const ip_endpoint *dst)
{
	size_t s = 0;
	s += jam_endpoint(buf, src);
	s += jam_char(buf, ' ');


	const struct ip_protocol *srcp = src != NULL ? endpoint_protocol(*src) : &ip_protocol_all;
	const struct ip_protocol *dstp = src != NULL ? endpoint_protocol(*dst) : &ip_protocol_all;
	s += jam_protocol_pair(buf, srcp, '-', dstp);

	s += jam_char(buf, ' ');
	s += jam_endpoint(buf, dst);
	return s;
}

const char *str_endpoint_pair(const ip_endpoint *src, const ip_endpoint *dst, endpoint_pair_buf *out)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(out->buf);
	jam_endpoint_pair(&buf, src, dst);
	return out->buf;
}

bool endpoint_eq_endpoint(const ip_endpoint l, const ip_endpoint r)
{
	if (endpoint_is_unset(&l) && endpoint_is_unset(&r)) {
		/* unset/NULL endpoints are equal */
		return true;
	}

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

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

bool endpoint_address_eq_address(const ip_endpoint endpoint, const ip_address address)
{
	ip_address ea = endpoint_address(endpoint);
	return address_eq_address(ea, address);
}

void pexpect_endpoint(const ip_endpoint *e, where_t where)
{
	if (e == NULL) {
		return;
	}

	/* more strict than is_unset() */
	if (endpoint_eq_endpoint(*e, unset_endpoint)) {
		return;
	}

	/*
	 * XXX: xfrm generates tcp acquires of the form:
	 *
	 *   192.1.2.45:TCP/0 -> 192.1.2.23:TCP/80 (0x5000)
	 *
	 * Presumably source port 0 is because the connect(?) call
	 * specified no source port.
	 *
	 * Until there's an ip_traffic object to wrap this up, this
	 * passert can't require a port.
	 *
	 * XXX: is [::]:TCP/10 valid?
	 */

	const struct ip_protocol *protocol = endpoint_protocol(*e);
	if (e->is_set == false ||
	    e->version == 0 ||
	    e->ipproto == 0 ||
	    protocol == NULL /* ||
	    (protocol->endpoint_requires_non_zero_port && e->hport == 0) */) {
		llog_pexpect(&global_logger, where, "invalid endpoint: "PRI_ENDPOINT, pri_endpoint(e));
	}
}