nat.h
Go to the documentation of this file.
1 /**
2  * @file nat.h
3  * @brief NAT (IP Network Address Translator)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
29  **/
30 
31 #ifndef _NAT_H
32 #define _NAT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //NAT support
38 #ifndef NAT_SUPPORT
39  #define NAT_SUPPORT DISABLED
40 #elif (NAT_SUPPORT != ENABLED && NAT_SUPPORT != DISABLED)
41  #error NAT_SUPPORT parameter is not valid
42 #endif
43 
44 //NAT support
45 #ifndef NAT_MAX_PRIVATE_INTERFACES
46  #define NAT_MAX_PRIVATE_INTERFACES 4
47 #elif (NAT_MAX_PRIVATE_INTERFACES < 1)
48  #error NAT_MAX_PRIVATE_INTERFACES parameter is not valid
49 #endif
50 
51 //NAT tick interval
52 #ifndef NAT_TICK_INTERVAL
53  #define NAT_TICK_INTERVAL 1000
54 #elif (NAT_TICK_INTERVAL < 10)
55  #error NAT_TICK_INTERVAL parameter is not valid
56 #endif
57 
58 //TCP session timeout
59 #ifndef NAT_TCP_SESSION_TIMEOUT
60  #define NAT_TCP_SESSION_TIMEOUT 120000
61 #elif (NAT_TCP_SESSION_TIMEOUT < 1000)
62  #error NAT_TCP_SESSION_TIMEOUT parameter is not valid
63 #endif
64 
65 //UDP session timeout
66 #ifndef NAT_UDP_SESSION_TIMEOUT
67  #define NAT_UDP_SESSION_TIMEOUT 120000
68 #elif (NAT_UDP_SESSION_TIMEOUT < 1000)
69  #error NAT_UDP_SESSION_TIMEOUT parameter is not valid
70 #endif
71 
72 //ICMP session timeout
73 #ifndef NAT_ICMP_SESSION_TIMEOUT
74  #define NAT_ICMP_SESSION_TIMEOUT 10000
75 #elif (NAT_ICMP_SESSION_TIMEOUT < 1000)
76  #error NAT_ICMP_SESSION_TIMEOUT parameter is not valid
77 #endif
78 
79 //TCP/UDP port range (lower limit)
80 #ifndef NAT_TCP_UDP_PORT_MIN
81  #define NAT_TCP_UDP_PORT_MIN 32768
82 #elif (NAT_TCP_UDP_PORT_MIN < 1024)
83  #error NAT_TCP_UDP_PORT_MIN parameter is not valid
84 #endif
85 
86 //TCP/UDP port range (upper limit)
87 #ifndef NAT_TCP_UDP_PORT_MAX
88  #define NAT_TCP_UDP_PORT_MAX 49151
89 #elif (NAT_TCP_UDP_PORT_MAX <= NAT_TCP_UDP_PORT_MIN || NAT_TCP_UDP_PORT_MAX > 65535)
90  #error NAT_TCP_UDP_PORT_MAX parameter is not valid
91 #endif
92 
93 //ICMP query identifier range (lower limit)
94 #ifndef NAT_ICMP_QUERY_ID_MIN
95  #define NAT_ICMP_QUERY_ID_MIN 32768
96 #elif (NAT_ICMP_QUERY_ID_MIN < 0)
97  #error NAT_ICMP_QUERY_ID_MIN parameter is not valid
98 #endif
99 
100 //ICMP query identifier range (upper limit)
101 #ifndef NAT_ICMP_QUERY_ID_MAX
102  #define NAT_ICMP_QUERY_ID_MAX 65535
103 #elif (NAT_ICMP_QUERY_ID_MAX <= NAT_ICMP_QUERY_ID_MIN || NAT_ICMP_QUERY_ID_MAX > 65535)
104  #error NAT_ICMP_QUERY_ID_MAX parameter is not valid
105 #endif
106 
107 //C++ guard
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111 
112 
113 /**
114  * @brief IP packet
115  **/
116 
117 typedef struct
118 {
121  size_t offset;
124  uint16_t srcPort;
126  uint16_t destPort;
127  uint16_t icmpType;
128  uint16_t icmpQueryId;
129  uint8_t ttl;
130  uint8_t tos;
131 } NatIpPacket;
132 
133 
134 /**
135  * @brief Port redirection rule
136  **/
137 
138 typedef struct
139 {
140  Ipv4Protocol protocol; ///<Transport protocol (TCP or UDP)
141  uint16_t publicPortMin; ///<Public port range to be redirected (lower value)
142  uint16_t publicPortMax; ///<Public port range to be redirected (upper value)
143  NetInterface *privateInterface; ///<Destination interface
144  Ipv4Addr privateIpAddr; ///<Destination IP address
145  uint16_t privatePortMin; ///<Destination port (lower value)
146  uint16_t privatePortMax; ///<Destination port (upper value)
148 
149 
150 /**
151  * @brief NAT session
152  **/
153 
154 typedef struct
155 {
156  Ipv4Protocol protocol; ///<IP protocol (TCP, UDP or ICMP)
157  NetInterface *privateInterface; ///<Private interface
158  Ipv4Addr privateIpAddr; ///<Internal IP address
159  uint16_t privatePort; ///<Internal TCP or UDP port number
160  uint16_t privateIcmpQueryId; ///<Internal ICMP query identifier
161  Ipv4Addr publicIpAddr; ///<External IP address
162  uint16_t publicPort; ///<External TCP or UDP port number
163  uint16_t publicIcmpQueryId; ///<External ICMP query identifier
164  Ipv4Addr remoteIpAddr; ///<Remote IP address
165  uint16_t remotePort; ///<Remote TCP or UDP port number
166  systime_t timestamp; ///<Timestamp to manage session timeout
167 } NatSession;
168 
169 
170 /**
171  * @brief NAT settings
172  **/
173 
174 typedef struct
175 {
176  NetInterface *publicInterface; ///<Public interface
177  uint_t publicIpAddrIndex; ///<Index of the public IP address to use
178  NetInterface *privateInterfaces[NAT_MAX_PRIVATE_INTERFACES]; ///<Private interfaces
179  uint_t numPrivateInterfaces; ///<Number of private interfaces
180  NatPortFwdRule *portFwdRules; ///<Port redirection rules
181  uint_t numPortFwdRules; ///<Number of port redirection rules
182  NatSession *sessions; ///<NAT sessions (initiated from a private host)
183  uint_t numSessions; ///<Number of NAT sessions
184 } NatSettings;
185 
186 
187 /**
188  * @brief NAT context
189  **/
190 
191 typedef struct
192 {
193  bool_t running; ///<This flag tells whether the NAT is running or not
194  NetInterface *publicInterface; ///<Public interface
195  uint_t publicIpAddrIndex; ///<Index of the public IP address to use
196  NetInterface *privateInterfaces[NAT_MAX_PRIVATE_INTERFACES]; ///<Private interfaces
197  uint_t numPrivateInterfaces; ///<Number of private interfaces
198  NatPortFwdRule *portFwdRules; ///<Port redirection rules
199  uint_t numPortFwdRules; ///<Number of port redirection rules
200  NatSession *sessions; ///<NAT sessions (initiated from a private host)
201  uint_t numSessions; ///<Number of NAT sessions
202 } NatContext;
203 
204 
205 //NAT related functions
206 void natGetDefaultSettings(NatSettings *settings);
207 error_t natInit(NatContext *context, const NatSettings *settings);
208 
210  NetInterface *publicInterface);
211 
213  Ipv4Protocol protocol, uint16_t publicPort, NetInterface *privateInterface,
214  Ipv4Addr privateIpAddr, uint16_t privatePort);
215 
217  Ipv4Protocol protocol, uint16_t publicPortMin, uint16_t publicPortMax,
218  NetInterface *privateInterface, Ipv4Addr privateIpAddr,
219  uint16_t privatePortMin);
220 
222 
223 error_t natStart(NatContext *context);
224 error_t natStop(NatContext *context);
225 
226 void natDeinit(NatContext *context);
227 
228 //C++ guard
229 #ifdef __cplusplus
230 }
231 #endif
232 
233 #endif
int bool_t
Definition: compiler_port.h:61
NatPortFwdRule * portFwdRules
Port redirection rules.
Definition: nat.h:180
Ipv4Protocol protocol
IP protocol (TCP, UDP or ICMP)
Definition: nat.h:156
uint8_t protocol
Definition: ipv4.h:327
uint_t publicIpAddrIndex
Index of the public IP address to use.
Definition: nat.h:177
Ipv4Addr destIpAddr
Definition: nat.h:125
uint16_t publicPort
External TCP or UDP port number.
Definition: nat.h:162
size_t offset
Definition: nat.h:121
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
systime_t timestamp
Timestamp to manage session timeout.
Definition: nat.h:166
error_t natSetPortFwdRule(NatContext *context, uint_t index, Ipv4Protocol protocol, uint16_t publicPort, NetInterface *privateInterface, Ipv4Addr privateIpAddr, uint16_t privatePort)
Add port redirection rule.
Definition: nat.c:196
uint_t numSessions
Number of NAT sessions.
Definition: nat.h:183
const NetBuffer * buffer
Definition: nat.h:120
uint16_t privatePortMin
Destination port (lower value)
Definition: nat.h:145
IP packet.
Definition: nat.h:118
uint8_t ttl
Definition: nat.h:129
error_t natStop(NatContext *context)
Stop NAT operation.
Definition: nat.c:345
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:298
Ipv4Addr srcIpAddr
Definition: nat.h:123
void natDeinit(NatContext *context)
Release NAT context.
Definition: nat.c:386
uint16_t srcPort
Definition: nat.h:124
uint16_t publicPortMax
Public port range to be redirected (upper value)
Definition: nat.h:142
uint16_t publicIcmpQueryId
External ICMP query identifier.
Definition: nat.h:163
uint_t numSessions
Number of NAT sessions.
Definition: nat.h:201
uint_t numPortFwdRules
Number of port redirection rules.
Definition: nat.h:199
uint16_t privateIcmpQueryId
Internal ICMP query identifier.
Definition: nat.h:160
error_t
Error codes.
Definition: error.h:43
Ipv4Addr privateIpAddr
Destination IP address.
Definition: nat.h:144
uint16_t publicPortMin
Public port range to be redirected (lower value)
Definition: nat.h:141
NAT context.
Definition: nat.h:192
Ipv4Addr remoteIpAddr
Remote IP address.
Definition: nat.h:164
Port redirection rule.
Definition: nat.h:139
#define NetInterface
Definition: net.h:36
NatPortFwdRule * portFwdRules
Port redirection rules.
Definition: nat.h:198
#define NAT_MAX_PRIVATE_INTERFACES
Definition: nat.h:46
error_t natSetPublicInterface(NatContext *context, NetInterface *publicInterface)
Specify the NAT public interface.
Definition: nat.c:154
Ipv4Protocol protocol
Definition: nat.h:122
NAT settings.
Definition: nat.h:175
uint_t numPrivateInterfaces
Number of private interfaces.
Definition: nat.h:197
error_t natSetPortRangeFwdRule(NatContext *context, uint_t index, Ipv4Protocol protocol, uint16_t publicPortMin, uint16_t publicPortMax, NetInterface *privateInterface, Ipv4Addr privateIpAddr, uint16_t privatePortMin)
Add port range redirection rule.
Definition: nat.c:224
uint8_t tos
Definition: nat.h:130
uint32_t systime_t
System time.
uint16_t icmpQueryId
Definition: nat.h:128
uint16_t remotePort
Remote TCP or UDP port number.
Definition: nat.h:165
NetInterface * publicInterface
Public interface.
Definition: nat.h:194
NatSession * sessions
NAT sessions (initiated from a private host)
Definition: nat.h:182
uint_t numPrivateInterfaces
Number of private interfaces.
Definition: nat.h:179
Ipv4Addr privateIpAddr
Internal IP address.
Definition: nat.h:158
error_t natClearPortFwdRule(NatContext *context, uint_t index)
Remove port redirection rule.
Definition: nat.c:273
Ipv4Protocol protocol
Transport protocol (TCP or UDP)
Definition: nat.h:140
NetInterface * interface
Definition: nat.h:119
NetInterface * privateInterface
Private interface.
Definition: nat.h:157
error_t natInit(NatContext *context, const NatSettings *settings)
NAT initialization.
Definition: nat.c:87
error_t natStart(NatContext *context)
Start NAT operation.
Definition: nat.c:301
uint_t publicIpAddrIndex
Index of the public IP address to use.
Definition: nat.h:195
NatSession * sessions
NAT sessions (initiated from a private host)
Definition: nat.h:200
NetInterface * privateInterface
Destination interface.
Definition: nat.h:143
Ipv4Protocol
IPv4 protocol field.
Definition: ipv4.h:249
NetInterface * publicInterface
Public interface.
Definition: nat.h:176
void natGetDefaultSettings(NatSettings *settings)
Initialize settings with default values.
Definition: nat.c:60
bool_t running
This flag tells whether the NAT is running or not.
Definition: nat.h:193
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
uint16_t privatePortMax
Destination port (upper value)
Definition: nat.h:146
uint16_t destPort
Definition: nat.h:126
uint16_t icmpType
Definition: nat.h:127
uint_t numPortFwdRules
Number of port redirection rules.
Definition: nat.h:181
uint16_t privatePort
Internal TCP or UDP port number.
Definition: nat.h:159
NAT session.
Definition: nat.h:155
Ipv4Addr publicIpAddr
External IP address.
Definition: nat.h:161