aboutgitcodebugslistschat
path: root/doc/platform-requirements/reuseaddr-priority.c
blob: 644553f8d106f1bcbf8d21706ac4bf74abdf35ba (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-License-Identifier: GPL-2.0-or-later

/* reuseaddr-priority.c
 *
 * Verify which SO_REUSEADDR UDP sockets get priority to receive
 * =============================================================
 *
 * SO_REUSEADDR allows multiple sockets to bind to overlapping addresses, so
 * there can be multiple sockets eligible to receive the same packet.  The exact
 * semantics of which socket will receive in this circumstance isn't very well
 * documented.
 *
 * This program verifies that things behave the way we expect.  Specifically we
 * expect:
 *
 * - If both a connected and an unconnected socket could receive a datagram, the
 *   connected one will receive it in preference to the unconnected one.
 *
 * - If an unconnected socket bound to a specific address and an unconnected
 *   socket bound to the "any" address (0.0.0.0 or ::) could receive a datagram,
 *   then the one with a specific address will receive it in preference to the
 *   other.
 *
 * These should be true regardless of the order the sockets are created in, or
 * the order they're polled in.
 *
 * Copyright Red Hat
 * Author: David Gibson <david@gibson.dropbear.id.au>
 */

#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"

#define SRCPORT	13246U
#define DSTPORT	13247U

/* Different cases for receiving socket configuration */
enum sock_type {
	/* Socket is bound to 0.0.0.0:DSTPORT and not connected */
	SOCK_BOUND_ANY = 0,

	/* Socket is bound to 127.0.0.1:DSTPORT and not connected */
	SOCK_BOUND_LO = 1,

	/* Socket is bound to 0.0.0.0:DSTPORT and connected to 127.0.0.1:SRCPORT */
	SOCK_CONNECTED = 2,

	NUM_SOCK_TYPES,
};

typedef enum sock_type order_t[NUM_SOCK_TYPES];

static order_t orders[] = {
	{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0},
};

/* 127.0.0.2 */
#define INADDR_LOOPBACK2	((in_addr_t)(0x7f000002))

/* 0.0.0.0:DSTPORT */
static const struct sockaddr_in any_dst = SOCKADDR_INIT(INADDR_ANY, DSTPORT);
/* 127.0.0.1:DSTPORT */
static const struct sockaddr_in lo_dst = SOCKADDR_INIT(INADDR_LOOPBACK, DSTPORT);

/* 127.0.0.2:DSTPORT */
static const struct sockaddr_in lo2_dst = SOCKADDR_INIT(INADDR_LOOPBACK2, DSTPORT);

/* 127.0.0.1:SRCPORT */
static const struct sockaddr_in lo_src = SOCKADDR_INIT(INADDR_LOOPBACK, SRCPORT);

/* Random token to send in datagram */
static long token;

/* Get a socket of the specified type for receiving */
static int sock_recv(enum sock_type type)
{
	const struct sockaddr *connect_sa = NULL;
	const struct sockaddr *bind_sa = NULL;
	int s;

	s = sock_reuseaddr();

	switch (type) {
	case SOCK_CONNECTED:
		connect_sa = (struct sockaddr *)&lo_src;
		/* fallthrough */
	case SOCK_BOUND_ANY:
		bind_sa = (struct sockaddr *)&any_dst;
		break;

	case SOCK_BOUND_LO:
		bind_sa = (struct sockaddr *)&lo_dst;
		break;

	default:
		die("bug");
	}

	if (bind_sa)
		if (bind(s, bind_sa, sizeof(struct sockaddr_in)) < 0)
			die("bind(): %s\n", strerror(errno));
	if (connect_sa)
		if (connect(s, connect_sa, sizeof(struct sockaddr_in)) < 0)
			die("connect(): %s\n", strerror(errno));

	return s;
}

/* Get a socket suitable for sending to the given type of receiving socket */
static int sock_send(enum sock_type type)
{
	const struct sockaddr *connect_sa = NULL;
	const struct sockaddr *bind_sa = NULL;
	int s;

	s = sock_reuseaddr();

	switch (type) {
	case SOCK_BOUND_ANY:
		connect_sa = (struct sockaddr *)&lo2_dst;
		break;

	case SOCK_CONNECTED:
		bind_sa = (struct sockaddr *)&lo_src;
		/* fallthrough */
	case SOCK_BOUND_LO:
		connect_sa = (struct sockaddr *)&lo_dst;
		break;

	default:
		die("bug");
	}

	if (bind_sa)
		if (bind(s, bind_sa, sizeof(struct sockaddr_in)) < 0)
			die("bind(): %s\n", strerror(errno));
	if (connect_sa)
		if (connect(s, connect_sa, sizeof(struct sockaddr_in)) < 0)
			die("connect(): %s\n", strerror(errno));

	return s;
}

/* Check for expected behaviour with one specific ordering for various operations:
 *
 * @recv_create_order:	Order to create receiving sockets in
 * @send_create_order:	Order to create sending sockets in
 * @test_order:		Order to test the behaviour of different types
 * @recv_order:		Order to check the receiving sockets
 */
static void check_one_order(const order_t recv_create_order,
			    const order_t send_create_order,
			    const order_t test_order,
			    const order_t recv_order)
{
	int rs[NUM_SOCK_TYPES];
	int ss[NUM_SOCK_TYPES];
	int nfds = 0;
	int i, j;

	for (i = 0; i < NUM_SOCK_TYPES; i++) {
		enum sock_type t = recv_create_order[i];
		int s;

		s = sock_recv(t);
		if (s >= nfds)
			nfds = s + 1;

		rs[t] = s;
	}

	for (i = 0; i < NUM_SOCK_TYPES; i++) {
		enum sock_type t = send_create_order[i];

		ss[t] = sock_send(t);
	}

	for (i = 0; i < NUM_SOCK_TYPES; i++) {
		enum sock_type ti = test_order[i];
		int recv_via = -1;

		send_token(ss[ti], token);

		for (j = 0; j < NUM_SOCK_TYPES; j++) {
			enum sock_type tj = recv_order[j];

			if (recv_token(rs[tj], token)) {
				if (recv_via != -1)
					die("Received token more than once\n");
				recv_via = tj;
			}
		}

		if (recv_via == -1)
			die("Didn't receive token at all\n");
		if (recv_via != ti)
			die("Received token via unexpected socket\n");
	}

	for (i = 0; i < NUM_SOCK_TYPES; i++) {
		close(rs[i]);
		close(ss[i]);
	}
}

static void check_all_orders(void)
{
	int norders = sizeof(orders) / sizeof(orders[0]);
	int i, j, k, l;

	for (i = 0; i < norders; i++)
		for (j = 0; j < norders; j++)
			for (k = 0; k < norders; k++)
				for (l = 0; l < norders; l++)
					check_one_order(orders[i], orders[j],
							orders[j], orders[l]);
}

int main(int argc, char *argv[])
{
	(void)argc;
	(void)argv;

	token = random();

	check_all_orders();

	printf("SO_REUSEADDR receive priorities seem to work as expected\n");

	exit(0);
}