Contiki-NG
Loading...
Searching...
No Matches
nat64-platform.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden AB.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \addtogroup nat64
33 * @{
34 *
35 * \file
36 * NAT64 platform interface — socket-based.
37 *
38 * Defines the session structure shared between the
39 * protocol-agnostic core and the platform layer, plus the
40 * small set of "send to IPv4" entry points the core invokes
41 * to forward packets out. The session struct is exposed
42 * (rather than opaque) so that nat64.c and nat64-tcp.c can
43 * read the address/port fields directly without accessor
44 * overhead, while the platform layer remains the sole owner
45 * of the file descriptor and connection state.
46 * \author
47 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
48 */
49
50#ifndef NAT64_PLATFORM_H_
51#define NAT64_PLATFORM_H_
52
53#include <stdbool.h>
54#include <stdint.h>
55#include "net/ipv6/uip.h"
56#include "sys/timer.h"
57
58/**
59 * \brief Transport protocol tracked by a NAT64 session.
60 *
61 * For NAT64_PROTO_ICMP, the session's ip6_peer_port field stores the
62 * ICMPv6 Echo identifier and ip4_remote_port is unused.
63 */
65 NAT64_PROTO_NONE,
66 NAT64_PROTO_UDP,
67 NAT64_PROTO_TCP,
68 NAT64_PROTO_ICMP,
69};
70
71/**
72 * \brief TCP connection state within the NAT64 splice proxy.
73 */
75 NAT64_TCP_CONNECTING, /**< Non-blocking connect() in progress. */
76 NAT64_TCP_ESTABLISHED, /**< Connection open, data can flow. */
77 NAT64_TCP_CLOSING, /**< Half-closed (SHUT_WR sent). */
78};
79
80/**
81 * \brief A NAT64 session binding an IoT node's IPv6 flow to an IPv4 socket.
82 */
84 bool active; /**< Session slot in use. */
85 enum nat64_session_proto proto; /**< UDP or TCP. */
86 int fd; /**< IPv4 socket file descriptor. */
87 uip_ip6addr_t ip6_peer; /**< IoT node's IPv6 address. */
88 uint16_t ip6_peer_port; /**< IoT node's transport port. */
89 uip_ip4addr_t ip4_remote; /**< IPv4 server address. */
90 uint16_t ip4_remote_port; /**< IPv4 server port. */
91 uint32_t peer_isn; /**< IoT node's ISN (TCP only). */
92 enum nat64_tcp_state tcp_state; /**< TCP connection state. */
93 struct timer expiry; /**< Session lifetime timer. */
94};
95
96/**
97 * \brief Initialize the platform layer.
98 * \return true on success, false on failure.
99 *
100 * On success, the NAT64 core has been activated and the platform is
101 * ready to create transport sessions for translated packets.
102 */
103bool nat64_platform_init(void);
104
105/**
106 * \brief Check whether the NAT64 gateway has been enabled at runtime.
107 * \return true if the user passed the platform's NAT64 enable option
108 * (e.g., `--nat64` on the native border router), false otherwise.
109 *
110 * Implemented by each platform layer alongside the option callback that
111 * sets the underlying flag.
112 */
113bool nat64_is_enabled(void);
114
115/**
116 * \brief Forward a UDP payload to an IPv4 server.
117 * \param dst IPv4 destination address.
118 * \param dstport Destination port (host byte order).
119 * \param ip6_src IoT node's IPv6 source address (used for session lookup).
120 * \param srcport Source port (host byte order).
121 * \param payload UDP payload bytes.
122 * \param len Payload length.
123 * \return Number of bytes sent, or -1 on error.
124 *
125 * Creates a new session and UDP socket if no matching session exists.
126 */
127int nat64_platform_udp_send(const uip_ip4addr_t *dst, uint16_t dstport,
128 const uip_ip6addr_t *ip6_src, uint16_t srcport,
129 const uint8_t *payload, uint16_t len);
130
131/**
132 * \brief Initiate a TCP connection to an IPv4 server.
133 * \param dst IPv4 destination address.
134 * \param dstport Destination port (host byte order).
135 * \param ip6_src IoT node's IPv6 source address.
136 * \param srcport Source port (host byte order).
137 * \param peer_isn The IoT node's initial sequence number.
138 * \return The session, or NULL on failure.
139 *
140 * The returned session may still be connecting. The platform calls
141 * nat64_tcp_established() later, from its event loop, once the IPv4
142 * connection is usable.
143 */
145 const uip_ip4addr_t *dst, uint16_t dstport,
146 const uip_ip6addr_t *ip6_src, uint16_t srcport,
147 uint32_t peer_isn);
148
149/**
150 * \brief Send data on an established TCP session.
151 * \param s The session (must be in ESTABLISHED state).
152 * \param data Data to send.
153 * \param len Data length.
154 * \return Number of bytes sent, 0 if would block, or -1 on error.
155 *
156 * A zero return means NAT64 did not retain the payload. The TCP core
157 * must leave its IoT-side acknowledgment point unchanged so the node
158 * retransmits the same bytes.
159 */
161 const uint8_t *data, uint16_t len);
162
163/**
164 * \brief Half-close a TCP session (send FIN).
165 * \param s The session to close.
166 */
168
169/**
170 * \brief Fully tear down a TCP session.
171 * \param s The session to destroy.
172 *
173 * Closes the IPv4 socket, releases the per-session sequence state, and
174 * frees the platform-layer session slot. After this call the session
175 * pointer is no longer valid. Use this when both sides have FIN'd and
176 * the connection is fully closed; for RST/abort semantics use
177 * ::nat64_platform_tcp_abort instead.
178 */
180
181/**
182 * \brief Abort a TCP session by sending RST upstream.
183 * \param s The session to abort.
184 *
185 * Sets SO_LINGER with a zero linger time so that close() emits a TCP
186 * RST instead of a graceful FIN, then tears down the session as in
187 * ::nat64_platform_tcp_destroy. Used when the IoT node sends a RST,
188 * so the IPv4 server sees an equivalent abort rather than a delayed
189 * graceful close.
190 */
192
193/**
194 * \brief Forward an ICMPv4 Echo Request to an IPv4 destination.
195 * \param dst IPv4 destination address.
196 * \param ip6_src IoT node's IPv6 source address.
197 * \param identifier ICMPv6 Echo identifier (host byte order).
198 * \param icmp_pkt ICMPv4 Echo Request bytes (type 8 + code + checksum
199 * + identifier + sequence + data).
200 * \param icmp_len Length of icmp_pkt in bytes.
201 * \return Number of bytes sent, or -1 on error.
202 *
203 * Allocates a session keyed on (ip6_src, identifier, dst) and a
204 * Linux unprivileged ICMP socket (SOCK_DGRAM, IPPROTO_ICMP). The
205 * session receives matching Echo Replies and forwards them via
206 * nat64_icmp_input().
207 */
209 const uip_ip6addr_t *ip6_src,
210 uint16_t identifier,
211 const uint8_t *icmp_pkt, uint16_t icmp_len);
212
213/** @} */
214
215#endif /* NAT64_PLATFORM_H_ */
void nat64_platform_tcp_destroy(struct nat64_session *s)
Fully tear down a TCP session.
Definition nat64-sock.c:576
int nat64_platform_udp_send(const uip_ip4addr_t *dst, uint16_t dstport, const uip_ip6addr_t *ip6_src, uint16_t srcport, const uint8_t *payload, uint16_t len)
Forward a UDP payload to an IPv4 server.
Definition nat64-sock.c:425
int nat64_platform_icmp_send(const uip_ip4addr_t *dst, const uip_ip6addr_t *ip6_src, uint16_t identifier, const uint8_t *icmp_pkt, uint16_t icmp_len)
Forward an ICMPv4 Echo Request to an IPv4 destination.
Definition nat64-sock.c:603
nat64_session_proto
Transport protocol tracked by a NAT64 session.
struct nat64_session * nat64_platform_tcp_connect(const uip_ip4addr_t *dst, uint16_t dstport, const uip_ip6addr_t *ip6_src, uint16_t srcport, uint32_t peer_isn)
Initiate a TCP connection to an IPv4 server.
Definition nat64-sock.c:477
void nat64_platform_tcp_close(struct nat64_session *s)
Half-close a TCP session (send FIN).
Definition nat64-sock.c:561
bool nat64_platform_init(void)
Initialize the platform layer.
Definition nat64-sock.c:690
void nat64_platform_tcp_abort(struct nat64_session *s)
Abort a TCP session by sending RST upstream.
Definition nat64-sock.c:586
int nat64_platform_tcp_send(struct nat64_session *s, const uint8_t *data, uint16_t len)
Send data on an established TCP session.
Definition nat64-sock.c:532
bool nat64_is_enabled(void)
Check whether the NAT64 gateway has been enabled at runtime.
Definition nat64-sock.c:724
nat64_tcp_state
TCP connection state within the NAT64 splice proxy.
@ NAT64_TCP_ESTABLISHED
Connection open, data can flow.
@ NAT64_TCP_CONNECTING
Non-blocking connect() in progress.
@ NAT64_TCP_CLOSING
Half-closed (SHUT_WR sent).
A NAT64 session binding an IoT node's IPv6 flow to an IPv4 socket.
bool active
Session slot in use.
uint16_t ip4_remote_port
IPv4 server port.
struct timer expiry
Session lifetime timer.
int fd
IPv4 socket file descriptor.
uip_ip6addr_t ip6_peer
IoT node's IPv6 address.
enum nat64_session_proto proto
UDP or TCP.
enum nat64_tcp_state tcp_state
TCP connection state.
uint32_t peer_isn
IoT node's ISN (TCP only).
uip_ip4addr_t ip4_remote
IPv4 server address.
uint16_t ip6_peer_port
IoT node's transport port.
A timer.
Definition timer.h:84
Timer library header file.
Header file for the uIP TCP/IP stack.
Representation of an IP address.
Definition uip.h:95