File: ip_packet.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 (179 lines) | stat: -rw-r--r-- 4,513 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
/* up selector, for libreswan
 *
 * Copyright (C) 2020  Andrew Cagney
 * Copyright (C) 2000  Henry Spencer.
 *
 * 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 "lswlog.h"

#include "ip_packet.h"
#include "ip_info.h"
#include "ip_protocol.h"

const ip_packet unset_packet;

ip_packet packet_from_raw(where_t where,
			  /* INFO determines meaning of BYTES */
			  const struct ip_info *info,
			  const struct ip_bytes *src_bytes,
			  const struct ip_bytes *dst_bytes,
			  /* PROTOCOL determines meaning of PORTs */
			  const struct ip_protocol *protocol,
			  ip_port src_port, ip_port dst_port)
{
	ip_packet packet = {
		.is_set = true,
		.info = info,
		.protocol = protocol,
		.src = {
			.bytes = *src_bytes,
			.hport = src_port.hport, /* can be zero */
		},
		.dst = {
			.bytes = *dst_bytes,
			.hport = dst_port.hport,
		},
	};
	pexpect_packet(&packet, where);
	return packet;
}

static const char *unset(const ip_packet *packet)
{
	return IP_INFO_PROTOCOL_UNSET(packet);
}

ip_address packet_src_address(const ip_packet packet)
{
	if (packet.is_set) {
		return address_from_raw(HERE, packet.info,
					 packet.src.bytes);
	} else {
		return unset_address;
	}
}

ip_address packet_dst_address(const ip_packet packet)
{
	if (packet.is_set) {
		return address_from_raw(HERE, packet.info,
					 packet.dst.bytes);
	} else {
		return unset_address;
	}
}

ip_endpoint packet_dst_endpoint(const ip_packet packet)
{
	if (packet.is_set) {
		return endpoint_from_raw(HERE, packet.info,
					 packet.dst.bytes,
					 packet.protocol,
					 ip_hport(packet.dst.hport));
	} else {
		return unset_endpoint;
	}
}

ip_selector packet_src_selector(const ip_packet packet)
{
	if (packet.is_set) {
		return selector_from_raw(HERE,
					 packet.info,
					 packet.src.bytes,
					 packet.src.bytes,
					 packet.protocol,
					 ip_hport(packet.src.hport));
	} else {
		return unset_selector;
	}
}

ip_selector packet_dst_selector(const ip_packet packet)
{
	if (packet.is_set) {
		return selector_from_raw(HERE,
					 packet.info,
					 packet.dst.bytes,
					 packet.dst.bytes,
					 packet.protocol,
					 ip_hport(packet.dst.hport));
	} else {
		return unset_selector;
	}
}

size_t jam_packet(struct jambuf *buf, const ip_packet *packet)
{
	const char *u = unset(packet);
	if (u != NULL) {
		return jam(buf, "<packet-%s>", u);
	}

	const struct ip_info *afi = packet->info;

	size_t s = 0;
	if (packet->src.hport == 0 && packet->protocol->zero_port_is_any) {
		/*
		 * SRC port can be zero aka wildcard aka ephemeral, it
		 * isn't know to pluto so denotes any and should be
		 * omitted.
		 *
		 * For IPv6, jam_wrapped() includes includes [] so
		 * output is consistent with endpoint.jam().
		 */
		s += afi->jam.address_wrapped(buf, afi, &packet->src.bytes);
	} else {
		s += afi->jam.address_wrapped(buf, afi, &packet->src.bytes);
		s += jam(buf, ":%u", packet->src.hport);
	}
	/* DST port is always valid */
	s += jam(buf, "-%s->", packet->protocol->name);
	s += afi->jam.address_wrapped(buf, afi, &packet->dst.bytes);
	s += jam(buf, ":%u", packet->dst.hport);
	return s;
}

const char *str_packet(const ip_packet *packet, packet_buf *dst)
{
	struct jambuf buf = ARRAY_AS_JAMBUF(dst->buf);
	jam_packet(&buf, packet);
	return dst->buf;
}

void pexpect_packet(const ip_packet *packet, where_t where)
{
	if (packet == NULL) {
		llog_pexpect(&global_logger, where, "null packet");
		return;
	}

	if (packet->info == NULL ||
	    packet->protocol == NULL ||
	    ip_bytes_is_zero(&packet->src.bytes) ||
	    ip_bytes_is_zero(&packet->dst.bytes) ||
	    /*
	     * An acquire triggered by a packet with no specified
	     * source port will have a zero source port.
	     */
	    (packet->protocol->zero_port_is_any && packet->dst.hport == 0)) {
		if (packet->is_set) {
			llog_pexpect(&global_logger, where,
				     "invalid packet: "PRI_PACKET, pri_packet(packet));

		}
	} else if (!packet->is_set) {
		llog_pexpect(&global_logger, where,
			     "invalid packet: "PRI_PACKET, pri_packet(packet));
	}
}