summaryrefslogtreecommitdiff
path: root/trafgen_l4.c
blob: b3b077502e5180fa8ac25e0ace896efece760709 (plain)
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
/*
 * netsniff-ng - the packet sniffing beast
 * Subject to the GPL, version 2.
 */

#include <stdbool.h>
#include <netinet/in.h>

#include "die.h"
#include "csum.h"
#include "built_in.h"
#include "trafgen_l3.h"
#include "trafgen_l4.h"
#include "trafgen_conf.h"
#include "trafgen_proto.h"

static struct proto_field udp_fields[] = {
	{ .id = UDP_SPORT, .len = 2, .offset = 0 },
	{ .id = UDP_DPORT, .len = 2, .offset = 2 },
	{ .id = UDP_LEN,   .len = 2, .offset = 4 },
	{ .id = UDP_CSUM,  .len = 2, .offset = 6 },
};

static void udp_header_init(struct proto_hdr *hdr)
{
	proto_lower_default_add(hdr, PROTO_IP4);

	proto_header_fields_add(hdr, udp_fields, array_size(udp_fields));
}

static void udp_field_changed(struct proto_field *field)
{
	field->hdr->is_csum_valid = false;
}

static void udp_csum_update(struct proto_hdr *hdr)
{
	struct proto_hdr *lower;
	uint16_t total_len;
	uint16_t csum;

	if (hdr->is_csum_valid)
		return;
	if (proto_field_is_set(hdr, UDP_CSUM))
		return;
	lower = proto_lower_header(hdr);
	if (!lower)
		return;

	total_len = packet_get(hdr->pkt_id)->len - hdr->pkt_offset;

	proto_field_set_default_be16(hdr, UDP_CSUM, 0);

	switch (lower->ops->id) {
	case PROTO_IP4:
		csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
				total_len, IPPROTO_UDP);
		break;
	case PROTO_IP6:
		csum = p6_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
				total_len, IPPROTO_UDP);
		break;
	default:
		csum = 0;
		break;
	}

	proto_field_set_default_be16(hdr, UDP_CSUM, bswap_16(csum));
	hdr->is_csum_valid = true;
}

static void udp_packet_finish(struct proto_hdr *hdr)
{
	struct packet *pkt = current_packet();
	uint16_t total_len;

	total_len = pkt->len - hdr->pkt_offset;
	proto_field_set_default_be16(hdr, UDP_LEN, total_len);

	udp_csum_update(hdr);
}

static const struct proto_ops udp_proto_ops = {
	.id		= PROTO_UDP,
	.layer		= PROTO_L4,
	.header_init	= udp_header_init,
	.packet_update  = udp_csum_update,
	.packet_finish  = udp_packet_finish,
	.field_changed  = udp_field_changed,
};

static struct proto_field tcp_fields[] = {
	{ .id = TCP_SPORT,   .len = 2, .offset = 0 },
	{ .id = TCP_DPORT,   .len = 2, .offset = 2 },
	{ .id = TCP_SEQ,     .len = 4, .offset = 4 },
	{ .id = TCP_ACK_SEQ, .len = 4, .offset = 8 },
	{ .id = TCP_DOFF,    .len = 2, .offset = 12, .shift = 12, .mask = 0xf000 },
	/* reserved (4 bits) */
	{ .id = TCP_CWR,     .len = 2, .offset = 12, .shift = 7, .mask = 0x0080 },
	{ .id = TCP_ECE,     .len = 2, .offset = 12, .shift = 6, .mask = 0x0040 },
	{ .id = TCP_URG,     .len = 2, .offset = 12, .shift = 5, .mask = 0x0020 },
	{ .id = TCP_ACK,     .len = 2, .offset = 12, .shift = 4, .mask = 0x0010 },
	{ .id = TCP_PSH,     .len = 2, .offset = 12, .shift = 3, .mask = 0x0008 },
	{ .id = TCP_RST,     .len = 2, .offset = 12, .shift = 2, .mask = 0x0004 },
	{ .id = TCP_SYN,     .len = 2, .offset = 12, .shift = 1, .mask = 0x0002 },
	{ .id = TCP_FIN,     .len = 2, .offset = 12, .shift = 0, .mask = 0x0001 },
	{ .id = TCP_WINDOW,  .len = 2, .offset = 14 },
	{ .id = TCP_CSUM,    .len = 2, .offset = 16 },
	{ .id = TCP_URG_PTR, .len = 2, .offset = 18 },
};

static void tcp_header_init(struct proto_hdr *hdr)
{
	proto_lower_default_add(hdr, PROTO_IP4);

	proto_header_fields_add(hdr, tcp_fields, array_size(tcp_fields));

	proto_field_set_default_be16(hdr, TCP_DOFF, 5);
}

static void tcp_field_changed(struct proto_field *field)
{
	field->hdr->is_csum_valid = false;
}

static void tcp_csum_update(struct proto_hdr *hdr)
{
	struct proto_hdr *lower = proto_lower_header(hdr);
	struct packet *pkt = current_packet();
	uint16_t total_len;
	uint16_t csum;

	if (hdr->is_csum_valid)
		return;
	if (proto_field_is_set(hdr, TCP_CSUM))
		return;

	if (!lower)
		return;

	total_len = pkt->len - hdr->pkt_offset;

	proto_field_set_default_be16(hdr, TCP_CSUM, 0);

	switch (lower->ops->id) {
	case PROTO_IP4:
		csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
				total_len, IPPROTO_TCP);
		break;
	case PROTO_IP6:
		csum = p6_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
				total_len, IPPROTO_TCP);
		break;
	default:
		csum = 0;
		break;
	}

	proto_field_set_default_be16(hdr, TCP_CSUM, bswap_16(csum));
	hdr->is_csum_valid = true;
}

static const struct proto_ops tcp_proto_ops = {
	.id		= PROTO_TCP,
	.layer		= PROTO_L4,
	.header_init	= tcp_header_init,
	.packet_update  = tcp_csum_update,
	.packet_finish  = tcp_csum_update,
	.field_changed  = tcp_field_changed,
};

static struct proto_field icmpv4_fields[] = {
	{ .id = ICMPV4_TYPE,       .len = 1, .offset = 0 },
	{ .id = ICMPV4_CODE,       .len = 1, .offset = 1 },
	{ .id = ICMPV4_CSUM,       .len = 2, .offset = 2 },
	/* Echo/Ping fields */
	{ .id = ICMPV4_ID,         .len = 2, .offset = 4 },
	{ .id = ICMPV4_SEQ,        .len = 2, .offset = 6 },
	/* Redirect field */
	{ .id = ICMPV4_REDIR_ADDR, .len = 4, .offset = 4 },
	/* Next-hop MTU */
	{ .id = ICMPV4_MTU,        .len = 2, .offset = 6 },
};

static void icmpv4_header_init(struct proto_hdr *hdr)
{
	proto_lower_default_add(hdr, PROTO_IP4);

	proto_header_fields_add(hdr, icmpv4_fields, array_size(icmpv4_fields));
}

static void icmpv4_csum_update(struct proto_hdr *hdr)
{
	struct packet *pkt;
	uint16_t csum;

	if (hdr->is_csum_valid)
		return;
	if (proto_field_is_set(hdr, ICMPV4_CSUM))
		return;

	pkt = packet_get(hdr->pkt_id);

	proto_field_set_default_u16(hdr, ICMPV4_CSUM, 0);
	csum = htons(calc_csum(proto_header_ptr(hdr), pkt->len - hdr->pkt_offset));
	proto_field_set_default_u16(hdr, ICMPV4_CSUM, bswap_16(csum));

	hdr->is_csum_valid = true;
}

static void icmpv4_field_changed(struct proto_field *field)
{
	field->hdr->is_csum_valid = false;
}

static const struct proto_ops icmpv4_proto_ops = {
	.id		= PROTO_ICMP4,
	.layer		= PROTO_L4,
	.header_init	= icmpv4_header_init,
	.packet_update  = icmpv4_csum_update,
	.packet_finish  = icmpv4_csum_update,
	.field_changed  = icmpv4_field_changed,
};

static struct proto_field icmpv6_fields[] = {
	{ .id = ICMPV6_TYPE, .len = 1, .offset = 0 },
	{ .id = ICMPV6_CODE, .len = 1, .offset = 1 },
	{ .id = ICMPV6_CSUM, .len = 2, .offset = 2 }
};

static void icmpv6_header_init(struct proto_hdr *hdr)
{
	proto_lower_default_add(hdr, PROTO_IP6);

	proto_header_fields_add(hdr, icmpv6_fields, array_size(icmpv6_fields));
}

static void icmpv6_csum_update(struct proto_hdr *hdr)
{
	struct proto_hdr *lower = proto_lower_header(hdr);
	struct packet *pkt = packet_get(hdr->pkt_id);
	uint16_t total_len;
	uint16_t csum;

	if (unlikely(!lower))
		return;
	if (hdr->is_csum_valid)
		return;
	if (proto_field_is_set(hdr, ICMPV6_CSUM))
		return;

	total_len = pkt->len - hdr->pkt_offset;

	proto_field_set_be16(hdr, ICMPV6_CSUM, 0);

	if (likely(lower->ops->id == PROTO_IP6)) {
		csum = p6_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
				total_len, IPPROTO_ICMPV6);

		proto_field_set_be16(hdr, ICMPV6_CSUM, bswap_16(csum));
		hdr->is_csum_valid = true;
	}
}

static void icmpv6_field_changed(struct proto_field *field)
{
	field->hdr->is_csum_valid = false;
}

static struct proto_ops icmpv6_proto_ops = {
	.id		= PROTO_ICMP6,
	.layer		= PROTO_L4,
	.header_init	= icmpv6_header_init,
	.packet_finish  = icmpv6_csum_update,
	.packet_update  = icmpv6_csum_update,
	.field_changed  = icmpv6_field_changed,
};

void protos_l4_init(void)
{
	proto_ops_register(&udp_proto_ops);
	proto_ops_register(&tcp_proto_ops);
	proto_ops_register(&icmpv4_proto_ops);
	proto_ops_register(&icmpv6_proto_ops);
}