1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Internet Protocol (IP)                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /**************************************************************************/
24 /*                                                                        */
25 /*  COMPONENT DEFINITION                                   RELEASE        */
26 /*                                                                        */
27 /*    nx_ip.h                                             PORTABLE C      */
28 /*                                                           6.1.9        */
29 /*  AUTHOR                                                                */
30 /*                                                                        */
31 /*    Yuxin Zhou, Microsoft Corporation                                   */
32 /*                                                                        */
33 /*  DESCRIPTION                                                           */
34 /*                                                                        */
35 /*    This file defines the NetX Internet Protocol component,             */
36 /*    including all data types and external references.  It is assumed    */
37 /*    that nx_api.h and nx_port.h have already been included.             */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
44 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
45 /*                                            resulting in version 6.1    */
46 /*  10-15-2021     Yuxin Zhou               Modified comment(s), included */
47 /*                                            necessary header file,      */
48 /*                                            resulting in version 6.1.9  */
49 /*                                                                        */
50 /**************************************************************************/
51 
52 #ifndef NX_IP_H
53 #define NX_IP_H
54 
55 #include "nx_api.h"
56 
57 
58 /* Define IP constants.  */
59 
60 #define NX_IP_ID                     ((ULONG)0x49502020)
61 
62 
63 
64 /* Define the mask for the IP header length field.  */
65 
66 #define NX_IP_LENGTH_MASK            ((ULONG)0x0F000000)       /* Mask for length bit      */
67 
68 
69 
70 /* Define IP event flags.  These events are processed by the IP thread. */
71 
72 #define NX_IP_ALL_EVENTS             ((ULONG)0xFFFFFFFF)       /* All event flags              */
73 #define NX_IP_PERIODIC_EVENT         ((ULONG)0x00000001)       /* Periodic event               */
74 #define NX_IP_UNFRAG_EVENT           ((ULONG)0x00000002)       /* Unfragment event             */
75 #define NX_IP_ICMP_EVENT             ((ULONG)0x00000004)       /* ICMP message event           */
76 #define NX_IP_RECEIVE_EVENT          ((ULONG)0x00000008)       /* IP receive packet event      */
77 #define NX_IP_ARP_REC_EVENT          ((ULONG)0x00000010)       /* ARP deferred receive event   */
78 #define NX_IP_RARP_REC_EVENT         ((ULONG)0x00000020)       /* RARP deferred receive event  */
79 #define NX_IP_IGMP_EVENT             ((ULONG)0x00000040)       /* IGMP message event           */
80 #define NX_IP_TCP_EVENT              ((ULONG)0x00000080)       /* TCP message event            */
81 #define NX_IP_FAST_EVENT             ((ULONG)0x00000100)       /* Fast TCP timer event         */
82 #ifdef NX_DRIVER_DEFERRED_PROCESSING
83 #define NX_IP_DRIVER_PACKET_EVENT    ((ULONG)0x00000200)       /* Driver Deferred packet event */
84 #endif /* NX_DRIVER_DEFERRED_PROCESSING */
85 #define NX_IP_IGMP_ENABLE_EVENT      ((ULONG)0x00000400)       /* IGMP enable event            */
86 #define NX_IP_DRIVER_DEFERRED_EVENT  ((ULONG)0x00000800)       /* Driver deferred processing   */
87                                                                /*   event                      */
88 #define NX_IP_TCP_CLEANUP_DEFERRED   ((ULONG)0x00001000)       /* Deferred TCP cleanup event   */
89 #ifdef NX_IPSEC_ENABLE
90 #define NX_IP_HW_DONE_EVENT          ((ULONG)0x00002000)       /* HW done event                */
91 #endif /* NX_IPSEC_ENABLE */
92 #define NX_IP_LINK_STATUS_EVENT      ((ULONG)0x00004000)       /* Link status change event     */
93 
94 
95 #ifndef NX_IP_FAST_TIMER_RATE
96 #define NX_IP_FAST_TIMER_RATE        10
97 #endif
98 
99 /* Define the amount of time to sleep in nx_ip_(interface_)status_check */
100 #ifndef NX_IP_STATUS_CHECK_WAIT_TIME
101 #define NX_IP_STATUS_CHECK_WAIT_TIME 1
102 #endif /* NX_IP_STATUS_CHECK_WAIT_TIME */
103 
104 #include "nx_ipv4.h"
105 
106 
107 
108 /* Define IP function prototypes.  */
109 UINT   _nx_ip_auxiliary_packet_pool_set(NX_IP *ip_ptr, NX_PACKET_POOL *auxiliary_pool);
110 USHORT _nx_ip_checksum_compute(NX_PACKET *packet_ptr, ULONG protocol, UINT data_length,
111                                ULONG *_src_ip_addr, ULONG *_dest_ip_addr);
112 UINT   _nx_ip_interface_address_mapping_configure(NX_IP *ip_ptr, UINT interface_index, UINT mapping_needed);
113 UINT   _nx_ip_interface_capability_get(NX_IP *ip_ptr, UINT interface_index, ULONG *interface_capability_flag);
114 UINT   _nx_ip_interface_capability_set(NX_IP *ip_ptr, UINT interface_index, ULONG interface_capability_flag);
115 UINT   _nx_ip_interface_info_get(NX_IP *ip_ptr, UINT interface_index, CHAR **interface_name, ULONG *ip_address,
116                                  ULONG *network_mask, ULONG *mtu_size, ULONG *phsyical_address_msw,
117                                  ULONG *physical_address_lsw);
118 UINT _nx_ip_interface_mtu_set(NX_IP *ip_ptr, UINT interface_index, ULONG mtu_size);
119 UINT _nx_ip_interface_physical_address_get(NX_IP *ip_ptr, UINT interface_index, ULONG *physical_msw, ULONG *physical_lsw);
120 UINT _nx_ip_interface_physical_address_set(NX_IP *ip_ptr, UINT interface_index, ULONG physical_msw, ULONG physical_lsw, UINT update_driver);
121 UINT _nx_ip_interface_status_check(NX_IP *ip_ptr, UINT interface_index, ULONG needed_status, ULONG *actual_status,
122                                    ULONG wait_option);
123 UINT _nx_ip_create(NX_IP *ip_ptr, CHAR *name, ULONG ip_address, ULONG network_mask,
124                    NX_PACKET_POOL *default_pool, VOID (*ip_link_driver)(NX_IP_DRIVER *),
125                    VOID *memory_ptr, ULONG memory_size, UINT priority);
126 UINT _nx_ip_delete(NX_IP *ip_ptr);
127 VOID _nx_ip_delete_queue_clear(NX_PACKET *head_ptr);
128 VOID _nx_ip_deferred_link_status_process(NX_IP *ip_ptr);
129 VOID _nx_ip_driver_link_status_event(NX_IP *ip_ptr, UINT interface_index);
130 VOID _nx_ip_driver_deferred_enable(NX_IP *ip_ptr, VOID (*driver_deferred_packet_handler)(NX_IP *ip_ptr, NX_PACKET *packet_ptr));
131 VOID _nx_ip_driver_deferred_processing(NX_IP *ip_ptr);
132 VOID _nx_ip_driver_deferred_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
133 UINT _nx_ip_driver_direct_command(NX_IP *ip_ptr, UINT command, ULONG *return_value_ptr);
134 UINT _nx_ip_driver_interface_direct_command(NX_IP *ip_ptr, UINT command, UINT interface_index, ULONG *return_value_ptr);
135 
136 
137 UINT _nx_ip_forwarding_disable(NX_IP *ip_ptr);
138 UINT _nx_ip_forwarding_enable(NX_IP *ip_ptr);
139 UINT _nx_ip_fragment_disable(NX_IP *ip_ptr);
140 UINT _nx_ip_fragment_enable(NX_IP *ip_ptr);
141 UINT _nx_ip_info_get(NX_IP *ip_ptr, ULONG *ip_total_packets_sent, ULONG *ip_total_bytes_sent,
142                      ULONG *ip_total_packets_received, ULONG *ip_total_bytes_received,
143                      ULONG *ip_invalid_packets, ULONG *ip_receive_packets_dropped,
144                      ULONG *ip_receive_checksum_errors, ULONG *ip_send_packets_dropped,
145                      ULONG *ip_total_fragments_sent, ULONG *ip_total_fragments_received);
146 UINT _nx_ip_interface_attach(NX_IP *ip_ptr, CHAR *interface_name, ULONG ip_address, ULONG network_mask, VOID (*ip_link_driver)(struct NX_IP_DRIVER_STRUCT *));
147 UINT _nx_ip_interface_detach(NX_IP *ip_ptr, UINT index);
148 UINT _nx_ip_max_payload_size_find(NX_IP *ip_ptr, NXD_ADDRESS *dest_address, UINT if_index,
149                                   UINT src_port, UINT dest_port, ULONG protocol, ULONG *start_offset_ptr,
150                                   ULONG *payload_length_ptr);
151 UINT _nx_ip_raw_packet_disable(NX_IP *ip_ptr);
152 UINT _nx_ip_raw_packet_enable(NX_IP *ip_ptr);
153 UINT _nx_ip_raw_packet_filter_set(NX_IP *ip_ptr,  UINT (*raw_packet_filter)(NX_IP *, ULONG, NX_PACKET *));
154 UINT _nx_ip_raw_receive_queue_max_set(NX_IP *ip_ptr, ULONG queue_max);
155 UINT _nx_ip_raw_packet_receive(NX_IP *ip_ptr, NX_PACKET **packet_ptr, ULONG wait_option);
156 UINT _nxd_ip_raw_packet_source_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr, NXD_ADDRESS *destination_ip, UINT address_index, ULONG protocol, UINT ttl, ULONG tos);
157 VOID _nx_ip_initialize(VOID);
158 VOID _nx_ip_periodic_timer_entry(ULONG ip_address);
159 VOID _nx_ip_packet_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
160 VOID _nx_ip_packet_deferred_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
161 UINT _nx_ip_status_check(NX_IP *ip_ptr, ULONG needed_status, ULONG *actual_status, ULONG wait_option);
162 UINT _nx_ip_link_status_change_notify_set(NX_IP *ip_ptr,  VOID (*link_status_change_notify)(NX_IP *ip_ptr, UINT interface_index, UINT link_up));
163 VOID _nx_ip_thread_entry(ULONG ip_ptr_value);
164 VOID _nx_ip_raw_packet_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER);
165 UINT _nx_ip_raw_packet_processing(NX_IP *ip_ptr, ULONG protocol, NX_PACKET *packet_ptr);
166 UINT _nxd_ip_raw_packet_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr,
167                              NXD_ADDRESS *destination_ip, ULONG protocol, UINT ttl, ULONG tos);
168 #ifndef NX_DISABLE_FRAGMENTATION
169 VOID _nx_ip_fragment_timeout_check(NX_IP *ip_ptr);
170 VOID _nx_ip_fragment_packet(struct NX_IP_DRIVER_STRUCT *driver_req_ptr);
171 VOID _nx_ip_fragment_assembly(NX_IP *ip_ptr);
172 #endif /* NX_DISABLE_FRAGMENTATION */
173 #ifdef NX_ENABLE_INTERFACE_CAPABILITY
174 VOID _nx_ip_packet_checksum_compute(NX_PACKET *packet_ptr);
175 #endif /* NX_ENABLE_INTERFACE_CAPABILITY */
176 
177 
178 /* Define error checking shells for API services.  These are only referenced by the
179    application.  */
180 
181 UINT _nxe_ip_create(NX_IP *ip_ptr, CHAR *name, ULONG ip_address, ULONG network_mask,
182                     NX_PACKET_POOL *default_pool,
183                     VOID (*ip_link_driver)(NX_IP_DRIVER *),
184                     VOID *memory_ptr, ULONG memory_size, UINT priority, UINT ip_control_block_size);
185 UINT _nxe_ip_delete(NX_IP *ip_ptr);
186 UINT _nxe_ip_driver_direct_command(NX_IP *ip_ptr, UINT command, ULONG *return_value_ptr);
187 UINT _nxe_ip_driver_interface_direct_command(NX_IP *ip_ptr, UINT command, UINT interface_index, ULONG *return_value_ptr);
188 UINT _nxe_ip_auxiliary_packet_pool_set(NX_IP *ip_ptr, NX_PACKET_POOL *auxiliary_pool);
189 
190 
191 UINT _nxe_ip_forwarding_disable(NX_IP *ip_ptr);
192 UINT _nxe_ip_forwarding_enable(NX_IP *ip_ptr);
193 UINT _nxe_ip_fragment_disable(NX_IP *ip_ptr);
194 UINT _nxe_ip_fragment_enable(NX_IP *ip_ptr);
195 UINT _nxe_ip_info_get(NX_IP *ip_ptr, ULONG *ip_total_packets_sent, ULONG *ip_total_bytes_sent,
196                       ULONG *ip_total_packets_received, ULONG *ip_total_bytes_received,
197                       ULONG *ip_invalid_packets, ULONG *ip_receive_packets_dropped,
198                       ULONG *ip_receive_checksum_errors, ULONG *ip_send_packets_dropped,
199                       ULONG *ip_total_fragments_sent, ULONG *ip_total_fragments_received);
200 UINT _nxe_ip_interface_attach(NX_IP *ip_ptr, CHAR *interface_name, ULONG ip_address, ULONG network_mask, VOID (*ip_link_driver)(struct NX_IP_DRIVER_STRUCT *));
201 UINT _nxe_ip_interface_detach(NX_IP *ip_ptr, UINT index);
202 UINT _nxe_ip_max_payload_size_find(NX_IP *ip_ptr, NXD_ADDRESS *dest_address, UINT if_index,
203                                    UINT src_port, UINT dest_port, ULONG protocol, ULONG *start_offset_ptr,
204                                    ULONG *payload_length_ptr);
205 UINT _nxe_ip_raw_packet_disable(NX_IP *ip_ptr);
206 UINT _nxe_ip_raw_packet_enable(NX_IP *ip_ptr);
207 UINT _nxe_ip_raw_packet_receive(NX_IP *ip_ptr, NX_PACKET **packet_ptr, ULONG wait_option);
208 UINT _nxde_ip_raw_packet_source_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr, NXD_ADDRESS *destination_ip, UINT address_index, ULONG protocol, UINT ttl, ULONG tos);
209 UINT _nxe_ip_status_check(NX_IP *ip_ptr, ULONG needed_status, ULONG *actual_status,
210                           ULONG wait_option);
211 
212 UINT _nxe_ip_link_status_change_notify_set(NX_IP *ip_ptr,  VOID (*link_status_change_notify)(NX_IP *ip_ptr, UINT interface_index, UINT link_up));
213 UINT _nxe_ip_interface_address_mapping_configure(NX_IP *ip_ptr, UINT interface_index, UINT mapping_needed);
214 UINT _nxe_ip_interface_capability_get(NX_IP *ip_ptr, UINT interface_index, ULONG *interface_capability_flag);
215 UINT _nxe_ip_interface_capability_set(NX_IP *ip_ptr, UINT interface_index, ULONG interface_capability_flag);
216 UINT _nxe_ip_interface_info_get(NX_IP *ip_ptr, UINT interface_index, CHAR **interface_name,
217                                 ULONG *ip_address, ULONG *network_mask, ULONG *mtu_size,
218                                 ULONG *physical_address_msw, ULONG *physical_address_lsw);
219 UINT _nxe_ip_interface_mtu_set(NX_IP *ip_ptr, UINT interface_index, ULONG mtu_size);
220 UINT _nxe_ip_interface_physical_address_get(NX_IP *ip_ptr, UINT interface_index,
221                                             ULONG *physical_msw, ULONG *physical_lsw);
222 UINT _nxe_ip_interface_physical_address_set(NX_IP *ip_ptr, UINT interface_index,
223                                             ULONG physical_msw, ULONG physical_lsw, UINT update_driver);
224 UINT _nxe_ip_interface_status_check(NX_IP *ip_ptr, UINT interface_index, ULONG needed_status,
225                                     ULONG *actual_status, ULONG wait_option);
226 UINT _nxe_ip_raw_packet_filter_set(NX_IP *ip_ptr,
227                                    UINT (*raw_packet_filter)(NX_IP *, ULONG, NX_PACKET *));
228 UINT _nxe_ip_raw_receive_queue_max_set(NX_IP *ip_ptr, ULONG queue_max);
229 
230 
231 VOID _nx_ip_fast_periodic_timer_create(NX_IP *ip_ptr);
232 
233 UINT _nx_ip_dispatch_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr, UINT protocol);
234 
235 /* IP component data declarations follow.  */
236 
237 /* Determine if the initialization function of this component is including
238    this file.  If so, make the data definitions really happen.  Otherwise,
239    make them extern so other functions in the component can access them.  */
240 
241 /*lint -e767 suppress different definitions.  */
242 #ifdef NX_IP_INIT
243 #define IP_DECLARE
244 #else
245 #define IP_DECLARE extern
246 #endif
247 /*lint +e767 enable checking for different definitions.  */
248 
249 
250 /* Define the head pointer of the created IP list.  */
251 
252 IP_DECLARE  NX_IP *_nx_ip_created_ptr;
253 
254 
255 /* Define the number of created IP instances.  */
256 
257 IP_DECLARE  ULONG _nx_ip_created_count;
258 
259 
260 #endif
261 
262