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-2019 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 1.9.6
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 //Reassembly algorithm tick interval
46 #ifndef IPV4_FRAG_TICK_INTERVAL
47  #define IPV4_FRAG_TICK_INTERVAL 1000
48 #elif (IPV4_FRAG_TICK_INTERVAL < 10)
49  #error IPV4_FRAG_TICK_INTERVAL parameter is not valid
50 #endif
51 
52 //Maximum number of fragmented packets the host will accept
53 //and hold in the reassembly queue simultaneously
54 #ifndef IPV4_MAX_FRAG_DATAGRAMS
55  #define IPV4_MAX_FRAG_DATAGRAMS 4
56 #elif (IPV4_MAX_FRAG_DATAGRAMS < 1)
57  #error IPV4_MAX_FRAG_DATAGRAMS parameter is not valid
58 #endif
59 
60 //Maximum datagram size the host will accept when reassembling fragments
61 #ifndef IPV4_MAX_FRAG_DATAGRAM_SIZE
62  #define IPV4_MAX_FRAG_DATAGRAM_SIZE 8192
63 #elif (IPV4_MAX_FRAG_DATAGRAM_SIZE < 576)
64  #error IPV4_MAX_FRAG_DATAGRAM_SIZE parameter is not valid
65 #endif
66 
67 //Maximum time an IPv4 fragment can spend waiting to be reassembled
68 #ifndef IPV4_FRAG_TIME_TO_LIVE
69  #define IPV4_FRAG_TIME_TO_LIVE 15000
70 #elif (IPV4_FRAG_TIME_TO_LIVE < 1000)
71  #error IPV4_FRAG_TIME_TO_LIVE parameter is not valid
72 #endif
73 
74 //Infinity is implemented by a very large integer
75 #define IPV4_INFINITY 0xFFFF
76 
77 //C++ guard
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 
83 //CodeWarrior or Win32 compiler?
84 #if defined(__CWCC__) || defined(_WIN32)
85  #pragma pack(push, 1)
86 #endif
87 
88 
89 /**
90  * @brief Hole descriptor
91  **/
92 
93 typedef __start_packed struct
94 {
95  uint16_t first;
96  uint16_t last;
97  uint16_t next;
99 
100 
101 //CodeWarrior or Win32 compiler?
102 #if defined(__CWCC__) || defined(_WIN32)
103  #pragma pack(pop)
104 #endif
105 
106 
107 /**
108  * @brief Reassembly buffer
109  **/
110 
111 typedef struct
112 {
117 
118 
119 /**
120  * @brief Fragmented packet descriptor
121  **/
122 
123 typedef struct
124 {
125  systime_t timestamp; ///<Time at which the first fragment was received
126  size_t headerLength; ///<Length of the header
127  size_t dataLen; ///<Length of the payload
128  uint16_t firstHole; ///<Index of the first hole
129  Ipv4ReassemblyBuffer buffer; ///<Buffer containing the reassembled datagram
130 } Ipv4FragDesc;
131 
132 
133 //Tick counter to handle periodic operations
135 
136 //IPv4 datagram fragmentation and reassembly
138  Ipv4PseudoHeader *pseudoHeader, uint16_t id, const NetBuffer *payload,
139  size_t payloadOffset, uint_t flags);
140 
141 void ipv4ReassembleDatagram(NetInterface *interface,
142  const Ipv4Header *packet, size_t length);
143 
144 void ipv4FragTick(NetInterface *interface);
145 
147  const Ipv4Header *packet);
148 
149 void ipv4FlushFragQueue(NetInterface *interface);
150 
151 Ipv4HoleDesc *ipv4FindHole(Ipv4FragDesc *frag, uint16_t offset);
152 void ipv4DumpHoleList(Ipv4FragDesc *frag);
153 
154 //C++ guard
155 #ifdef __cplusplus
156 }
157 #endif
158 
159 #endif
systime_t timestamp
Time at which the first fragment was received.
Definition: ipv4_frag.h:125
uint8_t length
Definition: dtls_misc.h:149
#define Ipv4Header
Definition: ipv4.h:36
uint16_t first
Definition: ipv4_frag.h:95
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
Ipv4FragDesc * ipv4SearchFragQueue(NetInterface *interface, const Ipv4Header *packet)
Search for a matching datagram in the reassembly queue.
Definition: ipv4_frag.c:517
uint16_t last
Definition: ipv4_frag.h:96
Reassembly buffer.
Definition: ipv4_frag.h:111
#define IPV4_MAX_FRAG_DATAGRAM_SIZE
Definition: ipv4_frag.h:62
#define N(size)
Definition: net_mem.h:64
void ipv4DumpHoleList(Ipv4FragDesc *frag)
Dump hole descriptor list.
Definition: ipv4_frag.c:653
uint16_t firstHole
Index of the first hole.
Definition: ipv4_frag.h:128
error_t
Error codes.
Definition: error.h:42
Ipv4HoleDesc * ipv4FindHole(Ipv4FragDesc *frag, uint16_t offset)
Retrieve hole descriptor.
Definition: ipv4_frag.c:641
Structure describing a chunk of data.
Definition: net_mem.h:76
size_t dataLen
Length of the payload.
Definition: ipv4_frag.h:127
error_t ipv4FragmentDatagram(NetInterface *interface, Ipv4PseudoHeader *pseudoHeader, uint16_t id, const NetBuffer *payload, size_t payloadOffset, uint_t flags)
Fragment an IPv4 datagram into smaller packets.
Definition: ipv4_frag.c:71
void ipv4FragTick(NetInterface *interface)
Fragment reassembly timeout handler.
Definition: ipv4_frag.c:449
#define NetInterface
Definition: net.h:36
__start_packed struct _Ipv4Header __end_packed
__start_packed struct @171 Ipv4HoleDesc
Hole descriptor.
#define Ipv4PseudoHeader
Definition: ipv4.h:39
size_t headerLength
Length of the header.
Definition: ipv4_frag.h:126
Fragmented packet descriptor.
Definition: ipv4_frag.h:123
uint8_t flags
Definition: tcp.h:314
void ipv4FlushFragQueue(NetInterface *interface)
Flush IPv4 reassembly queue.
Definition: ipv4_frag.c:620
void ipv4ReassembleDatagram(NetInterface *interface, const Ipv4Header *packet, size_t length)
IPv4 datagram reassembly algorithm.
Definition: ipv4_frag.c:179
IPv4 (Internet Protocol Version 4)
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint16_t next
Definition: ipv4_frag.h:97
Ipv4ReassemblyBuffer buffer
Buffer containing the reassembled datagram.
Definition: ipv4_frag.h:129
uint32_t systime_t
Definition: compiler_port.h:46
systime_t ipv4FragTickCounter
Definition: ipv4_frag.c:57