aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/net/netfilter/udpclash.c
blob: 79de163d61ab7925d6aaf2f9d315bb49b40ed0c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-License-Identifier: GPL-2.0

/* Usage: ./udpclash <IP> <PORT>
 *
 * Emit THREAD_COUNT UDP packets sharing the same saddr:daddr pair.
 *
 * This mimics DNS resolver libraries that emit A and AAAA requests
 * in parallel.
 *
 * This exercises conntrack clash resolution logic added and later
 * refined in
 *
 *  71d8c47fc653 ("netfilter: conntrack: introduce clash resolution on insertion race")
 *  ed07d9a021df ("netfilter: nf_conntrack: resolve clash for matching conntracks")
 *  6a757c07e51f ("netfilter: conntrack: allow insertion of clashing entries")
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>

#define THREAD_COUNT 128

struct thread_args {
	const struct sockaddr_in *si_remote;
	int sockfd;
};

static volatile int wait = 1;

static void *thread_main(void *varg)
{
	const struct sockaddr_in *si_remote;
	const struct thread_args *args = varg;
	static const char msg[] = "foo";

	si_remote = args->si_remote;

	while (wait == 1)
		;

	if (sendto(args->sockfd, msg, strlen(msg), MSG_NOSIGNAL,
		   (struct sockaddr *)si_remote, sizeof(*si_remote)) < 0)
		exit(111);

	return varg;
}

static int run_test(int fd, const struct sockaddr_in *si_remote)
{
	struct thread_args thread_args = {
		.si_remote = si_remote,
		.sockfd = fd,
	};
	pthread_t *tid = calloc(THREAD_COUNT, sizeof(pthread_t));
	unsigned int repl_count = 0, timeout = 0;
	int i;

	if (!tid) {
		perror("calloc");
		return 1;
	}

	for (i = 0; i < THREAD_COUNT; i++) {
		int err = pthread_create(&tid[i], NULL, &thread_main, &thread_args);

		if (err != 0) {
			perror("pthread_create");
			exit(1);
		}
	}

	wait = 0;

	for (i = 0; i < THREAD_COUNT; i++)
		pthread_join(tid[i], NULL);

	while (repl_count < THREAD_COUNT) {
		struct sockaddr_in si_repl;
		socklen_t si_repl_len = sizeof(si_repl);
		char repl[512];
		ssize_t ret;

		ret = recvfrom(fd, repl, sizeof(repl), MSG_NOSIGNAL,
			       (struct sockaddr *) &si_repl, &si_repl_len);
		if (ret < 0) {
			if (timeout++ > 5000) {
				fputs("timed out while waiting for reply from thread\n", stderr);
				break;
			}

			/* give reply time to pass though the stack */
			usleep(1000);
			continue;
		}

		if (si_repl_len != sizeof(*si_remote)) {
			fprintf(stderr, "warning: reply has unexpected repl_len %d vs %d\n",
				(int)si_repl_len, (int)sizeof(si_repl));
		} else if (si_remote->sin_addr.s_addr != si_repl.sin_addr.s_addr ||
			si_remote->sin_port != si_repl.sin_port) {
			char a[64], b[64];

			inet_ntop(AF_INET, &si_remote->sin_addr, a, sizeof(a));
			inet_ntop(AF_INET, &si_repl.sin_addr, b, sizeof(b));

			fprintf(stderr, "reply from wrong source: want %s:%d got %s:%d\n",
				a, ntohs(si_remote->sin_port), b, ntohs(si_repl.sin_port));
		}

		repl_count++;
	}

	printf("got %d of %d replies\n", repl_count, THREAD_COUNT);

	free(tid);

	return repl_count == THREAD_COUNT ? 0 : 1;
}

int main(int argc, char *argv[])
{
	struct sockaddr_in si_local = {
		.sin_family = AF_INET,
	};
	struct sockaddr_in si_remote = {
		.sin_family = AF_INET,
	};
	int fd, ret;

	if (argc < 3) {
		fputs("Usage: send_udp <daddr> <dport>\n", stderr);
		return 1;
	}

	si_remote.sin_port = htons(atoi(argv[2]));
	si_remote.sin_addr.s_addr = inet_addr(argv[1]);

	fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_UDP);
	if (fd < 0) {
		perror("socket");
		return 1;
	}

	if (bind(fd, (struct sockaddr *)&si_local, sizeof(si_local)) < 0) {
		perror("bind");
		return 1;
	}

	ret = run_test(fd, &si_remote);

	close(fd);

	return ret;
}