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

#include <string.h>

#include "str.h"
#include "xmalloc.h"
#include "built_in.h"
#include "trafgen_l7.h"
#include "trafgen_proto.h"

static struct proto_field dns_fields[] = {
	{ .id = DNS_ID,       .len = 2, .offset = 0 },
	{ .id = DNS_QR,       .len = 2, .offset = 2, .shift = 15, .mask = 0x8000 },
	{ .id = DNS_OPCODE,   .len = 2, .offset = 2, .shift = 11, .mask = 0x7800 },
	{ .id = DNS_AA,       .len = 2, .offset = 2, .shift = 10, .mask = 0x0400 },
	{ .id = DNS_TC,       .len = 2, .offset = 2, .shift = 9,  .mask = 0x0200 },
	{ .id = DNS_RD,       .len = 2, .offset = 2, .shift = 8,  .mask = 0x0100 },
	{ .id = DNS_RA,       .len = 2, .offset = 2, .shift = 7,  .mask = 0x80 },
	{ .id = DNS_ZERO,     .len = 2, .offset = 2, .shift = 4,  .mask = 0x30 },
	{ .id = DNS_RCODE,    .len = 2, .offset = 2, .shift = 0,  .mask = 0xf },
	{ .id = DNS_QD_COUNT, .len = 2, .offset = 4, },
	{ .id = DNS_AN_COUNT, .len = 2, .offset = 6, },
	{ .id = DNS_NS_COUNT, .len = 2, .offset = 8, },
	{ .id = DNS_AR_COUNT, .len = 2, .offset = 10, },
};

static struct proto_field dns_query_fields[] = {
	{ .id = DNS_QUERY_NAME,  .len = 0, .offset = 0 },
	{ .id = DNS_QUERY_TYPE,  .len = 2, .offset = 0 },
	{ .id = DNS_QUERY_CLASS, .len = 2, .offset = 2 },
};

static void dns_query_header_init(struct proto_hdr *hdr)
{
	proto_header_fields_add(hdr, dns_query_fields, array_size(dns_query_fields));
}

static void dns_query_header_finish(struct proto_hdr *hdr)
{
	proto_hdr_field_set_default_string(hdr, DNS_QUERY_NAME, "www.netsniff-ng.com");
	proto_hdr_field_set_default_be16(hdr, DNS_QUERY_CLASS, 1);
	proto_hdr_field_set_default_be16(hdr, DNS_QUERY_TYPE, 1);
}

static const struct proto_ops dns_proto_query_ops = {
	.header_init	= dns_query_header_init,
	.header_finish	= dns_query_header_finish,
};

static struct proto_field dns_rrecord_fields[] = {
	{ .id = DNS_RRECORD_NAME,  .len = 0, .offset = 0 },
	{ .id = DNS_RRECORD_TYPE,  .len = 2, .offset = 0 },
	{ .id = DNS_RRECORD_CLASS, .len = 2, .offset = 2 },
	{ .id = DNS_RRECORD_TTL,   .len = 4, .offset = 4 },
	{ .id = DNS_RRECORD_LEN,   .len = 2, .offset = 8 },
	{ .id = DNS_RRECORD_DATA,  .len = 0, .offset = 10 },
};

static void dns_rrecord_header_init(struct proto_hdr *hdr)
{
	proto_header_fields_add(hdr, dns_rrecord_fields, array_size(dns_rrecord_fields));
}

static void dns_rrecord_header_finish(struct proto_hdr *hdr)
{
	struct proto_field *data = proto_hdr_field_by_id(hdr, DNS_RRECORD_DATA);

	proto_hdr_field_set_default_be32(hdr, DNS_RRECORD_TTL, 1);
	proto_hdr_field_set_default_be16(hdr, DNS_RRECORD_CLASS, 1);
	proto_hdr_field_set_default_be16(hdr, DNS_RRECORD_LEN, data->len);
}

static const struct proto_ops dns_proto_rrecord_ops = {
	.header_init	= dns_rrecord_header_init,
	.header_finish  = dns_rrecord_header_finish,
};

static void dns_header_init(struct proto_hdr *hdr)
{
	proto_lower_default_add(hdr, PROTO_UDP);

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

static void dns_sort_headers(struct proto_hdr *hdr, uint32_t id, int index)
{
	int i;

	for (i = index; i < hdr->sub_headers_count; i++) {
		struct proto_hdr *sub_hdr = hdr->sub_headers[i];

		if (sub_hdr->id == id && sub_hdr->index != index) {
			proto_hdr_move_sub_header(hdr, sub_hdr, hdr->sub_headers[index]);
			index++;
		}
	}
}

static void dns_header_finish(struct proto_hdr *hdr)
{
	size_t ar_count = 0;
	size_t ns_count = 0;
	size_t qd_count = 0;
	size_t an_count = 0;
	int i;

	for (i = 0; i < hdr->sub_headers_count; i++) {
		struct proto_hdr *sub_hdr = hdr->sub_headers[i];

		switch (sub_hdr->id) {
		case DNS_QUERY_HDR:
			qd_count++;
			break;
		case DNS_ANSWER_HDR:
			an_count++;
			break;
		case DNS_AUTH_HDR:
			ns_count++;
			break;
		case DNS_ADD_HDR:
			ar_count++;
			break;
		}
	}

	dns_sort_headers(hdr, DNS_QUERY_HDR, 0);
	dns_sort_headers(hdr, DNS_ANSWER_HDR, qd_count);
	dns_sort_headers(hdr, DNS_AUTH_HDR, qd_count + an_count);
	dns_sort_headers(hdr, DNS_ADD_HDR, qd_count + an_count + ns_count);

	proto_hdr_field_set_default_be16(hdr, DNS_QD_COUNT, qd_count);
	proto_hdr_field_set_default_be16(hdr, DNS_AN_COUNT, an_count);
	proto_hdr_field_set_default_be16(hdr, DNS_NS_COUNT, ns_count);
	proto_hdr_field_set_default_be16(hdr, DNS_AR_COUNT, ar_count);

	if (an_count)
		proto_hdr_field_set_default_be16(hdr, DNS_QR, 1);
}

static void dns_push_sub_header(struct proto_hdr *hdr, struct proto_hdr *sub_hdr)
{
	switch (sub_hdr->id) {
	case DNS_QUERY_HDR:
		sub_hdr->ops = &dns_proto_query_ops;
		break;
	case DNS_ANSWER_HDR:
	case DNS_AUTH_HDR:
	case DNS_ADD_HDR:
		sub_hdr->ops = &dns_proto_rrecord_ops;
		break;
	default:
		bug();
	}
}

static const struct proto_ops dns_proto_ops = {
	.id		 = PROTO_DNS,
	.layer		 = PROTO_L7,
	.header_init	 = dns_header_init,
	.header_finish   = dns_header_finish,
	.push_sub_header = dns_push_sub_header,
};

void protos_l7_init(void)
{
	proto_ops_register(&dns_proto_ops);
}