2022-10-17 09:13:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/nat/nat.h
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __NET_NAT_NAT_H
|
|
|
|
#define __NET_NAT_NAT_H
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2022-10-26 08:31:04 +02:00
|
|
|
#include <stdbool.h>
|
2022-10-17 09:13:34 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2022-12-20 04:28:34 +01:00
|
|
|
#include <nuttx/hashtable.h>
|
2022-10-17 09:13:34 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_NAT) && defined(CONFIG_NET_IPv4)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct ipv4_nat_entry
|
|
|
|
{
|
2022-12-20 04:28:34 +01:00
|
|
|
hash_node_t hash_inbound;
|
|
|
|
hash_node_t hash_outbound;
|
2022-10-17 09:13:34 +02:00
|
|
|
|
|
|
|
/* Local Network External Network
|
|
|
|
* |----------------|
|
|
|
|
* <local IP, | | <external IP, <peer IP,
|
|
|
|
* -----------| |-----------------------------
|
|
|
|
* local port> | | external port> peer port>
|
|
|
|
* |----------------|
|
|
|
|
*
|
2022-12-21 08:11:28 +01:00
|
|
|
* Full cone NAT only need to save local ip:port and external ip:port.
|
2024-03-14 08:22:13 +01:00
|
|
|
* Symmetric NAT need to save peer ip:port as well.
|
2022-12-21 08:11:28 +01:00
|
|
|
* For ICMP, save id in port field.
|
2022-10-17 09:13:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
in_addr_t local_ip; /* IP address of the local (private) host. */
|
2022-12-21 08:11:28 +01:00
|
|
|
in_addr_t external_ip; /* External IP address. */
|
2024-03-14 08:22:13 +01:00
|
|
|
#ifdef CONFIG_NET_NAT_SYMMETRIC
|
|
|
|
in_addr_t peer_ip; /* Peer IP address. */
|
|
|
|
#endif
|
2022-10-17 09:13:34 +02:00
|
|
|
uint16_t local_port; /* Port of the local (private) host. */
|
|
|
|
uint16_t external_port; /* The external port of local (private) host. */
|
2024-03-14 08:22:13 +01:00
|
|
|
#ifdef CONFIG_NET_NAT_SYMMETRIC
|
|
|
|
uint16_t peer_port; /* Peer port. */
|
|
|
|
#endif
|
2022-10-17 09:13:34 +02:00
|
|
|
uint8_t protocol; /* L4 protocol (TCP, UDP etc). */
|
|
|
|
|
2022-12-21 13:34:39 +01:00
|
|
|
int32_t expire_time; /* The expiration time of this entry. */
|
2022-10-17 09:13:34 +02:00
|
|
|
};
|
|
|
|
|
2022-11-14 09:50:21 +01:00
|
|
|
/* NAT IP/Port manipulate type, to indicate whether to manipulate source or
|
|
|
|
* destination IP/Port in a packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum nat_manip_type_e
|
|
|
|
{
|
|
|
|
NAT_MANIP_SRC,
|
|
|
|
NAT_MANIP_DST
|
|
|
|
};
|
|
|
|
|
2022-10-17 09:13:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_enable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enable NAT function on a network device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the outbound packets will be masqueraded.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if NAT function is successfully enabled on the device;
|
|
|
|
* A negated errno value is returned if failed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv4_nat_enable(FAR struct net_driver_s *dev);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_disable
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Disable NAT function on a network device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the NAT function will be disabled.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if NAT function is successfully disabled on the device;
|
|
|
|
* A negated errno value is returned if failed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv4_nat_disable(FAR struct net_driver_s *dev);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_inbound
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if a received packet belongs to a NAT entry. If so, translate it.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet is received.
|
|
|
|
* ipv4 - Points to the IPv4 header with dev->d_buf.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if NAT is successfully applied, or is not enabled for
|
|
|
|
* this packet;
|
|
|
|
* A negated errno value is returned if error occured.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv4_nat_inbound(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct ipv4_hdr_s *ipv4);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_outbound
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if we want to perform NAT with this outbound packet before sending
|
|
|
|
* it. If so, translate it.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which the packet will be sent.
|
|
|
|
* ipv4 - Points to the IPv4 header to be filled into dev->d_buf later.
|
2022-11-14 09:50:21 +01:00
|
|
|
* manip_type - Whether local IP/Port is in source or destination.
|
2022-10-17 09:13:34 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if NAT is successfully applied, or is not enabled for
|
|
|
|
* this packet;
|
|
|
|
* A negated errno value is returned if error occured.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv4_nat_outbound(FAR struct net_driver_s *dev,
|
2022-11-14 09:50:21 +01:00
|
|
|
FAR struct ipv4_hdr_s *ipv4,
|
|
|
|
enum nat_manip_type_e manip_type);
|
2022-10-17 09:13:34 +02:00
|
|
|
|
2022-10-26 08:31:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_port_inuse
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check whether a port is currently used by NAT.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* protocol - The L4 protocol of the packet.
|
|
|
|
* ip - The IP bind with the port (in network byte order).
|
|
|
|
* port - The port number to check (in network byte order).
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True if the port is already used by NAT, otherwise false.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool ipv4_nat_port_inuse(uint8_t protocol, in_addr_t ip, uint16_t port);
|
|
|
|
|
2022-12-22 04:35:02 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_entry_clear
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Clear all entries related to dev. Called when NAT will be disabled on
|
|
|
|
* any device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - The device on which NAT entries will be cleared.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* NAT is initialized.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ipv4_nat_entry_clear(FAR struct net_driver_s *dev);
|
|
|
|
|
2022-10-17 09:13:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_inbound_entry_find
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the inbound entry in NAT entry list.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* protocol - The L4 protocol of the packet.
|
2022-12-21 08:11:28 +01:00
|
|
|
* external_ip - The external ip of the packet, supports INADDR_ANY.
|
2022-10-17 09:13:34 +02:00
|
|
|
* external_port - The external port of the packet.
|
2024-03-14 08:22:13 +01:00
|
|
|
* peer_ip - The peer ip of the packet.
|
|
|
|
* peer_port - The peer port of the packet.
|
2022-11-01 11:13:50 +01:00
|
|
|
* refresh - Whether to refresh the selected entry.
|
2022-10-17 09:13:34 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to entry on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct ipv4_nat_entry *
|
2022-12-21 08:11:28 +01:00
|
|
|
ipv4_nat_inbound_entry_find(uint8_t protocol, in_addr_t external_ip,
|
2024-03-14 08:22:13 +01:00
|
|
|
uint16_t external_port, in_addr_t peer_ip,
|
|
|
|
uint16_t peer_port, bool refresh);
|
2022-10-17 09:13:34 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv4_nat_outbound_entry_find
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the outbound entry in NAT entry list. Create one if corresponding
|
|
|
|
* entry does not exist.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2022-10-28 10:08:36 +02:00
|
|
|
* dev - The device on which the packet will be sent.
|
2022-10-17 09:13:34 +02:00
|
|
|
* protocol - The L4 protocol of the packet.
|
|
|
|
* local_ip - The local ip of the packet.
|
|
|
|
* local_port - The local port of the packet.
|
2024-03-14 08:22:13 +01:00
|
|
|
* peer_ip - The peer ip of the packet.
|
|
|
|
* peer_port - The peer port of the packet.
|
2022-11-15 10:49:43 +01:00
|
|
|
* try_create - Try create the entry if no entry found.
|
2022-10-17 09:13:34 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to entry on success; null on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct ipv4_nat_entry *
|
2022-10-28 10:08:36 +02:00
|
|
|
ipv4_nat_outbound_entry_find(FAR struct net_driver_s *dev, uint8_t protocol,
|
2022-11-15 10:49:43 +01:00
|
|
|
in_addr_t local_ip, uint16_t local_port,
|
2024-03-14 08:22:13 +01:00
|
|
|
in_addr_t peer_ip, uint16_t peer_port,
|
2022-11-15 10:49:43 +01:00
|
|
|
bool try_create);
|
2022-10-17 09:13:34 +02:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET_NAT && CONFIG_NET_IPv4 */
|
|
|
|
#endif /* __NET_NAT_NAT_H */
|