1
10
13
14
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
126
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
182
183
184
185
186
187
188
195
196
197
198
199
200
205
206
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
242
243
244
245
246
247
248
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_igmp.h"
#ifdef NX_IPSEC_ENABLE
#include "nx_ipsec.h"
#endif
#ifndef NX_DISABLE_IPV4...
...
UINT _nx_ip_header_add(NX_IP *ip_ptr, NX_PACKET *packet_ptr, ULONG source_ip, ULONG destination_ip,
ULONG type_of_service, ULONG time_to_live, ULONG protocol, ULONG fragment)
{
ULONG router_alert = 0;
NX_IPV4_HEADER *ip_header_ptr;
ULONG checksum;
#if defined(NX_DISABLE_IP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
UINT compute_checksum = 1;
#endif
ULONG val;
#ifndef NX_DISABLE_IGMPV2
if ((protocol == NX_IP_IGMP) && (ip_ptr -> nx_ip_igmp_router_version == NX_IGMP_HOST_VERSION_2))
{
router_alert = 4;
}if ((protocol == NX_IP_IGMP) && (ip_ptr -> nx_ip_igmp_router_version == NX_IGMP_HOST_VERSION_2)) { ... }
/* ... */#endif
packet_ptr -> nx_packet_prepend_ptr = (packet_ptr -> nx_packet_prepend_ptr - sizeof(NX_IPV4_HEADER)) - router_alert;
packet_ptr -> nx_packet_length = packet_ptr -> nx_packet_length + (ULONG)sizeof(NX_IPV4_HEADER) + router_alert;
NX_ASSERT(packet_ptr -> nx_packet_prepend_ptr >= packet_ptr -> nx_packet_data_start);
ip_header_ptr = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
packet_ptr -> nx_packet_ip_header = packet_ptr -> nx_packet_prepend_ptr;
packet_ptr -> nx_packet_ip_header_length = (UCHAR)(packet_ptr -> nx_packet_ip_header_length +
sizeof(NX_IPV4_HEADER) + router_alert);
/* ... */
if (packet_ptr -> nx_packet_identical_copy == NX_TRUE)
{
/* ... */
return(NX_SUCCESS);
}if (packet_ptr -> nx_packet_identical_copy == NX_TRUE) { ... }
#ifndef NX_DISABLE_IGMPV2
if (router_alert)
{
ip_header_ptr -> nx_ip_header_word_0 = (ULONG)((NX_IP_VERSION_V4 << 28) |
(NX_IP_HEADER_LENGTH_ENCODE_6 << 24) |
type_of_service |
(0xFFFF & packet_ptr -> nx_packet_length));
}if (router_alert) { ... }
else
#endif
{
ip_header_ptr -> nx_ip_header_word_0 = (NX_IP_VERSION | type_of_service | (0xFFFF & packet_ptr -> nx_packet_length));
}else { ... }
ip_header_ptr -> nx_ip_header_word_1 = (ip_ptr -> nx_ip_packet_id++ << NX_SHIFT_BY_16) | fragment;
ip_header_ptr -> nx_ip_header_word_2 = ((time_to_live << NX_IP_TIME_TO_LIVE_SHIFT) | protocol);
ip_header_ptr -> nx_ip_header_source_ip = source_ip;
ip_header_ptr -> nx_ip_header_destination_ip = destination_ip;
#ifndef NX_DISABLE_IGMPV2
if (router_alert)
{
*((ULONG *)(packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_IPV4_HEADER))) = (NX_IP_OPTION_COPY_FLAG |
NX_IP_OPTION_CLASS |
NX_IP_OPTION_ROUTER_ALERT_NUMBER |
NX_IP_OPTION_ROUTER_ALERT_LENGTH |
NX_IP_OPTION_ROUTER_ALERT_VALUE);
}if (router_alert) { ... }
/* ... */#endif
/* ... */
NX_CHANGE_ULONG_ENDIAN(ip_header_ptr -> nx_ip_header_word_0);
NX_CHANGE_ULONG_ENDIAN(ip_header_ptr -> nx_ip_header_word_1);
NX_CHANGE_ULONG_ENDIAN(ip_header_ptr -> nx_ip_header_word_2);
NX_CHANGE_ULONG_ENDIAN(ip_header_ptr -> nx_ip_header_source_ip);
NX_CHANGE_ULONG_ENDIAN(ip_header_ptr -> nx_ip_header_destination_ip);
#ifndef NX_DISABLE_IGMPV2
if (router_alert)
{
NX_CHANGE_ULONG_ENDIAN(*((ULONG *)(packet_ptr -> nx_packet_prepend_ptr + sizeof(NX_IPV4_HEADER))));
}if (router_alert) { ... }
/* ... */#endif
#ifdef NX_DISABLE_IP_TX_CHECKSUM
compute_checksum = 0;
#endif
#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)
{
compute_checksum = 0;
}if (packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_IPV4_TX_CHECKSUM) { ... }
/* ... */#endif
#ifdef NX_IPSEC_ENABLE
if (packet_ptr -> nx_packet_ipsec_sa_ptr != NX_NULL)
{
if ((((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) &&
(((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_encryption_method != NX_CRYPTO_NONE))
{
compute_checksum = 1;
}if ((((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_mode == NX_IPSEC_TUNNEL_MODE) && (((NX_IPSEC_SA *)(packet_ptr -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_encryption_method != NX_CRYPTO_NONE)) { ... }
}if (packet_ptr -> nx_packet_ipsec_sa_ptr != NX_NULL) { ... }
/* ... */
#endif
#if defined(NX_DISABLE_IP_TX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
if (compute_checksum)
#endif
{
checksum = _nx_ip_checksum_compute(packet_ptr, NX_IP_VERSION_V4,
(UINT)(20 + router_alert),
NULL, NULL);
val = (ULONG)(~checksum);
val = val & NX_LOWER_16_MASK;
NX_CHANGE_ULONG_ENDIAN(val);
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
return(NX_SUCCESS);
}{ ... }
/* ... */#endif