xref: /dpdk/lib/ip_frag/rte_ipv6_reassembly.c (revision 89b5642d0d45c22c0ceab57efe3fab3b49ff4324)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <stddef.h>
699a2dd95SBruce Richardson 
799a2dd95SBruce Richardson #include <rte_memcpy.h>
899a2dd95SBruce Richardson 
999a2dd95SBruce Richardson #include "ip_frag_common.h"
1099a2dd95SBruce Richardson 
1199a2dd95SBruce Richardson /**
1299a2dd95SBruce Richardson  * @file
1399a2dd95SBruce Richardson  * IPv6 reassemble
1499a2dd95SBruce Richardson  *
1599a2dd95SBruce Richardson  * Implementation of IPv6 reassembly.
1699a2dd95SBruce Richardson  */
1799a2dd95SBruce Richardson 
1899a2dd95SBruce Richardson static inline void
1999a2dd95SBruce Richardson ip_frag_memmove(char *dst, char *src, int len)
2099a2dd95SBruce Richardson {
2199a2dd95SBruce Richardson 	int i;
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson 	/* go backwards to make sure we don't overwrite anything important */
2499a2dd95SBruce Richardson 	for (i = len - 1; i >= 0; i--)
2599a2dd95SBruce Richardson 		dst[i] = src[i];
2699a2dd95SBruce Richardson }
2799a2dd95SBruce Richardson 
2899a2dd95SBruce Richardson /*
2999a2dd95SBruce Richardson  * Reassemble fragments into one packet.
3099a2dd95SBruce Richardson  */
3199a2dd95SBruce Richardson struct rte_mbuf *
3299a2dd95SBruce Richardson ipv6_frag_reassemble(struct ip_frag_pkt *fp)
3399a2dd95SBruce Richardson {
3499a2dd95SBruce Richardson 	struct rte_ipv6_hdr *ip_hdr;
35b7fc82ecSKonstantin Ananyev 	struct rte_ipv6_fragment_ext *frag_hdr;
3699a2dd95SBruce Richardson 	struct rte_mbuf *m, *prev;
3799a2dd95SBruce Richardson 	uint32_t i, n, ofs, first_len;
3899a2dd95SBruce Richardson 	uint32_t last_len, move_len, payload_len;
3999a2dd95SBruce Richardson 	uint32_t curr_idx = 0;
4099a2dd95SBruce Richardson 
4199a2dd95SBruce Richardson 	first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
4299a2dd95SBruce Richardson 	n = fp->last_idx - 1;
4399a2dd95SBruce Richardson 
4499a2dd95SBruce Richardson 	/*start from the last fragment. */
4599a2dd95SBruce Richardson 	m = fp->frags[IP_LAST_FRAG_IDX].mb;
4699a2dd95SBruce Richardson 	ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
4799a2dd95SBruce Richardson 	last_len = fp->frags[IP_LAST_FRAG_IDX].len;
4899a2dd95SBruce Richardson 	curr_idx = IP_LAST_FRAG_IDX;
4999a2dd95SBruce Richardson 
5099a2dd95SBruce Richardson 	payload_len = ofs + last_len;
5199a2dd95SBruce Richardson 
5299a2dd95SBruce Richardson 	while (ofs != first_len) {
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson 		prev = m;
5599a2dd95SBruce Richardson 
5699a2dd95SBruce Richardson 		for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
5799a2dd95SBruce Richardson 
5899a2dd95SBruce Richardson 			/* previous fragment found. */
5999a2dd95SBruce Richardson 			if (fp->frags[i].ofs + fp->frags[i].len == ofs) {
6099a2dd95SBruce Richardson 
6199a2dd95SBruce Richardson 				RTE_ASSERT(curr_idx != i);
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson 				/* adjust start of the last fragment data. */
6499a2dd95SBruce Richardson 				rte_pktmbuf_adj(m,
6599a2dd95SBruce Richardson 					(uint16_t)(m->l2_len + m->l3_len));
6699a2dd95SBruce Richardson 				rte_pktmbuf_chain(fp->frags[i].mb, m);
6799a2dd95SBruce Richardson 
6899a2dd95SBruce Richardson 				/* this mbuf should not be accessed directly */
6999a2dd95SBruce Richardson 				fp->frags[curr_idx].mb = NULL;
7099a2dd95SBruce Richardson 				curr_idx = i;
7199a2dd95SBruce Richardson 
7299a2dd95SBruce Richardson 				/* update our last fragment and offset. */
7399a2dd95SBruce Richardson 				m = fp->frags[i].mb;
7499a2dd95SBruce Richardson 				ofs = fp->frags[i].ofs;
7599a2dd95SBruce Richardson 			}
7699a2dd95SBruce Richardson 		}
7799a2dd95SBruce Richardson 
7899a2dd95SBruce Richardson 		/* error - hole in the packet. */
7999a2dd95SBruce Richardson 		if (m == prev) {
8099a2dd95SBruce Richardson 			return NULL;
8199a2dd95SBruce Richardson 		}
8299a2dd95SBruce Richardson 	}
8399a2dd95SBruce Richardson 
8499a2dd95SBruce Richardson 	/* chain with the first fragment. */
8599a2dd95SBruce Richardson 	rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
8699a2dd95SBruce Richardson 	rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
8799a2dd95SBruce Richardson 	fp->frags[curr_idx].mb = NULL;
8899a2dd95SBruce Richardson 	m = fp->frags[IP_FIRST_FRAG_IDX].mb;
8999a2dd95SBruce Richardson 	fp->frags[IP_FIRST_FRAG_IDX].mb = NULL;
9099a2dd95SBruce Richardson 
9199a2dd95SBruce Richardson 	/* update ipv6 header for the reassembled datagram */
9299a2dd95SBruce Richardson 	ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *, m->l2_len);
9399a2dd95SBruce Richardson 
9499a2dd95SBruce Richardson 	ip_hdr->payload_len = rte_cpu_to_be_16(payload_len);
9599a2dd95SBruce Richardson 
9699a2dd95SBruce Richardson 	/*
9799a2dd95SBruce Richardson 	 * remove fragmentation header. note that per RFC2460, we need to update
9899a2dd95SBruce Richardson 	 * the last non-fragmentable header with the "next header" field to contain
9999a2dd95SBruce Richardson 	 * type of the first fragmentable header, but we currently don't support
10099a2dd95SBruce Richardson 	 * other headers, so we assume there are no other headers and thus update
10199a2dd95SBruce Richardson 	 * the main IPv6 header instead.
10299a2dd95SBruce Richardson 	 */
10399a2dd95SBruce Richardson 	move_len = m->l2_len + m->l3_len - sizeof(*frag_hdr);
104b7fc82ecSKonstantin Ananyev 	frag_hdr = (struct rte_ipv6_fragment_ext *) (ip_hdr + 1);
10599a2dd95SBruce Richardson 	ip_hdr->proto = frag_hdr->next_header;
10699a2dd95SBruce Richardson 
10799a2dd95SBruce Richardson 	ip_frag_memmove(rte_pktmbuf_mtod_offset(m, char *, sizeof(*frag_hdr)),
10899a2dd95SBruce Richardson 			rte_pktmbuf_mtod(m, char*), move_len);
10999a2dd95SBruce Richardson 
11099a2dd95SBruce Richardson 	rte_pktmbuf_adj(m, sizeof(*frag_hdr));
11199a2dd95SBruce Richardson 
11299a2dd95SBruce Richardson 	return m;
11399a2dd95SBruce Richardson }
11499a2dd95SBruce Richardson 
11599a2dd95SBruce Richardson /*
11699a2dd95SBruce Richardson  * Process new mbuf with fragment of IPV6 datagram.
11799a2dd95SBruce Richardson  * Incoming mbuf should have its l2_len/l3_len fields setup correctly.
11899a2dd95SBruce Richardson  * @param tbl
11999a2dd95SBruce Richardson  *   Table where to lookup/add the fragmented packet.
12099a2dd95SBruce Richardson  * @param mb
12199a2dd95SBruce Richardson  *   Incoming mbuf with IPV6 fragment.
12299a2dd95SBruce Richardson  * @param tms
12399a2dd95SBruce Richardson  *   Fragment arrival timestamp.
12499a2dd95SBruce Richardson  * @param ip_hdr
12599a2dd95SBruce Richardson  *   Pointer to the IPV6 header.
12699a2dd95SBruce Richardson  * @param frag_hdr
12799a2dd95SBruce Richardson  *   Pointer to the IPV6 fragment extension header.
12899a2dd95SBruce Richardson  * @return
12999a2dd95SBruce Richardson  *   Pointer to mbuf for reassembled packet, or NULL if:
13099a2dd95SBruce Richardson  *   - an error occurred.
13199a2dd95SBruce Richardson  *   - not all fragments of the packet are collected yet.
13299a2dd95SBruce Richardson  */
13399a2dd95SBruce Richardson #define MORE_FRAGS(x) (((x) & 0x100) >> 8)
13499a2dd95SBruce Richardson #define FRAG_OFFSET(x) (rte_cpu_to_be_16(x) >> 3)
13599a2dd95SBruce Richardson struct rte_mbuf *
13699a2dd95SBruce Richardson rte_ipv6_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
13799a2dd95SBruce Richardson 	struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
138b7fc82ecSKonstantin Ananyev 	struct rte_ipv6_hdr *ip_hdr, struct rte_ipv6_fragment_ext *frag_hdr)
13999a2dd95SBruce Richardson {
14099a2dd95SBruce Richardson 	struct ip_frag_pkt *fp;
14199a2dd95SBruce Richardson 	struct ip_frag_key key;
14299a2dd95SBruce Richardson 	uint16_t ip_ofs;
14399a2dd95SBruce Richardson 	int32_t ip_len;
14499a2dd95SBruce Richardson 	int32_t trim;
14599a2dd95SBruce Richardson 
146*89b5642dSRobin Jarry 	rte_memcpy(&key.src_dst[0], &ip_hdr->src_addr, 16);
147*89b5642dSRobin Jarry 	rte_memcpy(&key.src_dst[2], &ip_hdr->dst_addr, 16);
14899a2dd95SBruce Richardson 
14999a2dd95SBruce Richardson 	key.id = frag_hdr->id;
15099a2dd95SBruce Richardson 	key.key_len = IPV6_KEYLEN;
15199a2dd95SBruce Richardson 
15299a2dd95SBruce Richardson 	ip_ofs = FRAG_OFFSET(frag_hdr->frag_data) * 8;
15399a2dd95SBruce Richardson 
15499a2dd95SBruce Richardson 	/*
15599a2dd95SBruce Richardson 	 * as per RFC2460, payload length contains all extension headers
15699a2dd95SBruce Richardson 	 * as well.
15799a2dd95SBruce Richardson 	 * since we don't support anything but frag headers,
15899a2dd95SBruce Richardson 	 * this is what we remove from the payload len.
15999a2dd95SBruce Richardson 	 */
16099a2dd95SBruce Richardson 	ip_len = rte_be_to_cpu_16(ip_hdr->payload_len) - sizeof(*frag_hdr);
16199a2dd95SBruce Richardson 	trim = mb->pkt_len - (ip_len + mb->l3_len + mb->l2_len);
16299a2dd95SBruce Richardson 
16399a2dd95SBruce Richardson 	IP_FRAG_LOG(DEBUG, "%s:%d:\n"
16499a2dd95SBruce Richardson 		"mbuf: %p, tms: %" PRIu64
16599a2dd95SBruce Richardson 		", key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
16699a2dd95SBruce Richardson 		"ofs: %u, len: %d, padding: %d, flags: %#x\n"
16799a2dd95SBruce Richardson 		"tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
16899a2dd95SBruce Richardson 		"max_entries: %u, use_entries: %u\n\n",
16999a2dd95SBruce Richardson 		__func__, __LINE__,
17099a2dd95SBruce Richardson 		mb, tms, IPv6_KEY_BYTES(key.src_dst), key.id, ip_ofs, ip_len,
17199a2dd95SBruce Richardson 		trim, RTE_IPV6_GET_MF(frag_hdr->frag_data),
17299a2dd95SBruce Richardson 		tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
17399a2dd95SBruce Richardson 		tbl->use_entries);
17499a2dd95SBruce Richardson 
17599a2dd95SBruce Richardson 	/* check that fragment length is greater then zero. */
17699a2dd95SBruce Richardson 	if (ip_len <= 0) {
17799a2dd95SBruce Richardson 		IP_FRAG_MBUF2DR(dr, mb);
17899a2dd95SBruce Richardson 		return NULL;
17999a2dd95SBruce Richardson 	}
18099a2dd95SBruce Richardson 
18199a2dd95SBruce Richardson 	if (unlikely(trim > 0))
18299a2dd95SBruce Richardson 		rte_pktmbuf_trim(mb, trim);
18399a2dd95SBruce Richardson 
18499a2dd95SBruce Richardson 	/* try to find/add entry into the fragment's table. */
18599a2dd95SBruce Richardson 	fp = ip_frag_find(tbl, dr, &key, tms);
18699a2dd95SBruce Richardson 	if (fp == NULL) {
18799a2dd95SBruce Richardson 		IP_FRAG_MBUF2DR(dr, mb);
18899a2dd95SBruce Richardson 		return NULL;
18999a2dd95SBruce Richardson 	}
19099a2dd95SBruce Richardson 
19199a2dd95SBruce Richardson 	IP_FRAG_LOG(DEBUG, "%s:%d:\n"
19299a2dd95SBruce Richardson 		"tbl: %p, max_entries: %u, use_entries: %u\n"
19399a2dd95SBruce Richardson 		"ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
19499a2dd95SBruce Richardson 		", total_size: %u, frag_size: %u, last_idx: %u\n\n",
19599a2dd95SBruce Richardson 		__func__, __LINE__,
19699a2dd95SBruce Richardson 		tbl, tbl->max_entries, tbl->use_entries,
19799a2dd95SBruce Richardson 		fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
19899a2dd95SBruce Richardson 		fp->total_size, fp->frag_size, fp->last_idx);
19999a2dd95SBruce Richardson 
20099a2dd95SBruce Richardson 
20199a2dd95SBruce Richardson 	/* process the fragmented packet. */
20299a2dd95SBruce Richardson 	mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len,
20399a2dd95SBruce Richardson 			MORE_FRAGS(frag_hdr->frag_data));
20499a2dd95SBruce Richardson 	ip_frag_inuse(tbl, fp);
20599a2dd95SBruce Richardson 
20699a2dd95SBruce Richardson 	IP_FRAG_LOG(DEBUG, "%s:%d:\n"
20799a2dd95SBruce Richardson 		"mbuf: %p\n"
20899a2dd95SBruce Richardson 		"tbl: %p, max_entries: %u, use_entries: %u\n"
20999a2dd95SBruce Richardson 		"ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64
21099a2dd95SBruce Richardson 		", total_size: %u, frag_size: %u, last_idx: %u\n\n",
21199a2dd95SBruce Richardson 		__func__, __LINE__, mb,
21299a2dd95SBruce Richardson 		tbl, tbl->max_entries, tbl->use_entries,
21399a2dd95SBruce Richardson 		fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id, fp->start,
21499a2dd95SBruce Richardson 		fp->total_size, fp->frag_size, fp->last_idx);
21599a2dd95SBruce Richardson 
21699a2dd95SBruce Richardson 	return mb;
21799a2dd95SBruce Richardson }
218