--- /dev/null
+#!/bin/bash
+set -e
+
+# Require root permissions
+test "$(id -u)" = "0" || exit 77
+
+prefix="pmtu_"
+
+# (Re)create the network namespaces we are going to use
+for ns in n nn p pn r; do
+ ip netns delete ${prefix}${ns} 2>/dev/null || true
+ ip netns add ${prefix}${ns}
+ ip netns exec ${prefix}${ns} ip link set dev lo up
+done
+
+# (Re)create the WAN bridge
+ip link del ${prefix}br0 2>/dev/null || true
+ip link add ${prefix}br0 type bridge
+ip link set dev ${prefix}br0 up
+
+# Set up the public interfaces of the nats and relay, and connect them to the bridge
+for ns in nn pn r; do
+ ip link del ${prefix}${ns}_p0 2>/dev/null || true
+ ip link del ${prefix}${ns}_e0 2>/dev/null || true
+ ip link add ${prefix}${ns}_e0 type veth peer name ${prefix}${ns}_p0
+ ip link set ${prefix}${ns}_e0 netns ${prefix}${ns} name eth0
+ ip link set ${prefix}${ns}_p0 master ${prefix}br0 up
+done
+
+# Set up the LAN interfaces
+for ns in n p; do
+ ip link add ${prefix}${ns}_e0 type veth peer name ${prefix}${ns}n_e1
+ ip link set ${prefix}${ns}_e0 netns ${prefix}${ns} name eth0
+ ip link set ${prefix}${ns}n_e1 netns ${prefix}${ns}n name eth1
+ ip netns exec ${prefix}${ns} iptables -I INPUT -p tcp -s 203.0.113.2 -j DROP
+ ip netns exec ${prefix}${ns} iptables -I INPUT -p tcp -s 203.0.113.3 -j DROP
+done
+
+# Configure the IP addresses of all interfaces
+while read ns iface addr gw; do
+ ip netns exec ${prefix}${ns} ip addr add ${addr} dev ${iface}
+ ip netns exec ${prefix}${ns} ip link set dev ${iface} up
+ if [ -n "${gw}" ]; then
+ ip netns exec ${prefix}${ns} ip route add default via ${gw}
+ fi
+done << EOF
+n eth0 192.168.1.2/24 192.168.1.1
+nn eth0 203.0.113.2/24
+nn eth1 192.168.1.1/24
+p eth0 192.168.1.2/24 192.168.1.1
+pn eth0 203.0.113.3/24
+pn eth1 192.168.1.1/24
+r eth0 203.0.113.1/24 203.0.113.254
+EOF
+
+# Enable NAT
+while read ns wan_addr; do
+ ip netns exec ${prefix}${ns} iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-destination 192.168.1.2
+ ip netns exec ${prefix}${ns} iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source ${wan_addr}
+done << EOF
+nn 203.0.113.2
+pn 203.0.113.3
+EOF
--- /dev/null
+#ifndef NDEBUG
+#undef NDEBUG
+#endif
+
+#include <assert.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "meshlink.h"
+#include "utils.h"
+
+#define nnodes 3
+
+static const struct info {
+ const char *name;
+ const char *confdir;
+ const char *netns;
+ dev_class_t devclass;
+} nodes[nnodes] = {
+ {"relay", "pmtu_conf.1", "/run/netns/pmtu_r", DEV_CLASS_BACKBONE},
+ {"peer", "pmtu_conf.2", "/run/netns/pmtu_p", DEV_CLASS_STATIONARY},
+ {"nut", "pmtu_conf.3", "/run/netns/pmtu_n", DEV_CLASS_STATIONARY},
+};
+
+static struct state {
+ meshlink_handle_t *mesh;
+ int netns;
+ struct sync_flag up_flag;
+ int pmtu;
+ int probe_count;
+ int probe_bytes;
+} states[nnodes];
+
+static void relay_up_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
+ struct state *state = mesh->priv;
+
+ // Check that we are connected to another peer besides the relay
+ if(reachable && node != meshlink_get_self(mesh) && strcmp(node->name, "relay")) {
+ set_sync_flag(&state->up_flag, true);
+ meshlink_set_node_status_cb(mesh, NULL);
+ }
+}
+
+static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
+ if(!data && !len) {
+ meshlink_channel_close(mesh, channel);
+ }
+}
+
+static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
+ (void)port;
+ (void)data;
+ (void)len;
+ meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
+ return true;
+}
+
+static void parse_log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
+ if(level >= MESHLINK_INFO) {
+ log_cb(mesh, level, text);
+ }
+
+ struct state *state = mesh->priv;
+
+ if(state->pmtu != 0) {
+ return;
+ }
+
+ int len;
+ char name[10] = "";
+
+ if(sscanf(text, "Sending UDP probe length %d to %9s", &len, name) == 2 || sscanf(text, "Got PMTU probe length %d from %s", &len, name) == 2) {
+ if(!strcmp(name, "nut")) {
+ state->probe_count++;
+ state->probe_bytes += len;
+ }
+ } else if(sscanf(text, "Fixing PMTU of %9s to %d", name, &len) == 2) {
+ if(!strcmp(name, "nut")) {
+ state->pmtu = len;
+ }
+ }
+}
+
+static void wait_for_pmtu(void) {
+ // Set up a channel from peer to nut
+ meshlink_set_channel_accept_cb(states[2].mesh, accept_cb);
+ meshlink_channel_t *channel = meshlink_channel_open(states[1].mesh, meshlink_get_node(states[1].mesh, nodes[2].name), 1, NULL, NULL, 0);
+ assert(channel);
+
+ // While sending regular data, wait for PMTU discovery to finish
+ for(int i = 0; i < 30; i++) {
+ sleep(1);
+
+ if(states[1].pmtu) {
+ break;
+ }
+
+ assert(meshlink_channel_send(states[1].mesh, channel, "ping", 4) == 4);
+ }
+
+ meshlink_channel_close(states[1].mesh, channel);
+}
+
+static void start_peer_nut(void) {
+ // Start peer and nut
+ for(int i = 1; i < nnodes; i++) {
+ meshlink_set_node_status_cb(states[i].mesh, relay_up_cb);
+ assert(meshlink_start(states[i].mesh));
+ }
+
+ // Wait for the peer and nut to see each other
+ for(int i = 1; i < nnodes; i++) {
+ assert(wait_sync_flag(&states[i].up_flag, 5));
+ }
+}
+
+static void stop_peer_nut(void) {
+ // Stop peer and nut, reset counters
+ for(int i = 1; i < nnodes; i++) {
+ meshlink_stop(states[i].mesh);
+ states[i].up_flag.flag = false;
+ states[i].pmtu = 0;
+ states[i].probe_count = 0;
+ states[i].probe_bytes = 0;
+ }
+}
+
+int main(void) {
+ // This test requires root access due to the use of network namespaces
+ if(getuid() != 0) {
+ return 77;
+ }
+
+ // Set up namespaces
+ assert(system("./pmtu-setup") == 0);
+
+ // Bring up the nodes
+ for(int i = 0; i < nnodes; i++) {
+ assert(meshlink_destroy(nodes[i].confdir));
+
+ // Open the network namespace
+ states[i].netns = open(nodes[i].netns, O_RDONLY);
+ assert(states[i].netns != -1);
+
+ // Open the MeshLink instance
+ meshlink_open_params_t *params;
+ assert(params = meshlink_open_params_init(nodes[i].confdir, nodes[i].name, "pmtu", nodes[i].devclass));
+ assert(meshlink_open_params_set_netns(params, states[i].netns));
+ assert(states[i].mesh = meshlink_open_ex(params));
+ free(params);
+
+ states[i].mesh->priv = &states[i];
+ meshlink_enable_discovery(states[i].mesh, false);
+ init_sync_flag(&states[i].up_flag);
+
+ meshlink_set_log_cb(states[i].mesh, MESHLINK_DEBUG, parse_log_cb);
+
+ // Link the relay node to the other nodes
+ if(i > 0) {
+ link_meshlink_pair(states[0].mesh, states[i].mesh);
+ }
+ }
+
+ // Start the relay
+ assert(meshlink_start(states[0].mesh));
+
+ // Start peers and wait for them to connect
+ start_peer_nut();
+
+ // Wait for PMTU discovery to finish
+ wait_for_pmtu();
+
+ assert(states[1].pmtu >= 1400 && states[1].pmtu <= 1500);
+ assert(states[1].probe_count <= 10);
+ assert(states[1].probe_bytes <= 1500 * 10);
+
+ // Drop the MTU to 800
+ stop_peer_nut();
+
+ assert(system("ip netns exec pmtu_p ip link set eth0 mtu 800") == 0);
+ assert(system("ip netns exec pmtu_n ip link set eth0 mtu 800") == 0);
+
+ // Workaround for autoconnect algorithm throttling reconnects
+ sleep(15);
+
+ start_peer_nut();
+ wait_for_pmtu();
+
+ assert(states[1].pmtu >= 700 && states[1].pmtu <= 800);
+ assert(states[1].probe_count <= 20);
+ assert(states[1].probe_bytes <= 800 * 20);
+
+ // Cleanup
+ for(int i = 0; i < nnodes; i++) {
+ meshlink_close(states[i].mesh);
+ }
+}