ipv4_frag.h
Go to the documentation of this file.
1 /**
2  * @file ipv4_frag.h
3  * @brief IPv4 fragmentation and reassembly
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _IPV4_FRAG_H
32 #define _IPV4_FRAG_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ipv4/ipv4.h"
37 
38 //IPv4 fragmentation support
39 #ifndef IPV4_FRAG_SUPPORT
40  #define IPV4_FRAG_SUPPORT ENABLED
41 #elif (IPV4_FRAG_SUPPORT != ENABLED && IPV4_FRAG_SUPPORT != DISABLED)
42  #error IPV4_FRAG_SUPPORT parameter is not valid
43 #endif
44 
45 //Support for overlapping fragments
46 #ifndef IPV4_OVERLAPPING_FRAG_SUPPORT
47  #define IPV4_OVERLAPPING_FRAG_SUPPORT ENABLED
48 #elif (IPV4_OVERLAPPING_FRAG_SUPPORT != ENABLED && IPV4_OVERLAPPING_FRAG_SUPPORT != DISABLED)
49  #error IPV4_OVERLAPPING_FRAG_SUPPORT parameter is not valid
50 #endif
51 
52 //Reassembly algorithm tick interval
53 #ifndef IPV4_FRAG_TICK_INTERVAL
54  #define IPV4_FRAG_TICK_INTERVAL 1000
55 #elif (IPV4_FRAG_TICK_INTERVAL < 10)
56  #error IPV4_FRAG_TICK_INTERVAL parameter is not valid
57 #endif
58 
59 //Maximum number of fragmented packets the host will accept
60 //and hold in the reassembly queue simultaneously
61 #ifndef IPV4_MAX_FRAG_DATAGRAMS
62  #define IPV4_MAX_FRAG_DATAGRAMS 4
63 #elif (IPV4_MAX_FRAG_DATAGRAMS < 1)
64  #error IPV4_MAX_FRAG_DATAGRAMS parameter is not valid
65 #endif
66 
67 //Maximum datagram size the host will accept when reassembling fragments
68 #ifndef IPV4_MAX_FRAG_DATAGRAM_SIZE
69  #define IPV4_MAX_FRAG_DATAGRAM_SIZE 8192
70 #elif (IPV4_MAX_FRAG_DATAGRAM_SIZE < 576)
71  #error IPV4_MAX_FRAG_DATAGRAM_SIZE parameter is not valid
72 #endif
73 
74 //Maximum time an IPv4 fragment can spend waiting to be reassembled
75 #ifndef IPV4_FRAG_TIME_TO_LIVE
76  #define IPV4_FRAG_TIME_TO_LIVE 15000
77 #elif (IPV4_FRAG_TIME_TO_LIVE < 1000)
78  #error IPV4_FRAG_TIME_TO_LIVE parameter is not valid
79 #endif
80 
81 //Infinity is implemented by a very large integer
82 #define IPV4_INFINITY 0xFFFF
83 
84 //C++ guard
85 #ifdef __cplusplus
86 extern "C" {
87 #endif
88 
89 
90 //CC-RX, CodeWarrior or Win32 compiler?
91 #if defined(__CCRX__)
92  #pragma pack
93 #elif defined(__CWCC__) || defined(_WIN32)
94  #pragma pack(push, 1)
95 #endif
96 
97 
98 /**
99  * @brief Hole descriptor
100  **/
101 
103 {
104  uint16_t first;
105  uint16_t last;
106  uint16_t next;
108 
109 
110 //CC-RX, CodeWarrior or Win32 compiler?
111 #if defined(__CCRX__)
112  #pragma unpack
113 #elif defined(__CWCC__) || defined(_WIN32)
114  #pragma pack(pop)
115 #endif
116 
117 
118 /**
119  * @brief Reassembly buffer
120  **/
121 
122 typedef struct
123 {
128 
129 
130 /**
131  * @brief Fragmented packet descriptor
132  **/
133 
134 typedef struct
135 {
136  systime_t timestamp; ///<Time at which the first fragment was received
137  size_t headerLength; ///<Length of the header
138  size_t dataLen; ///<Length of the payload
139  uint16_t firstHole; ///<Index of the first hole
140  Ipv4ReassemblyBuffer buffer; ///<Buffer containing the reassembled datagram
141 } Ipv4FragDesc;
142 
143 
144 //Tick counter to handle periodic operations
146 
147 //IPv4 datagram fragmentation and reassembly
149  const Ipv4PseudoHeader *pseudoHeader, uint16_t id, const NetBuffer *payload,
150  size_t payloadOffset, NetTxAncillary *ancillary);
151 
152 void ipv4ReassembleDatagram(NetInterface *interface, const Ipv4Header *packet,
153  size_t length, NetRxAncillary *ancillary);
154 
155 void ipv4FragTick(NetInterface *interface);
156 
158  const Ipv4Header *packet);
159 
160 void ipv4FlushFragQueue(NetInterface *interface);
161 
162 Ipv4HoleDesc *ipv4FindHole(Ipv4FragDesc *frag, uint16_t offset);
163 void ipv4DumpHoleList(Ipv4FragDesc *frag);
164 
165 //C++ guard
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif
systime_t timestamp
Time at which the first fragment was received.
Definition: ipv4_frag.h:136
#define Ipv4Header
Definition: ipv4.h:36
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
Ipv4FragDesc * ipv4SearchFragQueue(NetInterface *interface, const Ipv4Header *packet)
Search for a matching datagram in the reassembly queue.
Definition: ipv4_frag.c:557
uint16_t last
Definition: ipv4_frag.h:105
Reassembly buffer.
Definition: ipv4_frag.h:123
#define IPV4_MAX_FRAG_DATAGRAM_SIZE
Definition: ipv4_frag.h:69
#define N(size)
Definition: net_mem.h:64
void ipv4ReassembleDatagram(NetInterface *interface, const Ipv4Header *packet, size_t length, NetRxAncillary *ancillary)
IPv4 datagram reassembly algorithm.
Definition: ipv4_frag.c:184
void ipv4DumpHoleList(Ipv4FragDesc *frag)
Dump hole descriptor list.
Definition: ipv4_frag.c:694
typedef __packed_struct
Hole descriptor.
Definition: ipv4_frag.h:103
uint16_t firstHole
Index of the first hole.
Definition: ipv4_frag.h:139
error_t
Error codes.
Definition: error.h:43
Ipv4HoleDesc * ipv4FindHole(Ipv4FragDesc *frag, uint16_t offset)
Retrieve hole descriptor.
Definition: ipv4_frag.c:682
Structure describing a chunk of data.
Definition: net_mem.h:77
size_t dataLen
Length of the payload.
Definition: ipv4_frag.h:138
#define NetRxAncillary
Definition: net_misc.h:40
void ipv4FragTick(NetInterface *interface)
Fragment reassembly timeout handler.
Definition: ipv4_frag.c:488
#define NetInterface
Definition: net.h:36
#define NetTxAncillary
Definition: net_misc.h:36
#define Ipv4PseudoHeader
Definition: ipv4.h:39
uint8_t length
Definition: tcp.h:368
size_t headerLength
Length of the header.
Definition: ipv4_frag.h:137
uint32_t systime_t
System time.
Fragmented packet descriptor.
Definition: ipv4_frag.h:135
error_t ipv4FragmentDatagram(NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, uint16_t id, const NetBuffer *payload, size_t payloadOffset, NetTxAncillary *ancillary)
Fragment an IPv4 datagram into smaller packets.
Definition: ipv4_frag.c:72
void ipv4FlushFragQueue(NetInterface *interface)
Flush IPv4 reassembly queue.
Definition: ipv4_frag.c:661
uint8_t payload[]
Definition: ipv6.h:286
Ipv4HoleDesc
Definition: ipv4_frag.h:107
IPv4 (Internet Protocol Version 4)
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
uint16_t next
Definition: ipv4_frag.h:106
Ipv4ReassemblyBuffer buffer
Buffer containing the reassembled datagram.
Definition: ipv4_frag.h:140
systime_t ipv4FragTickCounter
Definition: ipv4_frag.c:57