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_packet.h"
#include "nx_ipsec.h"
...
_nx_ip_packet_send(NX_IP *, NX_PACKET *, ULONG, ULONG, ULONG, ULONG, ULONG, ULONG)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocommon/src/nx_ip_packet_send.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** Internet Protocol (IP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_ip.h" #include "nx_packet.h" #ifdef NX_IPSEC_ENABLE #include "nx_ipsec.h" #endif /* NX_IPSEC_ENABLE */ #ifndef NX_DISABLE_IPV4... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_ip_packet_send PORTABLE C */ /* 6.1.8 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function prepends an IP header and sends an IP packet to the */ /* appropriate link driver. */ /* */ /* INPUT */ /* */ /* ip_ptr Pointer to IP control block */ /* packet_ptr Pointer to packet to send */ /* destination_ip Destination IP address */ /* type_of_service Type of service for packet */ /* time_to_live Time to live value for packet */ /* protocol Protocol being encapsulated */ /* fragment Don't fragment bit */ /* next_hop_address Next Hop address */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_ip_checksum_compute Compute IP checksum */ /* _nx_ip_header_add Add the IP header */ /* _nx_ip_route_find Find suitable outgoing */ /* _nx_ip_driver_packet_send Send the IP packet */ /* _nx_packet_transmit_release Release transmit packet */ /* */ /* CALLED BY */ /* */ /* NetX Source Code */ /* */ /* 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 */ /* 08-02-2021 Yuxin Zhou Modified comment(s), and */ /* supported TCP/IP offload, */ /* added new ip filter, */ /* resulting in version 6.1.8 */ /* */... /**************************************************************************/ VOID _nx_ip_packet_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr, ULONG destination_ip, ULONG type_of_service, ULONG time_to_live, ULONG protocol, ULONG fragment, ULONG next_hop_address) { #ifdef NX_IPSEC_ENABLE UINT status = 0; ULONG payload_size; USHORT value; UCHAR is_hw_processed = NX_FALSE; NX_IPV4_HEADER *ip_header_ptr; ULONG checksum; ULONG val;/* ... */ #endif /* NX_IPSEC_ENABLE */ /* Add debug information. */ NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr); #ifndef NX_DISABLE_IP_INFO /* Increment the total send requests counter. */ ip_ptr -> nx_ip_total_packet_send_requests++;/* ... */ #endif /* Make sure the packet interface is set. */ if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr == NX_NULL) { #ifndef NX_DISABLE_IP_INFO /* Increment the IP invalid packet error. */ ip_ptr -> nx_ip_invalid_transmit_packets++;/* ... */ #endif /* !NX_DISABLE_IP_INFO */ /* Prepend the IP header to the packet. First, make room for the IP header. */ packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV4_HEADER); /* Increase the packet length. */ packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length + (ULONG)sizeof(NX_IPV4_HEADER); /* Release the packet. */ _nx_packet_transmit_release(packet_ptr); /* Return... nothing more can be done! */ return; }if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr == NX_NULL) { ... } #ifdef NX_ENABLE_TCPIP_OFFLOAD if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD) { #ifndef NX_DISABLE_IP_INFO /* Increment the IP invalid packet error. */ ip_ptr -> nx_ip_invalid_transmit_packets++;/* ... */ #endif /* Ignore sending all packets for TCP/IP offload. Release the packet. */ _nx_packet_transmit_release(packet_ptr); /* Return... nothing more can be done! */ return; }if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_TCPIP_OFFLOAD) { ... } /* ... */#endif /* NX_ENABLE_TCPIP_OFFLOAD */ #ifdef NX_IPSEC_ENABLE /* Check if this packet is continued after HW crypto engine. */ if (packet_ptr -> nx_packet_ipsec_sa_ptr && ((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TRANSPORT_MODE && (packet_ptr -> nx_packet_ipsec_state == NX_IPSEC_AH_PACKET || packet_ptr -> nx_packet_ipsec_state == NX_IPSEC_ESP_PACKET)) { is_hw_processed = NX_TRUE; }if (packet_ptr -> nx_packet_ipsec_sa_ptr && ((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TRANSPORT_MODE && (packet_ptr -> nx_packet_ipsec_state == NX_IPSEC_AH_PACKET || packet_ptr -> nx_packet_ipsec_state == NX_IPSEC_ESP_PACKET)) { ... } /* Process this packet in IPsec transport mode? */ if (packet_ptr -> nx_packet_ipsec_sa_ptr && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_ESP_PACKET && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_AH_PACKET && ((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TRANSPORT_MODE) { /* Perform IPSec processing and insert IPsec headers before the IP header. */ /* Notice that for TCP transmission, a new packet is used to store the encrypted data, while the original TCP packet is put back on to the transmitted queue. *//* ... */ payload_size = packet_ptr -> nx_packet_length; status = _nx_ipsec_ip_output_packet_process(ip_ptr, &packet_ptr, (protocol >> 16), payload_size, &payload_size); if ((status != NX_SUCCESS) && (status != NX_IPSEC_HW_PENDING)) { /* Install an area for the IP header. This is required by the nx_packet_transmit_release. */ packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV4_HEADER); /* Increase the packet length for the IP header. */ packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length + sizeof(NX_IPV4_HEADER); /* IPsec output packet process failed. */ /* Release the packet. */ _nx_packet_transmit_release(packet_ptr); return; }if ((status != NX_SUCCESS) && (status != NX_IPSEC_HW_PENDING)) { ... } /* Now set the IPSec protocol. */ protocol = (ULONG)((((NX_IPSEC_SA *)packet_ptr -> nx_packet_ipsec_sa_ptr) -> nx_ipsec_sa_protocol) << 16); /* Set the DF bit. Transport mode SAs have been defined to not carry fragments (IPv4 or IPv6),RFC 4301 page 66&88..*//* ... */ fragment = NX_DONT_FRAGMENT; }if (packet_ptr -> nx_packet_ipsec_sa_ptr && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_ESP_PACKET && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_AH_PACKET && ((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TRANSPORT_MODE) { ... } /* ... */#endif /* NX_IPSEC_ENABLE */ /* If the packet is processed by HW crypto engine, do not add IP header. */ #ifdef NX_IPSEC_ENABLE if (!is_hw_processed) #endif /* NX_IPSEC_ENABLE */ { /* Add the IP Header to the packet. */ _nx_ip_header_add(ip_ptr, packet_ptr, packet_ptr -> nx_packet_ip_interface -> nx_interface_ip_address, destination_ip, type_of_service, time_to_live, protocol, fragment); #ifdef NX_ENABLE_IP_PACKET_FILTER /* Check if the IP packet filter is set. */ if (ip_ptr -> nx_ip_packet_filter) { /* Yes, call the IP packet filter routine. */ if (ip_ptr -> nx_ip_packet_filter((VOID *)(packet_ptr -> nx_packet_prepend_ptr), NX_IP_PACKET_OUT) != NX_SUCCESS) { /* Drop the packet. */ _nx_packet_transmit_release(packet_ptr); return; }if (ip_ptr -> nx_ip_packet_filter((VOID *)(packet_ptr -> nx_packet_prepend_ptr), NX_IP_PACKET_OUT) != NX_SUCCESS) { ... } }if (ip_ptr -> nx_ip_packet_filter) { ... } /* Check if the IP packet filter extended is set. */ if (ip_ptr -> nx_ip_packet_filter_extended) { /* Yes, call the IP packet filter extended routine. */ if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_OUT) != NX_SUCCESS) { /* Drop the packet. */ _nx_packet_transmit_release(packet_ptr); return; }if (ip_ptr -> nx_ip_packet_filter_extended(ip_ptr, packet_ptr, NX_IP_PACKET_OUT) != NX_SUCCESS) { ... } }if (ip_ptr -> nx_ip_packet_filter_extended) { ... } /* ... */#endif /* NX_ENABLE_IP_PACKET_FILTER */ ...} /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_IP_SEND, ip_ptr, destination_ip, packet_ptr, packet_ptr -> nx_packet_length, NX_TRACE_INTERNAL_EVENTS, 0, 0); #ifdef NX_IPSEC_ENABLE if (is_hw_processed) { /* Destination IP is unknow after HW crypto engine process. */ /* Get destination IP from IP header. */ /* Setup the IP header pointer. */ ip_header_ptr = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_prepend_ptr; /* Fix payload size. */ value = (USHORT)(packet_ptr -> nx_packet_length); NX_CHANGE_USHORT_ENDIAN(value); /* First clear payload_size field. */ ip_header_ptr -> nx_ip_header_word_0 &= 0xFFFF; /* Fill payload_size field. */ ip_header_ptr -> nx_ip_header_word_0 |= (ULONG)(value << NX_SHIFT_BY_16) & 0xFFFF0000; #ifdef NX_ENABLE_INTERFACE_CAPABILITY if (!(packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_IPV4_TX_CHECKSUM)) #endif /* NX_ENABLE_INTERFACE_CAPABILITY */ { /* Clear checksum in the IP header. */ ip_header_ptr -> nx_ip_header_word_2 = ip_header_ptr -> nx_ip_header_word_2 & 0xFFFF; checksum = _nx_ip_checksum_compute(packet_ptr, NX_IP_VERSION_V4, /* Length is the size of IP header, including options */ 20, /* IPv4 header checksum does not use src/dest addresses */ NULL, NULL); val = (ULONG)(~checksum); val = val & NX_LOWER_16_MASK; /* Convert to network byte order. */ NX_CHANGE_ULONG_ENDIAN(val); /* Now store the checksum in the IP header. */ ip_header_ptr -> nx_ip_header_word_2 = ip_header_ptr -> nx_ip_header_word_2 | val; ...} #ifdef NX_ENABLE_INTERFACE_CAPABILITY else { packet_ptr -> nx_packet_interface_capability_flag |= NX_INTERFACE_CAPABILITY_IPV4_TX_CHECKSUM; }else { ... } /* ... */#endif /* NX_ENABLE_INTERFACE_CAPABILITY */ }if (is_hw_processed) { ... } /* Process this packet in IPsec tunnel mode? */ if (packet_ptr -> nx_packet_ipsec_sa_ptr && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_ESP_PACKET && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_AH_PACKET && ((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) { /* Perform IPSec processing for tunneling. Insert IPsec headers and encapsulating the IP header. */ payload_size = packet_ptr -> nx_packet_length; status = _nx_ipsec_ip_output_packet_process(ip_ptr, &packet_ptr, NX_PROTOCOL_IPV4, payload_size, &payload_size); if ((status != NX_SUCCESS) && (status != NX_IPSEC_HW_PENDING)) { /* IPsec output packet process failed. */ /* Release the packet. */ _nx_packet_transmit_release(packet_ptr); }if ((status != NX_SUCCESS) && (status != NX_IPSEC_HW_PENDING)) { ... } /* Tunnel consume the packet. */ return; }if (packet_ptr -> nx_packet_ipsec_sa_ptr && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_ESP_PACKET && packet_ptr -> nx_packet_ipsec_state != NX_IPSEC_AH_PACKET && ((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) { ... } /* Process IPSec on packet requiring AH processing. */ if (packet_ptr -> nx_packet_ipsec_sa_ptr && packet_ptr -> nx_packet_ipsec_state == NX_IPSEC_AH_PACKET) { status = ip_ptr -> nx_ip_ipsec_authentication_header_transmit(ip_ptr, &packet_ptr, protocol, 1); if ((status != NX_SUCCESS) && (status != NX_IPSEC_HW_PENDING)) { /* Release the packet. */ _nx_packet_transmit_release(packet_ptr); return; }if ((status != NX_SUCCESS) && (status != NX_IPSEC_HW_PENDING)) { ... } }if (packet_ptr -> nx_packet_ipsec_sa_ptr && packet_ptr -> nx_packet_ipsec_state == NX_IPSEC_AH_PACKET) { ... } /* HW crypto driver is processing packet. */ if (status == NX_IPSEC_HW_PENDING) { #ifndef NX_DISABLE_IP_INFO /* Decrement the total send requests counter. */ ip_ptr -> nx_ip_total_packet_send_requests--;/* ... */ #endif return; }if (status == NX_IPSEC_HW_PENDING) { ... } /* ... */ #endif /* If the next hop address is null, indicates the specified interface is unreached. */ if (next_hop_address == 0) { /* Check whether the forward feature is enabled. */ if (ip_ptr -> nx_ip_forward_packet_process) { /* Initialize the interface. */ packet_ptr -> nx_packet_address.nx_packet_interface_ptr = NX_NULL; /* Figure out the best interface to send the packet on. */ _nx_ip_route_find(ip_ptr, destination_ip, &packet_ptr -> nx_packet_address.nx_packet_interface_ptr, &next_hop_address); }if (ip_ptr -> nx_ip_forward_packet_process) { ... } /* Make sure the packet interface and next hop address are set. */ /*lint -e{644} suppress variable might not be initialized, since "next_hop_address" was initialized in _nx_ip_route_find. */ if ((packet_ptr -> nx_packet_address.nx_packet_interface_ptr == NX_NULL) || (next_hop_address == 0)) { #ifndef NX_DISABLE_IP_INFO /* Increment the IP invalid packet error. */ ip_ptr -> nx_ip_invalid_transmit_packets++;/* ... */ #endif /* !NX_DISABLE_IP_INFO */ /* Release the packet. */ _nx_packet_transmit_release(packet_ptr); /* Return... nothing more can be done! */ return; }if ((packet_ptr -> nx_packet_address.nx_packet_interface_ptr == NX_NULL) || (next_hop_address == 0)) { ... } }if (next_hop_address == 0) { ... } /* Directly send the packet. */ _nx_ip_driver_packet_send(ip_ptr, packet_ptr, destination_ip, fragment, next_hop_address); }{ ... } /* ... */#endif /* NX_DISABLE_IPV4 */