summaryrefslogtreecommitdiff
path: root/proto_ipv6_mobility_hdr.c
blob: 90f067808dd89d276777ecaab70d9b60fac261c2 (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
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
/*
 * netsniff-ng - the packet sniffing beast
 * Copyright 2012 Markus Amend <markus@netsniff-ng.org>, Deutsche Flugsicherung GmbH
 * Subject to the GPL, version 2.
 *
 * IPv6 Mobility Header described in RFC6275
 */

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <netinet/in.h>    /* for ntohs() */
#include <arpa/inet.h>

#include "proto.h"
#include "dissector_eth.h"
#include "built_in.h"
#include "pkt_buff.h"

#define BINDING_REFRESH_REQUEST_MESSAGE	0x00
#define HOME_TEST_INIT_MESSAGE		0x01
#define CARE_OF_TEST_INIT_MESSAGE	0x02
#define HOME_TEST_MESSAGE		0x03
#define CARE_OF_TEST_MESSAGE		0x04
#define BINDING_UPDATE_MESSAGE		0x05
#define BINDING_ACKNOWLEDGEMENT_MESSAGE	0x06
#define BINDING_ERROR_MESSAGE		0x07

struct mobilityhdr {
	uint8_t		payload_proto;
	uint8_t		hdr_len;
	uint8_t		MH_type;
	uint8_t		reserved;
	uint16_t	chksum;
	uint8_t		msgdata[0];
} __packed;

struct bin_refr_req_msg {
	uint16_t	reserved;
	uint8_t		mobility_opt[0];
} __packed;

/* for 0x01 and 0x02 */
struct tst_init_msg {
	uint16_t	reserved;
	uint64_t	init_cookie;
	uint8_t		mobility_opt[0];
} __packed;

/* for 0x03 and 0x04 */
struct tst_msg {
	uint16_t	nonce_index;
	uint64_t	init_cookie;
	uint64_t	keygen_token;
	uint8_t		mobility_opt[0];
} __packed;

struct bind_upd_msg {
	uint16_t	sequence;
	uint16_t	ahlk_res;
	uint16_t	lifetime;
	uint8_t		mobility_opt[0];
} __packed;

struct bind_ack_msg {
	uint8_t		status;
	uint8_t		k_res;
	uint16_t	sequence;
	uint16_t	lifetime;
	uint8_t		mobility_opt[0];
} __packed;

struct bind_err_msg {
	uint8_t		status;
	uint8_t		res;
	uint64_t	home_addr;
	uint8_t		mobility_opt[0];
} __packed;


static void dissect_mobility_options(struct pkt_buff *pkt __maybe_unused,
				     ssize_t *message_data_len)
{
	/* Have to been upgraded.
	 * http://tools.ietf.org/html/rfc6275#section-6.2.1
	 */
	if (*message_data_len)
		tprintf("MH Option(s) recognized ");

	/* If adding dissector reduce message_data_len for each using of
	 * pkt_pull to the same size.
	 */
}

static void dissect_mobilityhdr_type_0(struct pkt_buff *pkt,
				       ssize_t *message_data_len)
{
	struct bin_refr_req_msg *type_0;
	
	type_0 = (struct bin_refr_req_msg *) pkt_pull(pkt, sizeof(*type_0));
	*message_data_len -= sizeof(*type_0);
	if (type_0 == NULL || *message_data_len > pkt_len(pkt) ||
	    *message_data_len < 0)
		return;

	dissect_mobility_options(pkt, message_data_len);
}

static void dissect_mobilityhdr_type_1_2(struct pkt_buff *pkt,
					 ssize_t *message_data_len)
{
	struct tst_init_msg *type_1_2;

	type_1_2 = (struct tst_init_msg *) pkt_pull(pkt, sizeof(*type_1_2));
	*message_data_len -= sizeof(*type_1_2);
	if (type_1_2 == NULL || *message_data_len > pkt_len(pkt) ||
	    *message_data_len < 0)
		return;

	tprintf("Init Cookie (0x%"PRIx64")", ntohll(type_1_2->init_cookie));

	dissect_mobility_options(pkt, message_data_len);
}

static void dissect_mobilityhdr_type_3_4(struct pkt_buff *pkt,
					 ssize_t *message_data_len)
{
	struct tst_msg *type_3_4;

	type_3_4 = (struct tst_msg *) pkt_pull(pkt, sizeof(*type_3_4));
	*message_data_len -= sizeof(*type_3_4);
	if (type_3_4 == NULL || *message_data_len > pkt_len(pkt) ||
	    *message_data_len < 0)
		return;

	tprintf("HN Index (%u) ", ntohs(type_3_4->nonce_index));
	tprintf("Init Cookie (0x%"PRIx64") ", ntohll(type_3_4->init_cookie));
	tprintf("Keygen Token (0x%"PRIx64")", ntohll(type_3_4->keygen_token));

	dissect_mobility_options(pkt, message_data_len);
}

static void dissect_mobilityhdr_type_5(struct pkt_buff *pkt,
				       ssize_t *message_data_len)
{
	struct bind_upd_msg *type_5;

	type_5 = (struct bind_upd_msg *) pkt_pull(pkt, sizeof(*type_5));
	*message_data_len -= sizeof(*type_5);
	if (type_5 == NULL || *message_data_len > pkt_len(pkt) ||
	    *message_data_len < 0)
		return;

	tprintf("Sequence (0x%x) ", ntohs(type_5->sequence));
	tprintf("A|H|L|K (0x%x) ", ntohs(type_5->ahlk_res) >> 12);
	tprintf("Lifetime (%us)", ntohs(type_5->lifetime) * 4);

	dissect_mobility_options(pkt, message_data_len);
}

static void dissect_mobilityhdr_type_6(struct pkt_buff *pkt,
				       ssize_t *message_data_len)
{
	struct bind_ack_msg *type_6;

	type_6 = (struct bind_ack_msg *) pkt_pull(pkt, sizeof(*type_6));
	if (type_6 == NULL)
		return;

	*message_data_len -= sizeof(*type_6);
	if (*message_data_len > pkt_len(pkt) || *message_data_len < 0)
		return;

	tprintf("Status (0x%x) ", type_6->status);
	tprintf("K (%u) ", type_6->k_res >> 7);
	tprintf("Sequence (0x%x)", ntohs(type_6->sequence));
	tprintf("Lifetime (%us)", ntohs(type_6->lifetime) * 4);

	dissect_mobility_options(pkt, message_data_len);
}

static void dissect_mobilityhdr_type_7(struct pkt_buff *pkt,
				       ssize_t *message_data_len)
{
	char address[INET6_ADDRSTRLEN];
	uint64_t addr;
	struct bind_err_msg *type_7;

	type_7 = (struct bind_err_msg *) pkt_pull(pkt, sizeof(*type_7));
	if (type_7 == NULL)
		return;

	*message_data_len -= sizeof(*type_7);
	addr = ntohll(type_7->home_addr);
	if (*message_data_len > pkt_len(pkt) || *message_data_len < 0)
		return;

	tprintf("Status (0x%x) ", type_7->status);
	tprintf("Home Addr (%s)",
		inet_ntop(AF_INET6, &addr, address,
		sizeof(address)));

	dissect_mobility_options(pkt, message_data_len);
}

static void get_mh_type(struct pkt_buff *pkt, ssize_t *message_data_len,
			uint8_t *mh_type)
{
	switch (*mh_type) {
	case BINDING_REFRESH_REQUEST_MESSAGE:
		tprintf("Binding Refresh Request Message ");
		dissect_mobilityhdr_type_0(pkt, message_data_len);
		break;
	case HOME_TEST_INIT_MESSAGE:
		tprintf("Home Test Init Message ");
		dissect_mobilityhdr_type_1_2(pkt, message_data_len);
		break;
	case CARE_OF_TEST_INIT_MESSAGE:
		tprintf("Care-of Test Init Message ");
		dissect_mobilityhdr_type_1_2(pkt, message_data_len);
		break;
	case HOME_TEST_MESSAGE:
		tprintf("Binding Refresh Request Message ");
		dissect_mobilityhdr_type_3_4(pkt, message_data_len);
		break;
	case CARE_OF_TEST_MESSAGE:
		tprintf("Binding Refresh Request Message ");
		dissect_mobilityhdr_type_3_4(pkt, message_data_len);
		break;
	case BINDING_UPDATE_MESSAGE:
		tprintf("Binding Refresh Request Message ");
		dissect_mobilityhdr_type_5(pkt, message_data_len);
		break;
	case BINDING_ACKNOWLEDGEMENT_MESSAGE:
		tprintf("Binding Refresh Request Message ");
		dissect_mobilityhdr_type_6(pkt, message_data_len);
		break;
	case BINDING_ERROR_MESSAGE:
		tprintf("Binding Refresh Request Message ");
		dissect_mobilityhdr_type_7(pkt, message_data_len);
		break;
	default:
		tprintf("Type %u is unknown. Error", *mh_type);
	}
}

static void mobility(struct pkt_buff *pkt)
{
	uint16_t hdr_ext_len;
	ssize_t message_data_len;
	struct mobilityhdr *mobility;

	mobility = (struct mobilityhdr *) pkt_pull(pkt, sizeof(*mobility));
	if (mobility == NULL)
		return;

	/* Total Header Length in Bytes */
	hdr_ext_len = (mobility->hdr_len + 1) * 8;
	/* Total Message Data length in Bytes*/
	message_data_len = (hdr_ext_len - sizeof(*mobility));

	tprintf("\t [ Mobility ");
	tprintf("NextHdr (%u), ", mobility->payload_proto);
	if (message_data_len > pkt_len(pkt) || message_data_len < 0){
		tprintf("HdrExtLen (%u, %u Bytes %s), ", mobility->hdr_len,
				hdr_ext_len, colorize_start_full(black, red)
				"invalid" colorize_end());
		return;
	}
	tprintf("HdrExtLen (%u, %u Bytes), ", mobility->hdr_len,
		hdr_ext_len);
	tprintf("MH Type (%u), ", mobility->MH_type);
	tprintf("Res (0x%x), ", mobility->reserved);
	tprintf("Chks (0x%x), ", ntohs(mobility->chksum));
	tprintf("MH Data ");

	get_mh_type(pkt, &message_data_len, &mobility->MH_type);

	tprintf(" ]\n");

	if (message_data_len > pkt_len(pkt) || message_data_len < 0)
		return;

	pkt_pull(pkt, message_data_len);
	pkt_set_dissector(pkt, &eth_lay3, mobility->payload_proto);
}

static void mobility_less(struct pkt_buff *pkt)
{
	uint16_t hdr_ext_len;
	ssize_t message_data_len;
	struct mobilityhdr *mobility;

	mobility = (struct mobilityhdr *) pkt_pull(pkt, sizeof(*mobility));
	if (mobility == NULL)
		return;

	/* Total Header Length in Bytes */
	hdr_ext_len = (mobility->hdr_len + 1) * 8;
	/* Total Message Data length in Bytes*/
	message_data_len = (hdr_ext_len - sizeof(*mobility));
	if (message_data_len > pkt_len(pkt) || message_data_len < 0)
		return;

	tprintf(" Mobility Type (%u), ", mobility->MH_type);

	pkt_pull(pkt, message_data_len);
	pkt_set_dissector(pkt, &eth_lay3, mobility->payload_proto);
}

struct protocol ipv6_mobility_ops = {
	.key = 0x87,
	.print_full = mobility,
	.print_less = mobility_less,
};