nat_misc.c File Reference

Helper functions for NAT. More...

#include "core/net.h"
#include "ipv4/ipv4_misc.h"
#include "ipv4/icmp.h"
#include "nat/nat.h"
#include "nat/nat_misc.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   NAT_TRACE_LEVEL
 

Functions

void natTick (NatContext *context)
 NAT timer handler. More...
 
bool_t natIsPublicInterface (NatContext *context, NetInterface *interface)
 Check whether a network interface is the WAN interface. More...
 
bool_t natIsPrivateInterface (NatContext *context, NetInterface *interface)
 Check whether a network interface is a LAN interface. More...
 
error_t natProcessPacket (NatContext *context, NetInterface *inInterface, const Ipv4PseudoHeader *inPseudoHeader, const NetBuffer *inBuffer, size_t inOffset, NetRxAncillary *ancillary)
 Process IP packet. More...
 
error_t natTranslateInboundPacket (NatContext *context, NatIpPacket *packet)
 Perform address translation (inbound packet) More...
 
error_t natTranslateOutboundPacket (NatContext *context, NatIpPacket *packet)
 Perform address translation (outbound packet) More...
 
error_t natForwardPacket (NatContext *context, const NatIpPacket *packet)
 Forward an IP packet to the specified interface. More...
 
NatPortFwdRulenatMatchPortFwdRule (NatContext *context, const NatIpPacket *packet)
 Search the port forwarding rules for a matching entry. More...
 
NatSessionnatMatchSession (NatContext *context, const NatIpPacket *packet)
 Search the NAT sessions for a matching entry. More...
 
NatSessionnatCreateSession (NatContext *context)
 Create a new NAT session. More...
 
uint16_t natAllocatePort (NatContext *context)
 Allocate a new port number. More...
 
uint16_t natAllocateIcmpQueryId (NatContext *context)
 Allocate a new ICMP query identifier. More...
 
error_t natParseTransportHeader (NatIpPacket *packet)
 Parse transport header (TCP, UDP or ICMP) More...
 
error_t natTranslateTransportHeader (const NatIpPacket *packet, const Ipv4PseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset)
 Translate transport header (TCP, UDP or ICMP) More...
 
void natDumpPacket (const NatIpPacket *packet)
 Dump IP packet for debugging purpose. More...
 

Variables

systime_t natTickCounter
 

Detailed Description

Helper functions for NAT.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.5.0

Definition in file nat_misc.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   NAT_TRACE_LEVEL

Definition at line 32 of file nat_misc.c.

Function Documentation

◆ natAllocateIcmpQueryId()

uint16_t natAllocateIcmpQueryId ( NatContext context)

Allocate a new ICMP query identifier.

Parameters
[in]contextPointer to the NAT context
Returns
ICMP query identifier

Definition at line 872 of file nat_misc.c.

◆ natAllocatePort()

uint16_t natAllocatePort ( NatContext context)

Allocate a new port number.

Parameters
[in]contextPointer to the NAT context
Returns
Port number

Definition at line 827 of file nat_misc.c.

◆ natCreateSession()

NatSession* natCreateSession ( NatContext context)

Create a new NAT session.

Parameters
[in]contextPointer to the NAT context
Returns
Pointer to the newly created session

Definition at line 771 of file nat_misc.c.

◆ natDumpPacket()

void natDumpPacket ( const NatIpPacket packet)

Dump IP packet for debugging purpose.

Parameters
[in]packetIP packet

Definition at line 1110 of file nat_misc.c.

◆ natForwardPacket()

error_t natForwardPacket ( NatContext context,
const NatIpPacket packet 
)

Forward an IP packet to the specified interface.

Parameters
[in]contextPointer to the NAT context
[in]packetIP packet
Returns
Error code

Definition at line 524 of file nat_misc.c.

◆ natIsPrivateInterface()

bool_t natIsPrivateInterface ( NatContext context,
NetInterface interface 
)

Check whether a network interface is a LAN interface.

Parameters
[in]contextPointer to the NAT context
[in]interfacePointer to a network interface
Returns
TRUE if the specified interface is a LAN interface, else FALSE

Definition at line 142 of file nat_misc.c.

◆ natIsPublicInterface()

bool_t natIsPublicInterface ( NatContext context,
NetInterface interface 
)

Check whether a network interface is the WAN interface.

Parameters
[in]contextPointer to the NAT context
[in]interfacePointer to a network interface
Returns
TRUE if the specified interface is the WAN interface, else FALSE

Definition at line 113 of file nat_misc.c.

◆ natMatchPortFwdRule()

NatPortFwdRule* natMatchPortFwdRule ( NatContext context,
const NatIpPacket packet 
)

Search the port forwarding rules for a matching entry.

Parameters
[in]contextPointer to the NAT context
[in]packetIP packet
Returns
Pointer to the matching port forwarding rule, if any

Definition at line 604 of file nat_misc.c.

◆ natMatchSession()

NatSession* natMatchSession ( NatContext context,
const NatIpPacket packet 
)

Search the NAT sessions for a matching entry.

Parameters
[in]contextPointer to the NAT context
[in]packetIP packet
Returns
Pointer to the matching session, if any

Definition at line 666 of file nat_misc.c.

◆ natParseTransportHeader()

error_t natParseTransportHeader ( NatIpPacket packet)

Parse transport header (TCP, UDP or ICMP)

Parameters
[in,out]packetIP packet
Returns
Error code

Definition at line 916 of file nat_misc.c.

◆ natProcessPacket()

error_t natProcessPacket ( NatContext context,
NetInterface inInterface,
const Ipv4PseudoHeader inPseudoHeader,
const NetBuffer inBuffer,
size_t  inOffset,
NetRxAncillary ancillary 
)

Process IP packet.

Parameters
[in]contextPointer to the NAT context
[in]inInterfacePointer to the interface where the packet was received
[in]inPseudoHeaderPointer to the pseudo header
[in]inBufferMulti-part buffer that holds the incoming IP packet
[in]inOffsetPacket Offset to the payload of the IP packet
[in]ancillaryAdditional options passed to the stack along with the packet
Returns
Error code

Definition at line 182 of file nat_misc.c.

◆ natTick()

void natTick ( NatContext context)

NAT timer handler.

Parameters
[in]contextPointer to the NAT context

Definition at line 54 of file nat_misc.c.

◆ natTranslateInboundPacket()

error_t natTranslateInboundPacket ( NatContext context,
NatIpPacket packet 
)

Perform address translation (inbound packet)

Parameters
[in]contextPointer to the NAT context
[in]packetIP packet
Returns
Error code

Definition at line 316 of file nat_misc.c.

◆ natTranslateOutboundPacket()

error_t natTranslateOutboundPacket ( NatContext context,
NatIpPacket packet 
)

Perform address translation (outbound packet)

Parameters
[in]contextPointer to the NAT context
[in]packetIP packet
Returns
Error code

Definition at line 381 of file nat_misc.c.

◆ natTranslateTransportHeader()

error_t natTranslateTransportHeader ( const NatIpPacket packet,
const Ipv4PseudoHeader pseudoHeader,
const NetBuffer buffer,
size_t  offset 
)

Translate transport header (TCP, UDP or ICMP)

Parameters
[in]packetIP packet
[in]pseudoHeaderPointer to the pseudo header
[in]bufferMulti-part buffer that holds the IP packet
[in]offsetPacket Offset to the payload of the IP packet
Returns
Error code

Definition at line 1021 of file nat_misc.c.

Variable Documentation

◆ natTickCounter

systime_t natTickCounter

Definition at line 46 of file nat_misc.c.