summaryrefslogtreecommitdiffstats
path: root/common/VIL/l2l3_stack/lib_icmpv6.c
blob: eea67b0d6823923f09f2af5fe36477d327b87956 (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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
/*	Santosh Sethupathi*/

#include "lib_icmpv6.h"

static void print_pkt(uint8_t *rd)
{
	int i = 0, j = 0;

	printf("Packet Contents:\n");

	for (i = 0; i < 20; i++) {
		for (j = 0; j < 20; j++)
			printf("%02x ", rd[(20 * i) + j]);

		printf("\n");
	}
}

static uint16_t icmpv6_ipv6_nd_checksum(struct rte_mbuf *pkt)
{
	struct ether_hdr *eth_h;
	struct ipv6_hdr *ipv6_h;
	struct icmpv6_hdr *icmpv6_h;

	size_t tmplen, offset;
	uint8_t *tmppacket, *tpacket;

	eth_h = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
	ipv6_h = (struct ipv6_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
	icmpv6_h =
			(struct icmpv6_hdr *)((char *)ipv6_h + sizeof(struct ipv6_hdr));

	uint32_t payloadlen = 0x20;
	payloadlen = rte_bswap32(payloadlen);

	tmplen = 40 + sizeof(struct icmpv6_hdr) + sizeof(struct icmpv6_nd_hdr);
	tmplen = RTE_CACHE_LINE_ROUNDUP(tmplen);
	tmppacket = rte_zmalloc(NULL, tmplen, RTE_CACHE_LINE_SIZE);
	tpacket = tmppacket;

	offset = 16;
	memcpy(tpacket, &ipv6_h->src_addr[0], offset);
	tpacket += offset;
	memcpy(tpacket, &ipv6_h->dst_addr[0], offset);
	tpacket += offset;
	*tpacket = 0;
	tpacket++;
	*tpacket = 0;
	tpacket++;
	*tpacket = 0;
	tpacket++;
	memcpy(tpacket, &ipv6_h->proto, 1);
	tpacket++;
	memcpy(tpacket, &payloadlen, 4);
	tpacket += 4;
	memcpy(tpacket, icmpv6_h,
				 sizeof(struct icmpv6_hdr) + sizeof(struct icmpv6_nd_hdr));

	if (ARPICMP_DEBUG)
		print_pkt(tmppacket);

	return rte_raw_cksum(tmppacket, tmplen);
}

static uint16_t icmpv6_ipv6_echo_checksum(struct rte_mbuf *pkt)
{
	struct ether_hdr *eth_h;
	struct ipv6_hdr *ipv6_h;
	struct icmpv6_hdr *icmpv6_h;

	size_t tmplen, offset;
	uint8_t *tmppacket, *tpacket;

	eth_h = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
	ipv6_h = (struct ipv6_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
	icmpv6_h =
			(struct icmpv6_hdr *)((char *)ipv6_h + sizeof(struct ipv6_hdr));

	uint32_t payloadlen = rte_bswap16(ipv6_h->payload_len);
	uint32_t payloadlen_swap = rte_bswap32(payloadlen);

	if (ARPICMP_DEBUG)
		printf("%s: payloadlen: %u\n", __FUNCTION__, payloadlen);

	tmplen = 40 + payloadlen;
	tmplen = RTE_CACHE_LINE_ROUNDUP(tmplen);
	tmppacket = rte_zmalloc(NULL, tmplen, RTE_CACHE_LINE_SIZE);
	tpacket = tmppacket;

	offset = 16;
	memcpy(tpacket, &ipv6_h->src_addr[0], offset);
	tpacket += offset;
	memcpy(tpacket, &ipv6_h->dst_addr[0], offset);
	tpacket += offset;
	*tpacket = 0;
	tpacket++;
	*tpacket = 0;
	tpacket++;
	*tpacket = 0;
	tpacket++;
	memcpy(tpacket, &ipv6_h->proto, 1);
	tpacket++;
	memcpy(tpacket, &payloadlen_swap, 4);
	tpacket += 4;
	memcpy(tpacket, icmpv6_h, payloadlen);

	if (ARPICMP_DEBUG)
		print_pkt(tmppacket);

	return rte_raw_cksum(tmppacket, tmplen);
}

void process_icmpv6_pkt(struct rte_mbuf *pkt, l2_phy_interface_t *port)
{

	struct ether_hdr *eth_h;
	struct ipv6_hdr *ipv6_h;
	struct icmpv6_hdr *icmpv6_h;
	struct icmpv6_nd_hdr *icmpv6_nd_h;
	uint8_t ipv6_addr[16];
	uint8_t i = 0;
	uint8_t req_tipv6[16];
	/* To drop the packet */

	if (port == NULL) {
		printf("port is NULL");
		return;
	} else if (port->ipv6_list == NULL) {
		printf("IPV6 address not configured on link\n");
		return;
	}

	eth_h = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
	ipv6_h = (struct ipv6_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
	icmpv6_h =
			(struct icmpv6_hdr *)((char *)ipv6_h + sizeof(struct ipv6_hdr));

	if ((icmpv6_h->icmpv6_type == ICMPV6_ECHO_REQUEST)
			&& (icmpv6_h->icmpv6_code == 0)) {
		for (i = 0; i < 16; i++) {
			ipv6_addr[i] = ipv6_h->src_addr[i];
		}

		ether_addr_copy(&eth_h->s_addr, &eth_h->d_addr);
		ether_addr_copy((struct ether_addr *)&port->macaddr[0],
				&eth_h->s_addr);

		for (i = 0; i < 16; i++)
			ipv6_h->src_addr[i] = ipv6_h->dst_addr[i];
		for (i = 0; i < 16; i++)
			ipv6_h->dst_addr[i] = ipv6_addr[i];

		icmpv6_h->icmpv6_type = ICMPV6_ECHO_REPLY;
		icmpv6_h->icmpv6_cksum = 0;
		icmpv6_h->icmpv6_cksum = ~icmpv6_ipv6_echo_checksum(pkt);
		port->transmit_bulk_pkts(port, &pkt, 1);

		return;
	} else if ((icmpv6_h->icmpv6_type == ICMPV6_ECHO_REPLY)
			 && (icmpv6_h->icmpv6_code == 0)) {
		struct nd_key_ipv6 nd_key;
		nd_key.port_id = port->pmdid;
		for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
			nd_key.ipv6[i] = ipv6_h->src_addr[i];

		}
		nd_key.filler1 = 0;
		nd_key.filler2 = 0;
		nd_key.filler3 = 0;

		/*Validate if key-value pair already exists in the hash table for ND IPv6 */
		struct nd_entry_data *new_nd_data = retrieve_nd_entry(nd_key, DYNAMIC_ND);
		if (new_nd_data == NULL) {
			printf
					("Received unsolicited ICMPv6 echo reply on port %d\n",
					 nd_key.port_id);
			for (i = 0; i < ND_IPV6_ADDR_SIZE; i += 2) {
				printf("%02X%02X ", nd_key.ipv6[i],
							 nd_key.ipv6[i + 1]);
			}
			return;
		}

		new_nd_data->status = COMPLETE;
	} else if ((icmpv6_h->icmpv6_type == ICMPV6_NEIGHBOR_SOLICITATION)
			 && (icmpv6_h->icmpv6_code == 0)) {

		icmpv6_nd_h =
				(struct icmpv6_nd_hdr *)((char *)icmpv6_h +
							 sizeof(struct icmpv6_hdr));
		struct ether_addr *src_hw_addr = &eth_h->s_addr;
		uint8_t src_ipv6[16], dst_ipv6[16];
		uint16_t multi_addr;

		for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
			src_ipv6[i] = ipv6_h->src_addr[i];

		for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
			dst_ipv6[i] = ipv6_h->dst_addr[i];

		multi_addr = dst_ipv6[0];

		/*  Check for Multicast Address */
		if ((IPV6_MULTICAST & ((multi_addr << 8) | dst_ipv6[1]))
				|| !memcmp(&port->macaddr[0], &eth_h->d_addr, 6)) {

			populate_nd_entry(src_hw_addr, src_ipv6, port->pmdid,
						DYNAMIC_ND);

			/* build a Neighbor Advertisement message */
			for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
				req_tipv6[i] = icmpv6_nd_h->target_ipv6[i];

			if (!memcmp
					(&req_tipv6[0],
					 &((ipv6list_t *) port->ipv6_list)->ipaddr[0],
					 16)) {

				ether_addr_copy(&eth_h->s_addr, &eth_h->d_addr);
				ether_addr_copy((struct ether_addr *)&port->
						macaddr[0], &eth_h->s_addr);

				/* set sender mac address */
				for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
					ipv6_h->dst_addr[i] =
							ipv6_h->src_addr[i];
				for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
					ipv6_h->src_addr[i] = req_tipv6[i];
				icmpv6_h->icmpv6_type =
						ICMPV6_NEIGHBOR_ADVERTISEMENT;
				icmpv6_nd_h->type = e_Target_Link_Layer_Address;
				icmpv6_nd_h->length = 1;
				memcpy(&icmpv6_nd_h->link_layer_addr[0],
							 &port->macaddr[0], 6);
				icmpv6_nd_h->icmpv6_reserved = 0;
				icmpv6_nd_h->icmpv6_reserved |=
						rte_cpu_to_be_32
						(NEIGHBOR_ROUTER_OVERRIDE_SET);

				icmpv6_h->icmpv6_cksum = 0;
				icmpv6_h->icmpv6_cksum =
						~icmpv6_ipv6_nd_checksum(pkt);

				port->transmit_bulk_pkts(port, &pkt, 1);

			} else if (ARPICMP_DEBUG) {
				printf
						("............Some one else is the target host here !!!\n");
			}

			return;
		} else {
			if (ARPICMP_DEBUG) {
				printf
						("...............Malformed ND Solicitation message!!!\n");
			}
		}

	} else if ((icmpv6_h->icmpv6_type == ICMPV6_NEIGHBOR_ADVERTISEMENT)
			 && (icmpv6_h->icmpv6_code == 0)) {
		struct ether_addr *src_hw_addr = &eth_h->s_addr;
		uint8_t ipv6[16];
		for (i = 0; i < ND_IPV6_ADDR_SIZE; i++) {
			ipv6[i] = ipv6_h->src_addr[i];

		}
		populate_nd_entry(src_hw_addr, ipv6, port->pmdid, DYNAMIC_ND);
	} else {
		if (ARPICMP_DEBUG) {
			printf("ICMPv6 Type %d Not Supported yet !!!\n",
						 icmpv6_h->icmpv6_type);
		}
	}

	rte_pktmbuf_free(pkt);
}

struct rte_mbuf *request_icmpv6_echo(uint8_t ipv6[], l2_phy_interface_t *port)
{
	struct ether_hdr *eth_h;
	struct ipv6_hdr *ipv6_h;
	struct icmpv6_hdr *icmpv6_h;
	struct icmpv6_info_hdr *icmpv6_info_h;
	int i;
	uint8_t *icmp_data;

	struct rte_mbuf *icmpv6_pkt = lib_icmpv6_pkt;
	if (icmpv6_pkt == NULL) {
		if (ARPICMP_DEBUG)
			printf("Error allocating icmpv6_pkt rte_mbuf\n");
		return NULL;
	}

	eth_h = rte_pktmbuf_mtod(icmpv6_pkt, struct ether_hdr *);

	ipv6_h = (struct ipv6_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
	icmpv6_h =
			(struct icmpv6_hdr *)((char *)ipv6_h + sizeof(struct ipv6_hdr));
	icmpv6_info_h =
			(struct icmpv6_info_hdr *)((char *)icmpv6_h +
							 sizeof(struct icmpv6_hdr));

	ether_addr_copy((struct ether_addr *)&port->macaddr[0], &eth_h->s_addr);
	eth_h->ether_type = rte_bswap16(0x86dd);
	for (i = 0; i < 6; i++) {
		eth_h->d_addr.addr_bytes[i] = 0;
	}

	ipv6_h->vtc_flow = rte_bswap32(0x60000000);
	ipv6_h->payload_len = rte_bswap16(64);
	ipv6_h->proto = 58;
	ipv6_h->hop_limits = 64;

	for (i = 0; i < 16; i++) {
		ipv6_h->src_addr[i] = 0x0;
		ipv6_h->dst_addr[i] = ipv6[i];
	}

	icmpv6_h->icmpv6_type = ICMPV6_ECHO_REQUEST;
	icmpv6_h->icmpv6_code = 0;
	icmpv6_info_h->icmpv6_ident = rte_bswap16(0x5151);
	icmpv6_info_h->icmpv6_seq_nb = rte_bswap16(0x1);

	icmp_data = (uint8_t *) icmpv6_h + 8;
	for (i = 0; i < 56; i++) {
		*icmp_data = i + 1;
		icmp_data++;
	}
	icmpv6_h->icmpv6_cksum = 0;
	icmpv6_h->icmpv6_cksum = ~icmpv6_ipv6_echo_checksum(icmpv6_pkt);

	icmpv6_pkt->pkt_len =
			sizeof(struct ether_hdr) + sizeof(struct ipv6_hdr) + 64;
	icmpv6_pkt->data_len = icmpv6_pkt->pkt_len;

	if (port)
		port->transmit_single_pkt(port, icmpv6_pkt);

	return icmpv6_pkt;
}

struct rte_mbuf *request_nd(uint8_t ipv6[], l2_phy_interface_t *port)
{
	struct ether_hdr *eth_h;
	struct ipv6_hdr *ipv6_h;
	struct icmpv6_hdr *icmpv6_h;
	struct icmpv6_nd_hdr *icmpv6_nd_h;
	int i;

	struct rte_mbuf *icmpv6_pkt = lib_nd_pkt[port->pmdid];
	if (icmpv6_pkt == NULL) {
		if (ARPICMP_DEBUG)
			printf("Error allocating icmpv6_pkt rte_mbuf\n");
		return NULL;
	}

	uint8_t dst_ip[] = {255, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 255, 16, 100, 20};
	uint8_t dst_mac[] = {51,51,255, 16, 100, 20};

	eth_h = rte_pktmbuf_mtod(icmpv6_pkt, struct ether_hdr *);

	ipv6_h = (struct ipv6_hdr *)((char *)eth_h + sizeof(struct ether_hdr));
	icmpv6_h =
			(struct icmpv6_hdr *)((char *)ipv6_h + sizeof(struct ipv6_hdr));
	icmpv6_nd_h =
			(struct icmpv6_nd_hdr *)((char *)icmpv6_h +
						 sizeof(struct icmpv6_hdr));

	ether_addr_copy((struct ether_addr *)&port->macaddr[0], &eth_h->s_addr);
	eth_h->ether_type = rte_bswap16(0x86dd);

	for (i = 0; i < 6; i++) {
		if (i < 3)
			eth_h->d_addr.addr_bytes[i] = dst_mac[i];
		else
			eth_h->d_addr.addr_bytes[i] = ipv6[i];
	}

	for (i=13; i<16; i++)
		dst_ip[i] = ipv6[i];

	uint8_t *addr = ((ipv6list_t *) (port->ipv6_list))->ipaddr;

	ipv6_h->vtc_flow = rte_bswap32(0x60000000);
	ipv6_h->payload_len = rte_bswap16(32);
	ipv6_h->proto = 58;
	ipv6_h->hop_limits = 255;

	for (i = 0; i < 16; i++) {
		ipv6_h->src_addr[i] = *(addr + i);
		ipv6_h->dst_addr[i] = dst_ip[i];
	}

	icmpv6_h->icmpv6_type = ICMPV6_NEIGHBOR_SOLICITATION;
	icmpv6_h->icmpv6_code = 0;

	icmpv6_nd_h->icmpv6_reserved = 0x0;
	icmpv6_nd_h->icmpv6_reserved |=
			rte_cpu_to_be_32
			(NEIGHBOR_ROUTER_OVERRIDE_SET);

	for (i = 0; i < ND_IPV6_ADDR_SIZE; i++)
		icmpv6_nd_h->target_ipv6[i] = ipv6[i];
	icmpv6_nd_h->type = e_Source_Link_Layer_Address;
	icmpv6_nd_h->length = 1;
	memcpy(&icmpv6_nd_h->link_layer_addr[0], &port->macaddr[0], 6);

	icmpv6_h->icmpv6_cksum = 0;
	icmpv6_h->icmpv6_cksum = ~icmpv6_ipv6_nd_checksum(icmpv6_pkt);
	icmpv6_pkt->pkt_len =
			sizeof(struct ether_hdr) + sizeof(struct ipv6_hdr) + 32;
	icmpv6_pkt->data_len = icmpv6_pkt->pkt_len;

	if (port) {
		port->transmit_single_pkt(port, icmpv6_pkt);
	}

	return icmpv6_pkt;
}