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_debug.h> 899a2dd95SBruce Richardson 999a2dd95SBruce Richardson #include "ip_frag_common.h" 1099a2dd95SBruce Richardson 1199a2dd95SBruce Richardson /* 1299a2dd95SBruce Richardson * Reassemble fragments into one packet. 1399a2dd95SBruce Richardson */ 1499a2dd95SBruce Richardson struct rte_mbuf * 1599a2dd95SBruce Richardson ipv4_frag_reassemble(struct ip_frag_pkt *fp) 1699a2dd95SBruce Richardson { 1799a2dd95SBruce Richardson struct rte_ipv4_hdr *ip_hdr; 1899a2dd95SBruce Richardson struct rte_mbuf *m, *prev; 1999a2dd95SBruce Richardson uint32_t i, n, ofs, first_len; 2099a2dd95SBruce Richardson uint32_t curr_idx = 0; 2199a2dd95SBruce Richardson 2299a2dd95SBruce Richardson first_len = fp->frags[IP_FIRST_FRAG_IDX].len; 2399a2dd95SBruce Richardson n = fp->last_idx - 1; 2499a2dd95SBruce Richardson 2599a2dd95SBruce Richardson /*start from the last fragment. */ 2699a2dd95SBruce Richardson m = fp->frags[IP_LAST_FRAG_IDX].mb; 2799a2dd95SBruce Richardson ofs = fp->frags[IP_LAST_FRAG_IDX].ofs; 2899a2dd95SBruce Richardson curr_idx = IP_LAST_FRAG_IDX; 2999a2dd95SBruce Richardson 3099a2dd95SBruce Richardson while (ofs != first_len) { 3199a2dd95SBruce Richardson 3299a2dd95SBruce Richardson prev = m; 3399a2dd95SBruce Richardson 3499a2dd95SBruce Richardson for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) { 3599a2dd95SBruce Richardson 3699a2dd95SBruce Richardson /* previous fragment found. */ 3799a2dd95SBruce Richardson if(fp->frags[i].ofs + fp->frags[i].len == ofs) { 3899a2dd95SBruce Richardson 3999a2dd95SBruce Richardson RTE_ASSERT(curr_idx != i); 4099a2dd95SBruce Richardson 4199a2dd95SBruce Richardson /* adjust start of the last fragment data. */ 4299a2dd95SBruce Richardson rte_pktmbuf_adj(m, 4399a2dd95SBruce Richardson (uint16_t)(m->l2_len + m->l3_len)); 4499a2dd95SBruce Richardson rte_pktmbuf_chain(fp->frags[i].mb, m); 4599a2dd95SBruce Richardson 4699a2dd95SBruce Richardson /* this mbuf should not be accessed directly */ 4799a2dd95SBruce Richardson fp->frags[curr_idx].mb = NULL; 4899a2dd95SBruce Richardson curr_idx = i; 4999a2dd95SBruce Richardson 5099a2dd95SBruce Richardson /* update our last fragment and offset. */ 5199a2dd95SBruce Richardson m = fp->frags[i].mb; 5299a2dd95SBruce Richardson ofs = fp->frags[i].ofs; 5399a2dd95SBruce Richardson } 5499a2dd95SBruce Richardson } 5599a2dd95SBruce Richardson 5699a2dd95SBruce Richardson /* error - hole in the packet. */ 5799a2dd95SBruce Richardson if (m == prev) { 5899a2dd95SBruce Richardson return NULL; 5999a2dd95SBruce Richardson } 6099a2dd95SBruce Richardson } 6199a2dd95SBruce Richardson 6299a2dd95SBruce Richardson /* chain with the first fragment. */ 6399a2dd95SBruce Richardson rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len)); 6499a2dd95SBruce Richardson rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m); 6599a2dd95SBruce Richardson fp->frags[curr_idx].mb = NULL; 6699a2dd95SBruce Richardson m = fp->frags[IP_FIRST_FRAG_IDX].mb; 6799a2dd95SBruce Richardson fp->frags[IP_FIRST_FRAG_IDX].mb = NULL; 6899a2dd95SBruce Richardson 6999a2dd95SBruce Richardson /* update ipv4 header for the reassembled packet */ 7099a2dd95SBruce Richardson ip_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *, m->l2_len); 7199a2dd95SBruce Richardson 7299a2dd95SBruce Richardson ip_hdr->total_length = rte_cpu_to_be_16((uint16_t)(fp->total_size + 7399a2dd95SBruce Richardson m->l3_len)); 7499a2dd95SBruce Richardson ip_hdr->fragment_offset = (uint16_t)(ip_hdr->fragment_offset & 7599a2dd95SBruce Richardson rte_cpu_to_be_16(RTE_IPV4_HDR_DF_FLAG)); 7699a2dd95SBruce Richardson ip_hdr->hdr_checksum = 0; 7799a2dd95SBruce Richardson 7899a2dd95SBruce Richardson return m; 7999a2dd95SBruce Richardson } 8099a2dd95SBruce Richardson 8199a2dd95SBruce Richardson /* 8299a2dd95SBruce Richardson * Process new mbuf with fragment of IPV4 packet. 834a6672c2SStephen Hemminger * Incoming mbuf should have it's l2_len/l3_len fields setup correctly. 8499a2dd95SBruce Richardson * @param tbl 8599a2dd95SBruce Richardson * Table where to lookup/add the fragmented packet. 8699a2dd95SBruce Richardson * @param mb 8799a2dd95SBruce Richardson * Incoming mbuf with IPV4 fragment. 8899a2dd95SBruce Richardson * @param tms 8999a2dd95SBruce Richardson * Fragment arrival timestamp. 9099a2dd95SBruce Richardson * @param ip_hdr 9199a2dd95SBruce Richardson * Pointer to the IPV4 header inside the fragment. 9299a2dd95SBruce Richardson * @return 9399a2dd95SBruce Richardson * Pointer to mbuf for reassembled packet, or NULL if: 9499a2dd95SBruce Richardson * - an error occurred. 9599a2dd95SBruce Richardson * - not all fragments of the packet are collected yet. 9699a2dd95SBruce Richardson */ 9799a2dd95SBruce Richardson struct rte_mbuf * 9899a2dd95SBruce Richardson rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl, 9999a2dd95SBruce Richardson struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms, 10099a2dd95SBruce Richardson struct rte_ipv4_hdr *ip_hdr) 10199a2dd95SBruce Richardson { 10299a2dd95SBruce Richardson struct ip_frag_pkt *fp; 10399a2dd95SBruce Richardson struct ip_frag_key key; 10499a2dd95SBruce Richardson uint16_t flag_offset, ip_ofs, ip_flag; 10599a2dd95SBruce Richardson int32_t ip_len; 10699a2dd95SBruce Richardson int32_t trim; 10799a2dd95SBruce Richardson 10899a2dd95SBruce Richardson flag_offset = rte_be_to_cpu_16(ip_hdr->fragment_offset); 10999a2dd95SBruce Richardson ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK); 11099a2dd95SBruce Richardson ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG); 11199a2dd95SBruce Richardson 11299a2dd95SBruce Richardson /* use first 8 bytes only */ 113*5763d240SBruce Richardson memcpy(&key.src_dst[0], &ip_hdr->src_addr, 8); 11499a2dd95SBruce Richardson key.id = ip_hdr->packet_id; 11599a2dd95SBruce Richardson key.key_len = IPV4_KEYLEN; 11699a2dd95SBruce Richardson 11799a2dd95SBruce Richardson ip_ofs *= RTE_IPV4_HDR_OFFSET_UNITS; 11899a2dd95SBruce Richardson ip_len = rte_be_to_cpu_16(ip_hdr->total_length) - mb->l3_len; 11999a2dd95SBruce Richardson trim = mb->pkt_len - (ip_len + mb->l3_len + mb->l2_len); 12099a2dd95SBruce Richardson 12199a2dd95SBruce Richardson IP_FRAG_LOG(DEBUG, "%s:%d:\n" 12299a2dd95SBruce Richardson "mbuf: %p, tms: %" PRIu64 ", key: <%" PRIx64 ", %#x>" 12399a2dd95SBruce Richardson "ofs: %u, len: %d, padding: %d, flags: %#x\n" 12499a2dd95SBruce Richardson "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, " 12599a2dd95SBruce Richardson "max_entries: %u, use_entries: %u\n\n", 12699a2dd95SBruce Richardson __func__, __LINE__, 12799a2dd95SBruce Richardson mb, tms, key.src_dst[0], key.id, ip_ofs, ip_len, trim, ip_flag, 12899a2dd95SBruce Richardson tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries, 12999a2dd95SBruce Richardson tbl->use_entries); 13099a2dd95SBruce Richardson 13199a2dd95SBruce Richardson /* check that fragment length is greater then zero. */ 13299a2dd95SBruce Richardson if (ip_len <= 0) { 13399a2dd95SBruce Richardson IP_FRAG_MBUF2DR(dr, mb); 13499a2dd95SBruce Richardson return NULL; 13599a2dd95SBruce Richardson } 13699a2dd95SBruce Richardson 13799a2dd95SBruce Richardson if (unlikely(trim > 0)) 13899a2dd95SBruce Richardson rte_pktmbuf_trim(mb, trim); 13999a2dd95SBruce Richardson 14099a2dd95SBruce Richardson /* try to find/add entry into the fragment's table. */ 14199a2dd95SBruce Richardson if ((fp = ip_frag_find(tbl, dr, &key, tms)) == NULL) { 14299a2dd95SBruce Richardson IP_FRAG_MBUF2DR(dr, mb); 14399a2dd95SBruce Richardson return NULL; 14499a2dd95SBruce Richardson } 14599a2dd95SBruce Richardson 14699a2dd95SBruce Richardson IP_FRAG_LOG(DEBUG, "%s:%d:\n" 14799a2dd95SBruce Richardson "tbl: %p, max_entries: %u, use_entries: %u\n" 14899a2dd95SBruce Richardson "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64 14999a2dd95SBruce Richardson ", total_size: %u, frag_size: %u, last_idx: %u\n\n", 15099a2dd95SBruce Richardson __func__, __LINE__, 15199a2dd95SBruce Richardson tbl, tbl->max_entries, tbl->use_entries, 15299a2dd95SBruce Richardson fp, fp->key.src_dst[0], fp->key.id, fp->start, 15399a2dd95SBruce Richardson fp->total_size, fp->frag_size, fp->last_idx); 15499a2dd95SBruce Richardson 15599a2dd95SBruce Richardson 15699a2dd95SBruce Richardson /* process the fragmented packet. */ 15799a2dd95SBruce Richardson mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len, ip_flag); 15899a2dd95SBruce Richardson ip_frag_inuse(tbl, fp); 15999a2dd95SBruce Richardson 16099a2dd95SBruce Richardson IP_FRAG_LOG(DEBUG, "%s:%d:\n" 16199a2dd95SBruce Richardson "mbuf: %p\n" 16299a2dd95SBruce Richardson "tbl: %p, max_entries: %u, use_entries: %u\n" 16399a2dd95SBruce Richardson "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64 16499a2dd95SBruce Richardson ", total_size: %u, frag_size: %u, last_idx: %u\n\n", 16599a2dd95SBruce Richardson __func__, __LINE__, mb, 16699a2dd95SBruce Richardson tbl, tbl->max_entries, tbl->use_entries, 16799a2dd95SBruce Richardson fp, fp->key.src_dst[0], fp->key.id, fp->start, 16899a2dd95SBruce Richardson fp->total_size, fp->frag_size, fp->last_idx); 16999a2dd95SBruce Richardson 17099a2dd95SBruce Richardson return mb; 17199a2dd95SBruce Richardson } 172