1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Internet Protocol (IP)                                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  COMPONENT DEFINITION                                   RELEASE        */
27 /*                                                                        */
28 /*    nx_ipv4.h                                           PORTABLE C      */
29 /*                                                           6.1.9        */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Yuxin Zhou, Microsoft Corporation                                   */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the NetX Internet Protocol component,             */
37 /*    including all data types and external references.  It is assumed    */
38 /*    that nx_api.h and nx_port.h have already been included.             */
39 /*                                                                        */
40 /*  RELEASE HISTORY                                                       */
41 /*                                                                        */
42 /*    DATE              NAME                      DESCRIPTION             */
43 /*                                                                        */
44 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
45 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
46 /*                                            resulting in version 6.1    */
47 /*  08-02-2021     Yuxin Zhou               Modified comment(s), and      */
48 /*                                            supported TCP/IP offload,   */
49 /*                                            resulting in version 6.1.8  */
50 /*  10-15-2021     Yuxin Zhou               Modified comment(s), included */
51 /*                                            necessary header file,      */
52 /*                                            resulting in version 6.1.9  */
53 /*                                                                        */
54 /**************************************************************************/
55 
56 #ifndef NX_IPV4_H
57 #define NX_IPV4_H
58 
59 #include "nx_api.h"
60 
61 
62 #ifndef NX_DISABLE_IPV4
63 #define NX_IP_VERSION                    0x45000000UL /* Version 4, Length of 5   */
64 
65 #define NX_IP_NORMAL_LENGTH              5            /* Normal IP header length  */
66 #define NX_IP_HEADER_LENGTH_ENCODE_6     6            /* IP header length 6. */
67 
68 /* Define IP options.  */
69 
70 #define NX_IP_OPTION_COPY_FLAG           0x80000000UL       /* All fragments must carry the option. */
71 #define NX_IP_OPTION_CLASS               0x00000000UL       /* Control. */
72 #define NX_IP_OPTION_ROUTER_ALERT_NUMBER 0x14000000UL
73 #define NX_IP_OPTION_ROUTER_ALERT_LENGTH 0x00040000UL
74 #define NX_IP_OPTION_ROUTER_ALERT_VALUE  0x00000000UL
75 
76 
77 /* Define IP option type.  */
78 #define NX_IP_OPTION_END                 0
79 #define NX_IP_OPTION_NO_OPERATION        1
80 #define NX_IP_OPTION_INTERNET_TIMESTAMP  68
81 
82 
83 /* Define IP fragmenting information.  */
84 
85 #ifdef NX_DONT_FRAGMENT
86 #define NX_IP_DONT_FRAGMENT              NX_DONT_FRAGMENT
87 #else
88 #define NX_IP_DONT_FRAGMENT              0x00004000UL /* Don't fragment bit       */
89 #endif /* NX_DONT_FRAGMENT */
90 #ifdef NX_MORE_FRAGMENTS
91 #define NX_IP_MORE_FRAGMENT              NX_MORE_FRAGMENTS
92 #else
93 #define NX_IP_MORE_FRAGMENT              0x00002000UL /* More fragments           */
94 #endif /* NX_DONT_FRAGMENT */
95 #define NX_IP_FRAGMENT_MASK              0x00003FFFUL /* Mask for fragment bits   */
96 #ifdef NX_FRAG_OFFSET_MASK
97 #define NX_IP_OFFSET_MASK                NX_FRAG_OFFSET_MASK
98 #else
99 #define NX_IP_OFFSET_MASK                0x00001FFFUL /* Mask for fragment offset */
100 #endif /* NX_FRAG_OFFSET_MASK */
101 #define NX_IP_ALIGN_FRAGS                8            /* Fragment alignment       */
102 
103 /* Define basic IP Header constant.  */
104 
105 /* Define Basic Internet packet header data type.  This will be used to
106    build new IP packets and to examine incoming packets into NetX.  */
107 
108 typedef  struct NX_IPV4_HEADER_STRUCT
109 {
110     /* Define the first 32-bit word of the IP header.  This word contains
111        the following information:
112 
113             bits 31-28  IP Version = 0x4  (IP Version4)
114             bits 27-24  IP Header Length of 32-bit words (5 if no options)
115             bits 23-16  IP Type of Service, where 0x00 -> Normal
116                                                   0x10 -> Minimize Delay
117                                                   0x08 -> Maximize Throughput
118                                                   0x04 -> Maximize Reliability
119                                                   0x02 -> Minimize Monetary Cost
120             bits 15-0   IP Datagram length in bytes
121      */
122     ULONG nx_ip_header_word_0;
123 
124     /* Define the second word of the IP header.  This word contains
125        the following information:
126 
127             bits 31-16  IP Packet Identification (just an incrementing number)
128             bits 15-0   IP Flags and Fragment Offset (used for IP fragmenting)
129                             bit  15         Zero
130                             bit  14         Don't Fragment
131                             bit  13         More Fragments
132                             bits 12-0       (Fragment offset)/8
133      */
134     ULONG nx_ip_header_word_1;
135 
136     /* Define the third word of the IP header.  This word contains
137        the following information:
138 
139             bits 31-24  IP Time-To-Live (maximum number of routers
140                                          packet can traverse before being
141                                          thrown away.  Default values are typically
142                                          32 or 64)
143             bits 23-16  IP Protocol, where  0x01 -> ICMP Messages
144                                             0x02 -> IGMP Messages
145                                             0x06 -> TCP  Messages
146                                             0x11 -> UDP  Messages
147             bits 15-0   IP Checksum
148      */
149     ULONG nx_ip_header_word_2;
150 
151     /* Define the source IP address.  */
152     ULONG nx_ip_header_source_ip;
153 
154     /* Define the destination IP address.  */
155     ULONG nx_ip_header_destination_ip;
156 } NX_IPV4_HEADER;
157 
158 /* Define IPv4 internal function prototypes.  */
159 VOID  _nx_ip_forward_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
160 VOID  _nx_ip_fragment_forward_packet(NX_IP *ip_ptr, NX_PACKET *packet_ptr, ULONG destination_ip, ULONG fragment, ULONG next_hop_address);
161 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);
162 UINT  _nx_ip_header_add(NX_IP *ip_ptr, NX_PACKET *packet_ptr, ULONG source_ip, ULONG destination_ip,
163                         ULONG type_of_service, ULONG time_to_live, ULONG protocol, ULONG fragment);
164 VOID  _nx_ip_driver_packet_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr, ULONG destination_ip, ULONG fragment, ULONG next_hop_address);
165 ULONG _nx_ip_route_find(NX_IP *ip_ptr, ULONG destination_address, NX_INTERFACE **nx_ip_interface, ULONG *next_hop_address);
166 VOID  _nx_ipv4_packet_receive(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
167 UINT  _nx_ipv4_option_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr);
168 #endif /* NX_DISABLE_IPV4 */
169 
170 /* Define IPv4 function prototypes.  */
171 
172 UINT _nx_ip_address_change_notify(NX_IP *ip_ptr, VOID (*ip_address_change_notify)(NX_IP *, VOID *), VOID *additional_info);
173 UINT _nx_ip_address_get(NX_IP *ip_ptr, ULONG *ip_address, ULONG *network_mask);
174 UINT _nx_ip_address_set(NX_IP *ip_ptr, ULONG ip_address, ULONG network_mask);
175 UINT _nx_ip_gateway_address_set(NX_IP *ip_ptr, ULONG ip_address);
176 UINT _nx_ip_gateway_address_get(NX_IP *ip_ptr, ULONG *ip_address);
177 UINT _nx_ip_gateway_address_clear(NX_IP *ip_ptr);
178 UINT _nx_ip_interface_address_get(NX_IP *ip_ptr, UINT interface_index, ULONG *ip_address, ULONG *network_mask);
179 UINT _nx_ip_interface_address_set(NX_IP *ip_ptr, UINT interface_index, ULONG ip_address, ULONG network_mask);
180 UINT _nx_ip_raw_packet_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr,
181                             ULONG destination_ip, ULONG type_of_service);
182 UINT _nx_ip_raw_packet_source_send(NX_IP *ip_ptr, NX_PACKET *packet_ptr, ULONG destination_ip, UINT address_index, ULONG type_of_service);
183 UINT _nx_ip_static_route_add(NX_IP *ip_ptr, ULONG network_address,
184                              ULONG net_mask, ULONG next_hop);
185 UINT _nx_ip_static_route_delete(NX_IP *ip_ptr, ULONG network_address, ULONG net_mask);
186 UINT _nx_ipv4_multicast_interface_join(NX_IP *ip_ptr, ULONG group_address, UINT interface_index);
187 UINT _nx_ipv4_multicast_interface_leave(NX_IP *ip_ptr, ULONG group_address, UINT interface_index);
188 
189 
190 /* Define error checking shells for API services.  These are only referenced by the
191    application.  */
192 
193 UINT _nxe_ip_address_change_notify(NX_IP *ip_ptr, VOID (*ip_address_change_notify)(NX_IP *, VOID *), VOID *additional_info);
194 UINT _nxe_ip_address_get(NX_IP *ip_ptr, ULONG *ip_address, ULONG *network_mask);
195 UINT _nxe_ip_address_set(NX_IP *ip_ptr, ULONG ip_address, ULONG network_mask);
196 UINT _nxe_ip_interface_address_get(NX_IP *ip_ptr, UINT interface_index, ULONG *ip_address, ULONG *network_mask);
197 UINT _nxe_ip_interface_address_set(NX_IP *ip_ptr, UINT interface_index, ULONG ip_address, ULONG network_mask);
198 UINT _nxe_ip_gateway_address_set(NX_IP *ip_ptr, ULONG ip_address);
199 UINT _nxe_ip_gateway_address_get(NX_IP *ip_ptr, ULONG *ip_address);
200 UINT _nxe_ip_gateway_address_clear(NX_IP *ip_ptr);
201 UINT _nxe_ip_raw_packet_send(NX_IP *ip_ptr, NX_PACKET **packet_ptr_ptr,
202                              ULONG destination_ip, ULONG type_of_service);
203 UINT _nxe_ip_raw_packet_source_send(NX_IP *ip_ptr, NX_PACKET **packet_ptr_ptr,
204                                     ULONG destination_ip, UINT address_index, ULONG type_of_service);
205 UINT _nxe_ip_static_route_add(NX_IP *ip_ptr, ULONG network_address,
206                               ULONG net_mask, ULONG next_hop);
207 UINT _nxe_ip_static_route_delete(NX_IP *ip_ptr, ULONG network_address, ULONG net_mask);
208 UINT _nxe_ipv4_multicast_interface_join(NX_IP *ip_ptr, ULONG group_address, UINT interface_index);
209 UINT _nxe_ipv4_multicast_interface_leave(NX_IP *ip_ptr, ULONG group_address, UINT interface_index);
210 
211 #ifndef FEATURE_NX_IPV6
212 #ifdef NX_IPSEC_ENABLE
213 #define AUTHENTICATION_HEADER 5
214 #define ENCAP_SECURITY_HEADER 6
215 #endif /* NX_IPSEC_ENABLE */
216 #endif /* FEATURE_NX_IPV6 */
217 #endif /* NX_IPV4_H */
218 
219