Ethernet NAT Bridge

NATBridge connects a simulated Ethernet network to host TCP sockets. It is useful when target software should accept a TCP connection from the host without creating a TAP/TUN device or changing host network configuration.

The bridge is part of the BusModels plugin and acts as an Ethernet PHY on the simulated link. It has its own simulated MAC address and IPv4 address. Incoming host TCP connections are mapped to configured target IPv4 addresses and TCP ports inside the simulated network.

Setup

Create an Ethernet link and NAT bridge, configure the bridge address, connect it to the link, and add the TCP port mappings:

$ temu --project=temu-project.yaml
temu> import BusModels
temu> EthernetLink.new name=eth0
temu> NATBridge.new name=nat0
temu> nat0.set-mac mac=02:00:00:00:00:01
temu> nat0.set-ip ip=192.168.0.1
temu> eth-connect link=eth0 device=nat0:PHYIface

Connect the board Ethernet PHY to the same link. For a board MAC that uses a GenericPHY, this looks like:

temu> GenericPHY.new name=board0-phy0
temu> connect a=board0-eth0.phy b=board0-phy0:PHYIface
temu> connect a=board0-phy0.macDevice b=board0-eth0:MACIface
temu> eth-connect link=eth0 device=board0-phy0:PHYIface

If the board component already creates a PHY object, connect that object to eth0 instead.

Add a NAT mapping for each TCP service that should be reachable from the host. This example maps host port 10023 to target address 192.168.0.10, target port 23:

temu> nat0.add-nat name=telnet internal-addr=192.168.0.10 internal-port=23 external-port=10023
temu> nat0.start-nat

After the target software has configured its MAC and IP address and started the TCP service, connect from the host:

$ telnet localhost 10023

Use pmap to inspect the configured port mappings:

temu> nat0.pmap

ARP

The NAT bridge uses ARP to resolve target MAC addresses. If the target has not yet sent ARP traffic, ask the bridge to send an ARP request:

temu> nat0.do-arp-request ip=192.168.0.10
temu> nat0.parp

The target software must be configured so that nat0 and the target Ethernet interface are on the same IPv4 subnet, or the target must route replies back to the NAT bridge address.

Notes

NATBridge is a TCP bridge, not a full host network adapter. It forwards configured TCP connections between host sockets and the simulated Ethernet network. Use EthernetLink capture when debugging packet-level behavior:

temu> eth-enable-capture link=eth0 file=eth0.pcapng

For line-oriented debug protocols, enable-crlf-substitution can replace incoming CRLF sequences with \0:

temu> nat0.enable-crlf-substitution