Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_ipsec.h"
#include "nx_icmp.h"
#include "nx_ipv6.h"
...
...
_nx_ip_max_payload_size_find(NX_IP *, NXD_ADDRESS *, UINT, UINT, UINT, ULONG, ULONG *, ULONG *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocommon/src/nx_ip_max_payload_size_find.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Component */ /** */ /** Packet Pool Mangement (Packet) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_ip.h" #ifdef NX_IPSEC_ENABLE #include "nx_ipsec.h" #include "nx_icmp.h" /* ... */#endif /* NX_IPSEC_ENABLE */ #ifdef FEATURE_NX_IPV6 #include "nx_ipv6.h" #endif /* FEATURE_NX_IPV6 */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_ip_max_payload_size_find PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function finds the maximum payload size at application level */ /* that would not cause IPv4 or IPv6 fragmentation. It takes the */ /* layer 4 protocol (TCP vs. UDP), layer 3 protocol (IPv4 vs. IPv6) */ /* and IPsec into consideration, and reports back to values: */ /* (1) start_offset_ptr: This is the location where enough space */ /* for protocol header is reserved for TCP/UDP/IP/IPsec headers; */ /* (2) payload_length_ptr: the amount of data application may */ /* transfer without exceeding MTU. */ /* */ /* INPUT */ /* */ /* ip_ptr Pointer to IP instance */ /* dest_address Packet Destination Address */ /* if_index Interface index for ipv4, */ /* address index for ipv6. */ /* src_port Source port number, in host */ /* byte order. */ /* dest_port Destination port number, */ /* in host byte order. */ /* protocol Protocol type */ /* start_offset_ptr Pointer to the start of data */ /* for maximum packet payload. */ /* payload_length_ptr Pointer to the computed */ /* payload size that would not */ /* cause IP fragmentation. */ /* */ /* OUTPUT */ /* */ /* status Completion Code. */ /* */ /* CALLS */ /* */ /* _nx_ipsec_sa_egress_lookup The acutal function that */ /* performs the SA lookup */ /* (if IPsec is enable) */ /* */ /* CALLED BY */ /* */ /* Application */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_ip_max_payload_size_find(NX_IP *ip_ptr, NXD_ADDRESS *dest_address, UINT if_index, UINT src_port, UINT dest_port, ULONG protocol, ULONG *start_offset_ptr, ULONG *payload_length_ptr) { ULONG payload_length = 0; ULONG start_offset = 0; #if defined(NX_IPSEC_ENABLE) || defined(TX_ENABLE_EVENT_TRACE) NXD_ADDRESS src_address = {0}; #endif /* (NX_IPSEC_ENABLE) || defined(TX_ENABLE_EVENT_TRACE) */ #ifdef NX_IPSEC_ENABLE UINT block_size; NX_IPSEC_SA *sa = NX_NULL; UINT ret; ULONG data_offset;/* ... */ #endif /* NX_IPSEC_ENABLE */ #ifdef TX_ENABLE_EVENT_TRACE ULONG src_address_lsw = 0, dst_address_lsw = 0; #endif /* TX_ENABLE_EVENT_TRACE */ #ifndef NX_DISABLE_IPV4 /* First check the version. */ if (dest_address -> nxd_ip_version == NX_IP_VERSION_V4) { /* Computer header space needed for physical layer and IP layer. */ start_offset = NX_IPv4_PACKET; /* In this case, if_index indicate the interface_index. */ /* Check the validity of the if_index. */ if ((if_index >= NX_MAX_PHYSICAL_INTERFACES) || (!ip_ptr -> nx_ip_interface[if_index].nx_interface_ip_address)) { return(NX_INVALID_INTERFACE); }if ((if_index >= NX_MAX_PHYSICAL_INTERFACES) || (!ip_ptr -> nx_ip_interface[if_index].nx_interface_ip_address)) { ... } #if defined(NX_IPSEC_ENABLE) || defined(TX_ENABLE_EVENT_TRACE) /* Record the source address. */ src_address.nxd_ip_address.v4 = ip_ptr -> nx_ip_interface[if_index].nx_interface_ip_address; src_address.nxd_ip_version = NX_IP_VERSION_V4;/* ... */ #endif /* NX_IPSEC_ENABLE */ /* Get the mtu size. */ payload_length = ip_ptr -> nx_ip_interface[if_index].nx_interface_ip_mtu_size + NX_PHYSICAL_HEADER; }if (dest_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... } /* ... */#endif /* !NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if (dest_address -> nxd_ip_version == NX_IP_VERSION_V6) { /* Computer header space needed for physical layer and IP layer. */ start_offset = NX_IPv6_PACKET; /* In this case, if_index indicate the address_index. */ /* Check the validity of the if_index. */ if (if_index >= (NX_MAX_IPV6_ADDRESSES + NX_LOOPBACK_IPV6_ENABLED)) { return(NX_IP_ADDRESS_ERROR); }if (if_index >= (NX_MAX_IPV6_ADDRESSES + NX_LOOPBACK_IPV6_ENABLED)) { ... } #if defined(NX_IPSEC_ENABLE) || defined(TX_ENABLE_EVENT_TRACE) /* Record the source address. */ COPY_IPV6_ADDRESS(ip_ptr -> nx_ipv6_address[if_index].nxd_ipv6_address, src_address.nxd_ip_address.v6); src_address.nxd_ip_version = NX_IP_VERSION_V6;/* ... */ #endif /* NX_IPSEC_ENABLE */ /* Get the mtu size. */ payload_length = ip_ptr -> nx_ipv6_address[if_index].nxd_ipv6_address_attached -> nx_interface_ip_mtu_size + NX_PHYSICAL_HEADER; }if (dest_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ if (protocol == NX_PROTOCOL_TCP) { /* Add TCP header length. */ start_offset += 20; }if (protocol == NX_PROTOCOL_TCP) { ... } else if (protocol == NX_PROTOCOL_UDP) { /* Add UDP header length. */ start_offset += 8; }else if (protocol == NX_PROTOCOL_UDP) { ... } /* Now compute the max protocol payload size that would not lead to IP fragmentation e.g. keeping payload length at or below the MTU size. *//* ... */ payload_length -= start_offset; #ifdef NX_IPSEC_ENABLE ret = NX_IPSEC_TRAFFIC_BYPASS; if (ip_ptr -> nx_ip_packet_egress_sa_lookup) { #ifdef FEATURE_NX_IPV6 if (src_address.nxd_ip_version == NX_IP_VERSION_V4) { #endif /* FEATURE_NX_IPV6 */ ret = ip_ptr -> nx_ip_packet_egress_sa_lookup(ip_ptr, &src_address, dest_address, (UCHAR)protocol, src_port, dest_port, &data_offset, (VOID **)&sa, (NX_ICMP_ECHO_REQUEST_TYPE << 8)); #ifdef FEATURE_NX_IPV6 }if (src_address.nxd_ip_version == NX_IP_VERSION_V4) { ... } else { ret = ip_ptr -> nx_ip_packet_egress_sa_lookup(ip_ptr, &src_address, dest_address, (UCHAR)protocol, src_port, dest_port, &data_offset, (VOID **)&sa, (NX_ICMPV6_ECHO_REQUEST_TYPE << 8)); }else { ... } #endif /* FEATURE_NX_IPV6 */ }if (ip_ptr -> nx_ip_packet_egress_sa_lookup) { ... } if (ret == NX_IPSEC_TRAFFIC_PROTECT) { /* Add IPsec protocol header layer. */ start_offset += sa -> nx_ipsec_sa_head_encap_size; payload_length -= sa -> nx_ipsec_sa_head_encap_size; /* Align the payload_length to the SA algorithm block size. */ if (sa -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_ENCAP_SECURITY) { /* The next block computes the authentication trailer block size in the ESP combined mode. */ /* Payload length should exclude this trailer size. */ if (sa -> nx_ipsec_sa_integrity_method != NX_NULL) { block_size = sa -> nx_ipsec_sa_integrity_method -> nx_crypto_block_size_in_bytes; payload_length -= block_size; }if (sa -> nx_ipsec_sa_integrity_method != NX_NULL) { ... } /* Compute the the actual data payload size. */ if ((sa -> nx_ipsec_sa_encryption_method != NX_NULL) && (sa -> nx_ipsec_sa_encryption_method -> nx_crypto_algorithm != NX_CRYPTO_NONE)) { /* Assume block size is 4, for ENCRYPTION_NULL algorithm. */ block_size = 4; if (sa -> nx_ipsec_sa_encryption_method -> nx_crypto_algorithm != NX_CRYPTO_ENCRYPTION_NULL) { block_size = sa -> nx_ipsec_sa_encryption_method -> nx_crypto_block_size_in_bytes; }if (sa -> nx_ipsec_sa_encryption_method -> nx_crypto_algorithm != NX_CRYPTO_ENCRYPTION_NULL) { ... } /* Round the payload size to multiple of block size. */ payload_length = payload_length / block_size; payload_length = payload_length * block_size; payload_length -= NX_IPSEC_ESP_TRAIL_LEN; }if ((sa -> nx_ipsec_sa_encryption_method != NX_NULL) && (sa -> nx_ipsec_sa_encryption_method -> nx_crypto_algorithm != NX_CRYPTO_NONE)) { ... } }if (sa -> nx_ipsec_sa_protocol == NX_PROTOCOL_NEXT_HEADER_ENCAP_SECURITY) { ... } /* No need to update payload_length if sa_protocol is AH. */ }if (ret == NX_IPSEC_TRAFFIC_PROTECT) { ... } /* ... */#else NX_PARAMETER_NOT_USED(src_port); NX_PARAMETER_NOT_USED(dest_port);/* ... */ #endif /* NX_IPSEC_ENABLE */ /* Return payload_length and start_offset values. */ if (payload_length_ptr) { *payload_length_ptr = payload_length; }if (payload_length_ptr) { ... } if (start_offset_ptr) { *start_offset_ptr = start_offset; }if (start_offset_ptr) { ... } #ifdef TX_ENABLE_EVENT_TRACE #ifndef NX_DISABLE_IPV4 if (dest_address -> nxd_ip_version == NX_IP_VERSION_V4) { src_address_lsw = src_address.nxd_ip_address.v4; dst_address_lsw = dest_address -> nxd_ip_address.v4; }if (dest_address -> nxd_ip_version == NX_IP_VERSION_V4) { ... } /* ... */#endif /* NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if (dest_address -> nxd_ip_version == NX_IP_VERSION_V6) { src_address_lsw = src_address.nxd_ip_address.v6[3]; dst_address_lsw = dest_address -> nxd_ip_address.v6[3]; }if (dest_address -> nxd_ip_version == NX_IP_VERSION_V6) { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NXD_TRACE_IP_MAX_PAYLOAD_SIZE_FIND, src_address_lsw, dst_address_lsw, payload_length, start_offset, NX_TRACE_PACKET_EVENTS, 0, 0); /* ... */ #endif /* TX_ENABLE_EVENT_TRACE */ return(NX_SUCCESS); }{ ... }