test-route.c 56.9 KB
Newer Older
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
17
 * Copyright 2016 - 2017 Red Hat, Inc.
18 19 20
 */

#include "nm-default.h"
21

22
#include <linux/rtnetlink.h>
23
#include <linux/fib_rules.h>
24

25
#include "nm-core-utils.h"
26
#include "platform/nm-platform-utils.h"
27
#include "platform/nmp-rules-manager.h"
Pavel Šimerda's avatar
Pavel Šimerda committed
28

29
#include "test-common.h"
30

31 32
#define DEVICE_IFINDEX NMTSTP_ENV1_IFINDEX
#define EX             NMTSTP_ENV1_EX
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
static void
_wait_for_ipv4_addr_device_route (NMPlatform *platform,
                                  gint64 timeout_ms,
                                  int ifindex,
                                  in_addr_t addr,
                                  guint8 plen)
{
	/* Wait that the addresses gets a device-route. After adding a address,
	 * the device route is not added immediately. It takes a moment... */

	addr = nm_utils_ip4_address_clear_host_address (addr, plen);
	NMTST_WAIT_ASSERT (400, {
		NMDedupMultiIter iter;
		NMPLookup lookup;
		const NMPObject *o;

		nmp_cache_iter_for_each (&iter,
		                         nm_platform_lookup (platform,
52 53 54
		                                             nmp_lookup_init_object (&lookup,
		                                                                     NMP_OBJECT_TYPE_IP4_ROUTE,
		                                                                     ifindex)),
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		                         &o) {
			const NMPlatformIP4Route *r = NMP_OBJECT_CAST_IP4_ROUTE (o);

			if (   r->plen == plen
			    && addr == nm_utils_ip4_address_clear_host_address (r->network, plen)
			    && r->metric == 0
			    && r->scope_inv == nm_platform_route_scope_inv (RT_SCOPE_LINK)
			    && r->rt_source == NM_IP_CONFIG_SOURCE_RTPROT_KERNEL)
				return;
		}
		nmtstp_assert_wait_for_signal (platform,
		                               (nmtst_wait_end_us - g_get_monotonic_time ()) / 1000);
	});
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83
static void
_wait_for_ipv6_addr_non_tentative (NMPlatform *platform,
                                   gint64 timeout_ms,
                                   int ifindex,
                                   guint addr_n,
                                   const struct in6_addr *addrs)
{
	guint i;

	/* Wait that the addresses become non-tentative.  Dummy interfaces are NOARP
	 * and thus don't do DAD, but the kernel sets the address as tentative for a
	 * small amount of time, which prevents the immediate addition of the route
	 * with RTA_PREFSRC */

84
	NMTST_WAIT_ASSERT (timeout_ms, {
85 86 87 88
		gboolean should_wait = FALSE;
		const NMPlatformIP6Address *plt_addr;

		for (i = 0; i < addr_n; i++) {
89
			plt_addr = nm_platform_ip6_address_get (platform, ifindex, addrs[i]);
90 91 92 93 94 95 96 97
			if (   !plt_addr
			    || NM_FLAGS_HAS (plt_addr->n_ifa_flags, IFA_F_TENTATIVE)) {
				should_wait = TRUE;
				break;
			}
		}
		if (!should_wait)
			return;
98
		nmtstp_assert_wait_for_signal (platform,
99 100 101 102
		                               (nmtst_wait_end_us - g_get_monotonic_time ()) / 1000);
	});
}

Pavel Šimerda's avatar
Pavel Šimerda committed
103
static void
104
ip4_route_callback (NMPlatform *platform, int obj_type_i, int ifindex, const NMPlatformIP4Route *received, int change_type_i, SignalData *data)
Pavel Šimerda's avatar
Pavel Šimerda committed
105
{
106 107
	const NMPObjectType obj_type = obj_type_i;
	const NMPlatformSignalChangeType change_type = change_type_i;
108 109
	NMPObject o_id;
	nm_auto_nmpobj NMPObject *o_id_p = nmp_object_new (NMP_OBJECT_TYPE_IP4_ROUTE, NULL);
110 111

	g_assert_cmpint (obj_type, ==, NMP_OBJECT_TYPE_IP4_ROUTE);
Pavel Šimerda's avatar
Pavel Šimerda committed
112
	g_assert (received);
113
	g_assert_cmpint (received->ifindex, ==, ifindex);
114 115
	g_assert (data && data->name);
	g_assert_cmpstr (data->name, ==, NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED);
Pavel Šimerda's avatar
Pavel Šimerda committed
116

117 118 119 120 121
	/* run code for initializing the ID only */
	nmp_object_stackinit_id (&o_id, NMP_OBJECT_UP_CAST (received));
	nmp_object_copy (o_id_p, NMP_OBJECT_UP_CAST (received), TRUE);
	nmp_object_copy (o_id_p, NMP_OBJECT_UP_CAST (received), FALSE);

Pavel Šimerda's avatar
Pavel Šimerda committed
122 123
	if (data->ifindex && data->ifindex != received->ifindex)
		return;
124 125
	if (data->change_type != change_type)
		return;
Pavel Šimerda's avatar
Pavel Šimerda committed
126 127 128 129

	if (data->loop)
		g_main_loop_quit (data->loop);

130
	data->received_count++;
131
	_LOGD ("Received signal '%s' %dth time.", data->name, data->received_count);
Pavel Šimerda's avatar
Pavel Šimerda committed
132 133 134
}

static void
135
ip6_route_callback (NMPlatform *platform, int obj_type_i, int ifindex, const NMPlatformIP6Route *received, int change_type_i, SignalData *data)
Pavel Šimerda's avatar
Pavel Šimerda committed
136
{
137 138
	const NMPObjectType obj_type = obj_type_i;
	const NMPlatformSignalChangeType change_type = change_type_i;
139 140
	NMPObject o_id;
	nm_auto_nmpobj NMPObject *o_id_p = nmp_object_new (NMP_OBJECT_TYPE_IP6_ROUTE, NULL);
141 142

	g_assert_cmpint (obj_type, ==, NMP_OBJECT_TYPE_IP6_ROUTE);
Pavel Šimerda's avatar
Pavel Šimerda committed
143
	g_assert (received);
144
	g_assert_cmpint (received->ifindex, ==, ifindex);
145 146
	g_assert (data && data->name);
	g_assert_cmpstr (data->name, ==, NM_PLATFORM_SIGNAL_IP6_ROUTE_CHANGED);
Pavel Šimerda's avatar
Pavel Šimerda committed
147

148 149 150 151 152
	/* run code for initializing the ID only */
	nmp_object_stackinit_id (&o_id, NMP_OBJECT_UP_CAST (received));
	nmp_object_copy (o_id_p, NMP_OBJECT_UP_CAST (received), TRUE);
	nmp_object_copy (o_id_p, NMP_OBJECT_UP_CAST (received), FALSE);

Pavel Šimerda's avatar
Pavel Šimerda committed
153 154
	if (data->ifindex && data->ifindex != received->ifindex)
		return;
155 156
	if (data->change_type != change_type)
		return;
Pavel Šimerda's avatar
Pavel Šimerda committed
157 158 159 160

	if (data->loop)
		g_main_loop_quit (data->loop);

161
	data->received_count++;
162
	_LOGD ("Received signal '%s' %dth time.", data->name, data->received_count);
Pavel Šimerda's avatar
Pavel Šimerda committed
163 164
}

165 166 167
static void
test_ip4_route_metric0 (void)
{
168
	int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
169
	SignalData *route_added = add_signal (NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip4_route_callback);
170
	SignalData *route_changed = add_signal (NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, ip4_route_callback);
171 172 173 174 175 176 177
	SignalData *route_removed = add_signal (NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip4_route_callback);
	in_addr_t network = nmtst_inet4_from_string ("192.0.2.5"); /* from 192.0.2.0/24 (TEST-NET-1) (rfc5737) */
	int plen = 32;
	int metric = 22987;
	int mss = 1000;

	/* No routes initially */
178 179
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, metric, 0);
180 181

	/* add the first route */
182
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, network, plen, INADDR_ANY, 0, metric, mss);
183 184
	accept_signal (route_added);

185 186
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 1,  DEVICE_NAME, network, plen, metric, 0);
187 188

	/* Deleting route with metric 0 does nothing */
189
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, network, plen, 0));
190
	ensure_no_signal (route_removed);
191

192 193
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 1,  DEVICE_NAME, network, plen, metric, 0);
194 195

	/* add the second route */
196
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, network, plen, INADDR_ANY, 0, 0, mss);
197 198
	accept_signal (route_added);

199 200
	nmtstp_assert_ip4_route_exists (NULL, 1,  DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 1,  DEVICE_NAME, network, plen, metric, 0);
201 202

	/* Delete route with metric 0 */
203
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, network, plen, 0));
204 205
	accept_signal (route_removed);

206 207
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 1,  DEVICE_NAME, network, plen, metric, 0);
208 209

	/* Delete route with metric 0 again (we expect nothing to happen) */
210
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, network, plen, 0));
211
	ensure_no_signal (route_removed);
212

213 214
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 1,  DEVICE_NAME, network, plen, metric, 0);
215 216

	/* Delete the other route */
217
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, network, plen, metric));
218 219
	accept_signal (route_removed);

220 221
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, 0, 0);
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, metric, 0);
222 223

	free_signal (route_added);
224
	free_signal (route_changed);
225 226 227
	free_signal (route_removed);
}

Pavel Šimerda's avatar
Pavel Šimerda committed
228
static void
229
test_ip4_route (void)
Pavel Šimerda's avatar
Pavel Šimerda committed
230
{
231
	int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
232 233 234
	SignalData *route_added = add_signal (NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip4_route_callback);
	SignalData *route_changed = add_signal (NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, ip4_route_callback);
	SignalData *route_removed = add_signal (NM_PLATFORM_SIGNAL_IP4_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip4_route_callback);
235
	GPtrArray *routes;
236
	NMPlatformIP4Route rts[3];
Pavel Šimerda's avatar
Pavel Šimerda committed
237
	in_addr_t network;
238
	guint8 plen = 24;
Pavel Šimerda's avatar
Pavel Šimerda committed
239
	in_addr_t gateway;
240 241
	/* Choose a high metric so that we hopefully don't conflict. */
	int metric = 22986;
Pavel Šimerda's avatar
Pavel Šimerda committed
242 243
	int mss = 1000;

244 245
	inet_pton (AF_INET, "192.0.3.0", &network);
	inet_pton (AF_INET, "198.51.100.1", &gateway);
Pavel Šimerda's avatar
Pavel Šimerda committed
246 247

	/* Add route to gateway */
248
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, gateway, 32, INADDR_ANY, 0, metric, mss);
Pavel Šimerda's avatar
Pavel Šimerda committed
249 250 251
	accept_signal (route_added);

	/* Add route */
252
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, metric, 0);
253
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, network, plen, gateway, 0, metric, mss);
254
	nmtstp_assert_ip4_route_exists (NULL, 1, DEVICE_NAME, network, plen, metric, 0);
Pavel Šimerda's avatar
Pavel Šimerda committed
255 256 257
	accept_signal (route_added);

	/* Add route again */
258
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, network, plen, gateway, 0, metric, mss);
259
	accept_signals (route_changed, 0, 1);
Pavel Šimerda's avatar
Pavel Šimerda committed
260

261
	/* Add default route */
262
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, 0, 0, metric, 0);
263
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, 0, 0, gateway, 0, metric, mss);
264
	nmtstp_assert_ip4_route_exists (NULL, 1, DEVICE_NAME, 0, 0, metric, 0);
265 266 267
	accept_signal (route_added);

	/* Add default route again */
268
	nmtstp_ip4_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, 0, 0, gateway, 0, metric, mss);
269
	accept_signals (route_changed, 0, 1);
270

Pavel Šimerda's avatar
Pavel Šimerda committed
271
	/* Test route listing */
272
	routes = nmtstp_ip4_route_get_all (NM_PLATFORM_GET, ifindex);
Pavel Šimerda's avatar
Pavel Šimerda committed
273
	memset (rts, 0, sizeof (rts));
274
	rts[0].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER);
Pavel Šimerda's avatar
Pavel Šimerda committed
275 276 277 278 279
	rts[0].network = gateway;
	rts[0].plen = 32;
	rts[0].ifindex = ifindex;
	rts[0].gateway = INADDR_ANY;
	rts[0].metric = metric;
280
	rts[0].mss = mss;
281
	rts[0].scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK);
282
	rts[1].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER);
Pavel Šimerda's avatar
Pavel Šimerda committed
283 284 285 286 287
	rts[1].network = network;
	rts[1].plen = plen;
	rts[1].ifindex = ifindex;
	rts[1].gateway = gateway;
	rts[1].metric = metric;
288
	rts[1].mss = mss;
289
	rts[1].scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE);
290
	rts[2].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER);
291 292 293 294 295 296
	rts[2].network = 0;
	rts[2].plen = 0;
	rts[2].ifindex = ifindex;
	rts[2].gateway = gateway;
	rts[2].metric = metric;
	rts[2].mss = mss;
297
	rts[2].scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE);
298
	g_assert_cmpint (routes->len, ==, 3);
299 300
	nmtst_platform_ip4_routes_equal_aptr ((const NMPObject *const*) routes->pdata, rts, routes->len, TRUE);
	g_ptr_array_unref (routes);
Pavel Šimerda's avatar
Pavel Šimerda committed
301 302

	/* Remove route */
303
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, network, plen, metric));
304
	nmtstp_assert_ip4_route_exists (NULL, 0, DEVICE_NAME, network, plen, metric, 0);
Pavel Šimerda's avatar
Pavel Šimerda committed
305 306 307
	accept_signal (route_removed);

	/* Remove route again */
308
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, network, plen, metric));
Pavel Šimerda's avatar
Pavel Šimerda committed
309

310
	/* Remove default route */
311
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, 0, 0, metric));
312 313 314
	accept_signal (route_removed);

	/* Remove route to gateway */
315
	g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, ifindex, gateway, 32, metric));
316 317
	accept_signal (route_removed);

Pavel Šimerda's avatar
Pavel Šimerda committed
318
	free_signal (route_added);
319
	free_signal (route_changed);
Pavel Šimerda's avatar
Pavel Šimerda committed
320 321 322 323
	free_signal (route_removed);
}

static void
324
test_ip6_route (void)
Pavel Šimerda's avatar
Pavel Šimerda committed
325
{
326
	int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
327 328 329
	SignalData *route_added = add_signal (NM_PLATFORM_SIGNAL_IP6_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_ADDED, ip6_route_callback);
	SignalData *route_changed = add_signal (NM_PLATFORM_SIGNAL_IP6_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, ip6_route_callback);
	SignalData *route_removed = add_signal (NM_PLATFORM_SIGNAL_IP6_ROUTE_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, ip6_route_callback);
330
	GPtrArray *routes;
331
	NMPlatformIP6Route rts[3];
Pavel Šimerda's avatar
Pavel Šimerda committed
332
	struct in6_addr network;
333
	guint8 plen = 64;
334
	struct in6_addr gateway, pref_src;
335 336
	/* Choose a high metric so that we hopefully don't conflict. */
	int metric = 22987;
Pavel Šimerda's avatar
Pavel Šimerda committed
337 338 339 340
	int mss = 1000;

	inet_pton (AF_INET6, "2001:db8:a:b:0:0:0:0", &network);
	inet_pton (AF_INET6, "2001:db8:c:d:1:2:3:4", &gateway);
341 342 343 344 345
	inet_pton (AF_INET6, "::42", &pref_src);

	g_assert (nm_platform_ip6_address_add (NM_PLATFORM_GET, ifindex, pref_src, 128, in6addr_any,
	                                       NM_PLATFORM_LIFETIME_PERMANENT, NM_PLATFORM_LIFETIME_PERMANENT, 0));
	accept_signals (route_added, 0, 1);
Pavel Šimerda's avatar
Pavel Šimerda committed
346

347
	_wait_for_ipv6_addr_non_tentative (NM_PLATFORM_GET, 200, ifindex, 1, &pref_src);
348

349
	/* Add route to gateway */
350
	nmtstp_ip6_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, gateway, 128, in6addr_any, in6addr_any, metric, mss);
Pavel Šimerda's avatar
Pavel Šimerda committed
351 352 353
	accept_signal (route_added);

	/* Add route */
354
	g_assert (!nmtstp_ip6_route_get (NM_PLATFORM_GET, ifindex, &network, plen, metric, NULL, 0));
355
	nmtstp_ip6_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, network, plen, gateway, pref_src, metric, mss);
356
	g_assert (nmtstp_ip6_route_get (NM_PLATFORM_GET, ifindex, &network, plen, metric, NULL, 0));
Pavel Šimerda's avatar
Pavel Šimerda committed
357 358 359
	accept_signal (route_added);

	/* Add route again */
360
	nmtstp_ip6_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, network, plen, gateway, pref_src, metric, mss);
361
	accept_signals (route_changed, 0, 1);
Pavel Šimerda's avatar
Pavel Šimerda committed
362

363
	/* Add default route */
364
	g_assert (!nmtstp_ip6_route_get (NM_PLATFORM_GET, ifindex, &in6addr_any, 0, metric, NULL, 0));
365
	nmtstp_ip6_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, in6addr_any, 0, gateway, in6addr_any, metric, mss);
366
	g_assert (nmtstp_ip6_route_get (NM_PLATFORM_GET, ifindex, &in6addr_any, 0, metric, NULL, 0));
367 368 369
	accept_signal (route_added);

	/* Add default route again */
370
	nmtstp_ip6_route_add (NM_PLATFORM_GET, ifindex, NM_IP_CONFIG_SOURCE_USER, in6addr_any, 0, gateway, in6addr_any, metric, mss);
371
	accept_signals (route_changed, 0, 1);
372

Pavel Šimerda's avatar
Pavel Šimerda committed
373
	/* Test route listing */
374
	routes = nmtstp_ip6_route_get_all (NM_PLATFORM_GET, ifindex);
Pavel Šimerda's avatar
Pavel Šimerda committed
375
	memset (rts, 0, sizeof (rts));
376
	rts[0].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER);
Pavel Šimerda's avatar
Pavel Šimerda committed
377 378 379 380
	rts[0].network = gateway;
	rts[0].plen = 128;
	rts[0].ifindex = ifindex;
	rts[0].gateway = in6addr_any;
381
	rts[0].pref_src = in6addr_any;
382
	rts[0].metric = nm_utils_ip6_route_metric_normalize (metric);
383
	rts[0].mss = mss;
384
	rts[1].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER);
Pavel Šimerda's avatar
Pavel Šimerda committed
385 386 387 388
	rts[1].network = network;
	rts[1].plen = plen;
	rts[1].ifindex = ifindex;
	rts[1].gateway = gateway;
389
	rts[1].pref_src = pref_src;
390
	rts[1].metric = nm_utils_ip6_route_metric_normalize (metric);
391
	rts[1].mss = mss;
392
	rts[2].rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER);
393 394 395 396
	rts[2].network = in6addr_any;
	rts[2].plen = 0;
	rts[2].ifindex = ifindex;
	rts[2].gateway = gateway;
397
	rts[2].pref_src = in6addr_any;
398
	rts[2].metric = nm_utils_ip6_route_metric_normalize (metric);
399 400
	rts[2].mss = mss;
	g_assert_cmpint (routes->len, ==, 3);
401 402
	nmtst_platform_ip6_routes_equal_aptr ((const NMPObject *const*) routes->pdata, rts, routes->len, TRUE);
	g_ptr_array_unref (routes);
Pavel Šimerda's avatar
Pavel Šimerda committed
403 404

	/* Remove route */
405
	g_assert (nmtstp_platform_ip6_route_delete (NM_PLATFORM_GET, ifindex, network, plen, metric));
406
	g_assert (!nmtstp_ip6_route_get (NM_PLATFORM_GET, ifindex, &network, plen, metric, NULL, 0));
Pavel Šimerda's avatar
Pavel Šimerda committed
407 408 409
	accept_signal (route_removed);

	/* Remove route again */
410
	g_assert (nmtstp_platform_ip6_route_delete (NM_PLATFORM_GET, ifindex, network, plen, metric));
Pavel Šimerda's avatar
Pavel Šimerda committed
411

412
	/* Remove default route */
413
	g_assert (nmtstp_platform_ip6_route_delete (NM_PLATFORM_GET, ifindex, in6addr_any, 0, metric));
414 415 416
	accept_signal (route_removed);

	/* Remove route to gateway */
417
	g_assert (nmtstp_platform_ip6_route_delete (NM_PLATFORM_GET, ifindex, gateway, 128, metric));
418 419
	accept_signal (route_removed);

Pavel Šimerda's avatar
Pavel Šimerda committed
420
	free_signal (route_added);
421
	free_signal (route_changed);
Pavel Šimerda's avatar
Pavel Šimerda committed
422 423 424
	free_signal (route_removed);
}

425 426
/*****************************************************************************/

427
static void
428
test_ip4_route_get (void)
429 430 431
{
	int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
	in_addr_t a;
432
	int result;
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	nm_auto_nmpobj NMPObject *route = NULL;
	const NMPlatformIP4Route *r;

	nmtstp_run_command_check ("ip route add 1.2.3.0/24 dev %s", DEVICE_NAME);

	NMTST_WAIT_ASSERT (100, {
		nmtstp_wait_for_signal (NM_PLATFORM_GET, 10);
		if (nmtstp_ip4_route_get (NM_PLATFORM_GET, ifindex, nmtst_inet4_from_string ("1.2.3.0"), 24, 0, 0))
			break;
	});

	a = nmtst_inet4_from_string ("1.2.3.1");
	result = nm_platform_ip_route_get (NM_PLATFORM_GET,
	                                   AF_INET,
	                                   &a,
448
	                                   nmtst_get_rand_int () % 2 ? 0 : ifindex,
449 450
	                                   &route);

451
	g_assert (NMTST_NM_ERR_SUCCESS (result));
452 453 454 455
	g_assert (NMP_OBJECT_GET_TYPE (route) == NMP_OBJECT_TYPE_IP4_ROUTE);
	g_assert (!NMP_OBJECT_IS_STACKINIT (route));
	g_assert (route->parent._ref_count == 1);
	r = NMP_OBJECT_CAST_IP4_ROUTE (route);
456
	g_assert (NM_FLAGS_HAS (r->r_rtm_flags, RTM_F_CLONED));
457
	g_assert (r->ifindex == ifindex);
458 459 460 461 462 463 464 465
	g_assert (r->network == a);
	g_assert (r->plen == 32);

	nmtstp_run_command_check ("ip route flush dev %s", DEVICE_NAME);

	nmtstp_wait_for_signal (NM_PLATFORM_GET, 50);
}

466 467 468 469 470 471 472 473 474
static void
test_ip4_zero_gateway (void)
{
	int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);

	nmtstp_run_command_check ("ip route add 1.2.3.1/32 via 0.0.0.0 dev %s", DEVICE_NAME);
	nmtstp_run_command_check ("ip route add 1.2.3.2/32 dev %s", DEVICE_NAME);

	NMTST_WAIT_ASSERT (100, {
475
		nmtstp_wait_for_signal (NM_PLATFORM_GET, 10);
476 477
		if (   nmtstp_ip4_route_get (NM_PLATFORM_GET, ifindex, nmtst_inet4_from_string ("1.2.3.1"), 32, 0, 0)
		    && nmtstp_ip4_route_get (NM_PLATFORM_GET, ifindex, nmtst_inet4_from_string ("1.2.3.2"), 32, 0, 0))
478 479 480 481 482
			break;
	});

	nmtstp_run_command_check ("ip route flush dev %s", DEVICE_NAME);

483
	nmtstp_wait_for_signal (NM_PLATFORM_GET, 50);
484 485
}

486
static void
487
test_ip4_route_options (gconstpointer test_data)
488
{
489 490 491 492 493 494 495 496 497 498
	const int TEST_IDX = GPOINTER_TO_INT (test_data);
	const int IFINDEX = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
	gs_unref_ptrarray GPtrArray *routes = NULL;
#define RTS_MAX 3
	NMPlatformIP4Route rts_add[RTS_MAX] = { };
	NMPlatformIP4Route rts_cmp[RTS_MAX] = { };
	NMPlatformIP4Address addr[1] = { };
	guint i;
	guint rts_n = 0;
	guint addr_n = 0;
499

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	switch (TEST_IDX) {
	case 1:
		rts_add[rts_n++] = ((NMPlatformIP4Route) {
			.ifindex = IFINDEX,
			.rt_source = NM_IP_CONFIG_SOURCE_USER,
			.network = nmtst_inet4_from_string ("172.16.1.0"),
			.plen = 24,
			.metric = 20,
			.tos = 0x28,
			.window = 10000,
			.cwnd = 16,
			.initcwnd = 30,
			.initrwnd = 50,
			.mtu = 1350,
			.lock_cwnd = TRUE,
		});
		break;
	case 2:
		addr[addr_n++] = ((NMPlatformIP4Address) {
			.ifindex = IFINDEX,
			.address = nmtst_inet4_from_string ("172.16.1.5"),
			.peer_address = nmtst_inet4_from_string ("172.16.1.5"),
			.plen = 24,
			.lifetime = NM_PLATFORM_LIFETIME_PERMANENT,
			.preferred = NM_PLATFORM_LIFETIME_PERMANENT,
			.n_ifa_flags = 0,
		});
		rts_add[rts_n++] = ((NMPlatformIP4Route) {
			.ifindex = IFINDEX,
			.rt_source = NM_IP_CONFIG_SOURCE_USER,
			.network = nmtst_inet4_from_string ("172.17.1.0"),
			.gateway = nmtst_inet4_from_string ("172.16.1.1"),
			.plen = 24,
			.metric = 20,
		});
		rts_add[rts_n++] = ((NMPlatformIP4Route) {
			.ifindex = IFINDEX,
			.rt_source = NM_IP_CONFIG_SOURCE_USER,
			.network = nmtst_inet4_from_string ("172.19.1.0"),
			.gateway = nmtst_inet4_from_string ("172.18.1.1"),
			.r_rtm_flags = RTNH_F_ONLINK,
			.plen = 24,
			.metric = 20,
		});
		break;
	default:
		g_assert_not_reached ();
		break;
	}
	g_assert (rts_n <= G_N_ELEMENTS (rts_add));
	g_assert (addr_n <= G_N_ELEMENTS (addr));
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	for (i = 0; i < addr_n; i++) {
		const NMPlatformIP4Address *a = &addr[i];

		g_assert (a->ifindex == IFINDEX);
		g_assert (nm_platform_ip4_address_add (NM_PLATFORM_GET,
		                                       a->ifindex,
		                                       a->address,
		                                       a->plen,
		                                       a->peer_address,
		                                       a->lifetime,
		                                       a->preferred,
		                                       a->n_ifa_flags,
		                                       a->label));
		if (a->peer_address == a->address)
			_wait_for_ipv4_addr_device_route (NM_PLATFORM_GET, 200, a->ifindex, a->address, a->plen);
	}
568

569
	for (i = 0; i < rts_n; i++)
570
		g_assert (NMTST_NM_ERR_SUCCESS (nm_platform_ip4_route_add (NM_PLATFORM_GET, NMP_NLM_FLAG_REPLACE, &rts_add[i])));
571

572 573 574 575
	for (i = 0; i < rts_n; i++) {
		rts_cmp[i] = rts_add[i];
		nm_platform_ip_route_normalize (AF_INET, NM_PLATFORM_IP_ROUTE_CAST (&rts_cmp[i]));
	}
576

577 578 579
	routes = nmtstp_ip4_route_get_all (NM_PLATFORM_GET, IFINDEX);
	g_assert_cmpint (routes->len, ==, rts_n);
	nmtst_platform_ip4_routes_equal_aptr ((const NMPObject *const*) routes->pdata, rts_cmp, routes->len, TRUE);
580

581 582 583 584 585 586
	for (i = 0; i < rts_n; i++) {
		g_assert (nmtstp_platform_ip4_route_delete (NM_PLATFORM_GET, IFINDEX,
		                                            rts_add[i].network, rts_add[i].plen,
		                                            rts_add[i].metric));
	}
#undef RTS_MAX
587 588
}

589 590 591 592 593
static void
test_ip6_route_get (void)
{
	int ifindex = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
	const struct in6_addr *a;
594
	int result;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	nm_auto_nmpobj NMPObject *route = NULL;
	const NMPlatformIP6Route *r;

	nmtstp_run_command_check ("ip -6 route add fd01:abcd::/64 via fe80::99 dev %s", DEVICE_NAME);

	NMTST_WAIT_ASSERT (100, {
		nmtstp_wait_for_signal (NM_PLATFORM_GET, 10);
		if (nmtstp_ip6_route_get (NM_PLATFORM_GET, ifindex, nmtst_inet6_from_string ("fd01:abcd::"), 64, 0, NULL, 0))
			break;
	});

	a = nmtst_inet6_from_string ("fd01:abcd::42");
	result = nm_platform_ip_route_get (NM_PLATFORM_GET,
	                                   AF_INET6,
	                                   a,
	                                   nmtst_get_rand_int () % 2 ? 0 : ifindex,
	                                   &route);

613
	g_assert (NMTST_NM_ERR_SUCCESS (result));
614 615 616 617 618 619 620 621 622 623 624 625 626 627
	g_assert (NMP_OBJECT_GET_TYPE (route) == NMP_OBJECT_TYPE_IP6_ROUTE);
	g_assert (!NMP_OBJECT_IS_STACKINIT (route));
	g_assert (route->parent._ref_count == 1);
	r = NMP_OBJECT_CAST_IP6_ROUTE (route);
	g_assert (r->ifindex == ifindex);
	nmtst_assert_ip6_address (&r->network, "fd01:abcd::42");
	g_assert_cmpint (r->plen, ==, 128);
	nmtst_assert_ip6_address (&r->gateway, "fe80::99");

	nmtstp_run_command_check ("ip -6 route flush dev %s", DEVICE_NAME);

	nmtstp_wait_for_signal (NM_PLATFORM_GET, 50);
}

628
static void
629
test_ip6_route_options (gconstpointer test_data)
630
{
631 632
	const int TEST_IDX = GPOINTER_TO_INT (test_data);
	const int IFINDEX = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
633
	gs_unref_ptrarray GPtrArray *routes = NULL;
634
#define RTS_MAX 3
635 636 637
	NMPlatformIP6Route rts_add[RTS_MAX] = { };
	NMPlatformIP6Route rts_cmp[RTS_MAX] = { };
	NMPlatformIP6Address addr[1] = { };
638
	struct in6_addr addr_in6[G_N_ELEMENTS (addr)] = { };
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	guint rts_n = 0;
	guint addr_n = 0;
	guint i;

	switch (TEST_IDX) {
	case 1:
		rts_add[rts_n++] = ((NMPlatformIP6Route) {
			.ifindex = IFINDEX,
			.rt_source = NM_IP_CONFIG_SOURCE_USER,
			.network = *nmtst_inet6_from_string ("2001:db8:a:b:0:0:0:0"),
			.plen = 64,
			.gateway = in6addr_any,
			.metric = 1024,
			.window = 20000,
			.cwnd = 8,
			.initcwnd = 22,
			.initrwnd = 33,
			.mtu = 1300,
			.lock_mtu = TRUE,
		});
		break;
	case 2:
		addr[addr_n++] = ((NMPlatformIP6Address) {
			.ifindex = IFINDEX,
			.address = *nmtst_inet6_from_string ("2000::2"),
			.plen = 128,
			.peer_address = in6addr_any,
			.lifetime = NM_PLATFORM_LIFETIME_PERMANENT,
			.preferred = NM_PLATFORM_LIFETIME_PERMANENT,
			.n_ifa_flags = 0,
		});
		rts_add[rts_n++] = ((NMPlatformIP6Route) {
			.ifindex = IFINDEX,
			.rt_source = NM_IP_CONFIG_SOURCE_USER,
			.network = *nmtst_inet6_from_string ("1010::1"),
			.plen = 128,
			.gateway = in6addr_any,
			.metric = 256,
			.pref_src = *nmtst_inet6_from_string ("2000::2"),
		});
		break;
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	case 3:
		addr[addr_n++] = ((NMPlatformIP6Address) {
			.ifindex = IFINDEX,
			.address = *nmtst_inet6_from_string ("2001:db8:8086::5"),
			.plen = 128,
			.peer_address = in6addr_any,
			.lifetime = NM_PLATFORM_LIFETIME_PERMANENT,
			.preferred = NM_PLATFORM_LIFETIME_PERMANENT,
			.n_ifa_flags = 0,
		});
		rts_add[rts_n++] = ((NMPlatformIP6Route) {
			.ifindex = IFINDEX,
			.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER),
			.network = *nmtst_inet6_from_string ("2001:db8:8086::"),
			.plen = 110,
			.metric = 10021,
			.mss = 0,
		});
		rts_add[rts_n++] = ((NMPlatformIP6Route) {
			.ifindex = IFINDEX,
			.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER),
			.network = *nmtst_inet6_from_string ("2001:db8:abad:c0de::"),
			.plen = 64,
			.gateway = *nmtst_inet6_from_string ("2001:db8:8086::1"),
			.metric = 21,
			.mss = 0,
		});
		break;
708 709 710
	default:
		g_assert_not_reached ();
	}
711 712
	g_assert (rts_n <= G_N_ELEMENTS (rts_add));
	g_assert (addr_n <= G_N_ELEMENTS (addr));
713 714

	for (i = 0; i < addr_n; i++) {
715 716 717 718
		g_assert (addr[i].ifindex == IFINDEX);
		addr_in6[i] = addr[i].address;
		g_assert (nm_platform_ip6_address_add (NM_PLATFORM_GET,
		                                       IFINDEX,
719 720 721 722 723 724 725
		                                       addr[i].address,
		                                       addr[i].plen,
		                                       addr[i].peer_address,
		                                       addr[i].lifetime,
		                                       addr[i].preferred,
		                                       addr[i].n_ifa_flags));
	}
726
	_wait_for_ipv6_addr_non_tentative (NM_PLATFORM_GET, 400, IFINDEX, addr_n, addr_in6);
727

728
	for (i = 0; i < rts_n; i++)
729
		g_assert (NMTST_NM_ERR_SUCCESS (nm_platform_ip6_route_add (NM_PLATFORM_GET, NMP_NLM_FLAG_REPLACE, &rts_add[i])));
730

731 732 733
	for (i = 0; i < rts_n; i++) {
		rts_cmp[i] = rts_add[i];
		nm_platform_ip_route_normalize (AF_INET6, NM_PLATFORM_IP_ROUTE_CAST (&rts_cmp[i]));
734
	}
735 736

	routes = nmtstp_ip6_route_get_all (NM_PLATFORM_GET, IFINDEX);
737
	g_assert_cmpint (routes->len, ==, rts_n);
738
	nmtst_platform_ip6_routes_equal_aptr ((const NMPObject *const*) routes->pdata, rts_cmp, routes->len, TRUE);
739 740

	for (i = 0; i < rts_n; i++) {
741 742 743
		g_assert (nmtstp_platform_ip6_route_delete (NM_PLATFORM_GET, IFINDEX,
		                                            rts_add[i].network, rts_add[i].plen,
		                                            rts_add[i].metric));
744 745 746 747 748 749 750 751 752
	}

	for (i = 0; i < addr_n; i++) {
		nmtstp_ip6_address_del (NM_PLATFORM_GET,
		                        EX,
		                        IFINDEX,
		                        rts_add[i].network,
		                        rts_add[i].plen);
	}
753
#undef RTS_MAX
754 755
}

756 757
/*****************************************************************************/

758 759 760 761 762 763 764 765 766 767 768 769 770 771
static void
test_ip (gconstpointer test_data)
{
	const int TEST_IDX = GPOINTER_TO_INT (test_data);
	const int IFINDEX = nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME);
	guint i, j, k;
	const NMPlatformLink *l;
	char ifname[IFNAMSIZ];
	char ifname2[IFNAMSIZ];
	char s1[NM_UTILS_INET_ADDRSTRLEN];
	NMPlatform *platform = NM_PLATFORM_GET;
	const int EX_ = -1;
	struct {
		int ifindex;
772
	} iface_data[10] = { { 0 }, };
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
	int order_idx[G_N_ELEMENTS (iface_data)] = { 0 };
	guint order_len;
	guint try;

	for (i = 0; i < G_N_ELEMENTS (iface_data); i++) {
		nm_sprintf_buf (ifname, "v%02u", i);
		nm_sprintf_buf (ifname2, "w%02u", i);

		g_assert (!nm_platform_link_get_by_ifname (platform, ifname));
		g_assert (!nm_platform_link_get_by_ifname (platform, ifname2));
		l = nmtstp_link_veth_add (platform, EX_, ifname, ifname2);
		iface_data[i].ifindex = l->ifindex;

		nmtstp_link_set_updown (platform, EX_, iface_data[i].ifindex, TRUE);
		nmtstp_link_set_updown (platform, EX_, nmtstp_link_get (platform, -1, ifname2)->ifindex, TRUE);

		nm_sprintf_buf (s1, "192.168.7.%d", 100 + i);
		nmtstp_ip4_address_add (platform,
		                        EX_,
		                        iface_data[i].ifindex,
		                        nmtst_inet4_from_string (s1),
		                        24,
		                        nmtst_inet4_from_string (s1),
		                        3600,
		                        3600,
		                        0,
		                        NULL);
	}

	order_len = 0;
	for (try = 0; try < 5 * G_N_ELEMENTS (order_idx); try++) {
		NMPObject o;
		NMPlatformIP4Route *r;
		guint idx;
		const NMDedupMultiHeadEntry *head_entry;
		NMPLookup lookup;

		nmp_object_stackinit (&o, NMP_OBJECT_TYPE_IP4_ROUTE, NULL);
		r = NMP_OBJECT_CAST_IP4_ROUTE (&o);
		r->network = nmtst_inet4_from_string ("192.168.9.0");
		r->plen = 24;
		r->metric = 109;

		if (   order_len == 0
		    || (   order_len < G_N_ELEMENTS (order_idx)
		        && nmtst_get_rand_int () % 2)) {
again_find_idx:
			idx = nmtst_get_rand_int () % G_N_ELEMENTS (iface_data);
			for (i = 0; i < order_len; i++) {
				if (order_idx[i] == idx)
					goto again_find_idx;
			}
			order_idx[order_len++] = idx;

			r->ifindex = iface_data[idx].ifindex;
828
			g_assert (NMTST_NM_ERR_SUCCESS (nm_platform_ip4_route_add (platform, NMP_NLM_FLAG_APPEND, r)));
829 830 831 832 833 834 835 836
		} else {
			i = nmtst_get_rand_int () % order_len;
			idx = order_idx[i];
			for (i++; i < order_len; i++)
				order_idx[i - 1] = order_idx[i];
			order_len--;

			r->ifindex = iface_data[idx].ifindex;
837
			g_assert (nm_platform_object_delete (platform, &o));
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
		}

		head_entry = nm_platform_lookup (platform,
		                                 nmp_lookup_init_obj_type (&lookup, NMP_OBJECT_TYPE_IP4_ROUTE));
		for (j = 0; j < G_N_ELEMENTS (iface_data); j++) {
			gboolean has;
			NMDedupMultiIter iter;
			const NMPObject *o_cached;

			has = FALSE;
			for (k = 0; k < order_len; k++) {
				if (order_idx[k] == j) {
					g_assert (!has);
					has = TRUE;
				}
			}

			nmp_cache_iter_for_each (&iter, head_entry, &o_cached) {
				const NMPlatformIP4Route *r_cached = NMP_OBJECT_CAST_IP4_ROUTE (o_cached);

				if (   r_cached->ifindex != iface_data[j].ifindex
				    || r_cached->metric != 109)
					continue;

				g_assert (has);
				has = FALSE;
			}
			g_assert (!has);
		}
	}

	for (i = 0; i < G_N_ELEMENTS (iface_data); i++)
		g_assert (nm_platform_link_delete (platform, iface_data[i].ifindex));

	(void) TEST_IDX;
	(void) IFINDEX;
}

/*****************************************************************************/

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
#define FRA_SUPPRESS_IFGROUP         13
#define FRA_SUPPRESS_PREFIXLEN       14
#define FRA_L3MDEV                   19
#define FRA_UID_RANGE                20
#define FRA_PROTOCOL                 21
#define FRA_IP_PROTO                 22
#define FRA_SPORT_RANGE              23
#define FRA_DPORT_RANGE              24

static const NMPObject *
_rule_find_by_priority (NMPlatform *platform,
                        guint32 priority)
{
	const NMDedupMultiHeadEntry *head_entry;
	NMDedupMultiIter iter;
	const NMPObject *o;
	const NMPObject *obj = NULL;
	NMPLookup lookup;

	nmp_lookup_init_obj_type (&lookup, NMP_OBJECT_TYPE_ROUTING_RULE);
	head_entry = nm_platform_lookup (platform, &lookup);
	nmp_cache_iter_for_each (&iter, head_entry, &o) {
		if (NMP_OBJECT_CAST_ROUTING_RULE (o)->priority != priority)
			continue;
		g_assert (!obj);
		obj = o;
	}
	return obj;
}

static const NMPObject *
_rule_check_kernel_support_one (NMPlatform *platform,
                                const NMPlatformRoutingRule *rr)
{
	nm_auto_nmpobj const NMPObject *obj = NULL;
	int r;

	g_assert (!_rule_find_by_priority (platform, rr->priority));

	r = nm_platform_routing_rule_add (platform, NMP_NLM_FLAG_ADD, rr);
	g_assert_cmpint (r, ==, 0);

	obj = nmp_object_ref (_rule_find_by_priority (platform, rr->priority));
	g_assert (obj);

	r = nm_platform_object_delete (platform, obj);
	g_assert_cmpint (r, ==, TRUE);

	g_assert (!_rule_find_by_priority (platform, rr->priority));

	return g_steal_pointer (&obj);
}

static gboolean
_rule_check_kernel_support (NMPlatform *platform,
                            int attribute)
{
	static int support[] = {
		[FRA_SUPPRESS_IFGROUP]   = 0,
		[FRA_SUPPRESS_PREFIXLEN] = 0,
		[FRA_L3MDEV]             = 0,
		[FRA_UID_RANGE]          = 0,
		[FRA_PROTOCOL]           = 0,
		[FRA_IP_PROTO]           = 0,
		[FRA_SPORT_RANGE]        = 0,
		[FRA_DPORT_RANGE]        = 0,
	};
	const guint32 PROBE_PRORITY = 12033;
	gboolean sup;

	g_assert (NM_IS_PLATFORM (platform));
	g_assert (attribute >= 0 && attribute < G_N_ELEMENTS (support));

	if (support[attribute] != 0)
		return support[attribute] >= 0;

	switch (attribute) {
	case FRA_SUPPRESS_IFGROUP: {
		nm_auto_nmpobj const NMPObject *obj = NULL;
		const NMPlatformRoutingRule rr = {
			.addr_family              = AF_INET,
			.priority                 = PROBE_PRORITY,
			.suppress_ifgroup_inverse = ~((guint32) 1245),
		};

		obj = _rule_check_kernel_support_one (platform, &rr);

		sup = NMP_OBJECT_CAST_ROUTING_RULE (obj)->suppress_prefixlen_inverse == rr.suppress_ifgroup_inverse;
		break;
	}
	case FRA_SUPPRESS_PREFIXLEN: {
		nm_auto_nmpobj const NMPObject *obj = NULL;
		const NMPlatformRoutingRule rr = {
			.addr_family                = AF_INET,
			.priority                   = PROBE_PRORITY,
			.suppress_prefixlen_inverse = ~((guint32) 1245),
		};

		obj = _rule_check_kernel_support_one (platform, &rr);

		sup = NMP_OBJECT_CAST_ROUTING_RULE (obj)->suppress_prefixlen_inverse == rr.suppress_prefixlen_inverse;
		break;
	}
	case FRA_L3MDEV: {
		nm_auto_nmpobj const NMPObject *obj = NULL;
		const NMPlatformRoutingRule rr = {
			.addr_family = AF_INET,
			.priority    = PROBE_PRORITY,
			.l3mdev      = TRUE,
		};

		obj = _rule_check_kernel_support_one (platform, &rr);

		sup = NMP_OBJECT_CAST_ROUTING_RULE (obj)->l3mdev != 0;
		break;
	}
	case FRA_UID_RANGE: {
		nm_auto_nmpobj const NMPObject *obj = NULL;
		const NMPlatformRoutingRule rr = {
			.addr_family  = AF_INET,
			.priority     = PROBE_PRORITY,
			.uid_range    = {
				.start = 0,
				.end   = 0,
			},
			.uid_range_has = TRUE,
		};

		obj = _rule_check_kernel_support_one (platform, &rr);

		sup = NMP_OBJECT_CAST_ROUTING_RULE (obj)->uid_range_has;
		break;
	}
	case FRA_PROTOCOL: {
		nm_auto_nmpobj const NMPObject *obj = NULL;
		const NMPlatformRoutingRule rr = {
			.addr_family = AF_INET,
			.priority    = PROBE_PRORITY,
			.protocol    = 30,
		};

		obj = _rule_check_kernel_support_one (platform, &rr);

		sup = NMP_OBJECT_CAST_ROUTING_RULE (obj)->protocol == 30;
		break;
	}
	case FRA_IP_PROTO: {
		nm_auto_nmpobj const NMPObject *obj = NULL;
		const NMPlatformRoutingRule rr = {
			.addr_family = AF_INET,
			.priority    = PROBE_PRORITY,
			.ip_proto    = 30,
		};

		obj = _rule_check_kernel_support_one (platform, &rr);

		sup = NMP_OBJECT_CAST_ROUTING_RULE (obj)->ip_proto == 30;
		break;
	}
	case FRA_SPORT_RANGE:
	case FRA_DPORT_RANGE:
		/* these were added at the same time as FRA_IP_PROTO. */
		sup = _rule_check_kernel_support (platform, FRA_IP_PROTO);
		break;
	default:
		g_assert_not_reached ();
		return FALSE;
	}

	support[attribute] = sup ? 1 : -1;

	_LOGD ("kernel support for routing rule attribute #%d %s", attribute, sup ? "detected" : "not detected");
	return sup;
}

static const NMPObject *
_platform_has_routing_rule (NMPlatform *platform,
                            const NMPObject *obj)
{
	const NMPObject *o;

	g_assert (NM_IS_PLATFORM (platform));
	g_assert (NMP_OBJECT_IS_VALID (obj));
	g_assert (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_ROUTING_RULE);

	o = nm_platform_lookup_obj (platform, NMP_CACHE_ID_TYPE_OBJECT_TYPE, obj);
	if (o)
		g_assert (nm_platform_routing_rule_cmp (NMP_OBJECT_CAST_ROUTING_RULE (obj), NMP_OBJECT_CAST_ROUTING_RULE (o), NM_PLATFORM_ROUTING_RULE_CMP_TYPE_ID) == 0);

	return o;
}

static const NMPObject *
_rule_create_random (NMPlatform *platform)
{
	NMPObject *obj;
	NMPlatformRoutingRule *rr;
	guint32 p;
	int addr_size;
	guint i;
	char saddr[NM_UTILS_INET_ADDRSTRLEN];
	static struct {
		guint32 uid;
		guint32 euid;
		bool initialized;
	} uids;

	if (G_UNLIKELY (!uids.initialized)) {
		uids.uid = getuid ();
		uids.euid = geteuid ();
		uids.initialized = TRUE;
	}

	obj = nmp_object_new (NMP_OBJECT_TYPE_ROUTING_RULE, NULL);
	rr = NMP_OBJECT_CAST_ROUTING_RULE (obj);

	rr->addr_family = nmtst_rand_select (AF_INET, AF_INET6);

	addr_size = nm_utils_addr_family_to_size (rr->addr_family);

	p = nmtst_get_rand_int () % 1000u;
	if (p < 250)
		rr->priority = 10000 + (p / 50);

	p = nmtst_get_rand_int () % 1000u;
	if (p < 60)
		nm_sprintf_buf (rr->iifname, "t-iif-%u", p / 20);
	else if (p < 120)
		nm_sprintf_buf (rr->iifname, "%s", DEVICE_NAME);

	p = nmtst_get_rand_int () % 1000u;
	if (p < 60)
		nm_sprintf_buf (rr->oifname, "t-oif-%d", p / 20);
	else if (p < 120)
		nm_sprintf_buf (rr->oifname, "%s", DEVICE_NAME);

	p = nmtst_get_rand_int () % 1000u;
	if (p < 60)
		nm_sprintf_buf (rr->iifname, "t-iif-%d", p / 20);
	else if (p < 120)
		nm_sprintf_buf (rr->iifname, "%s", DEVICE_NAME);

	for (i = 0; i < 2; i++) {
		NMIPAddr *p_addr = i ? &rr->src     : &rr->dst;
		guint8 *p_len    = i ? &rr->src_len : &rr->dst_len;

		p = nmtst_get_rand_int () % 1000u;
		if (p < 300) {
			/* if we set src_len/dst_len to zero, the src/dst is actually ignored.
			 *
			 * For fuzzying, still set the address. It shall have no further effect.
			 * */
			*p_len = p % (addr_size * 8 + 1);
			p = nmtst_get_rand_int () % 750;
			if (p <= 255) {
				if (rr->addr_family == AF_INET)
					p_addr->addr4 = nmtst_inet4_from_string (nm_sprintf_buf (saddr, "192.192.5.%u", p));
				else
					p_addr->addr6 = *nmtst_inet6_from_string (nm_sprintf_buf (saddr, "1:2:3:4::f:%02x", p));
			} else if (p <= 512)
				nmtst_rand_buf (NULL, p_addr, addr_size);
		}
	}

	p = nmtst_get_rand_int () % 1000u;
	if (p < 50)
		rr->tun_id = 10000 + p;

again_action:
	p = nmtst_get_rand_int () % 1000u;
	if (p < 300)
		rr->action = FR_ACT_UNSPEC;
	else if (p < 700)
		rr->action = p % 12;
	else
		rr->action = p % 0xFF;

	rr->priority = nmtst_rand_select (0,
	                                  nmtst_get_rand_int () % 100,
	                                  nmtst_get_rand_int ());
	if (   rr->action == FR_ACT_GOTO
	    && rr->priority == G_MAXINT32)
		goto again_action;

again_goto_target:
	rr->goto_target = nmtst_rand_select (0,
	                                     nmtst_get_rand_int () % 100,
	                                     nmtst_get_rand_int (),
	                                     rr->priority + 1);
	if (   rr->action == FR_ACT_GOTO
	    && rr->goto_target <= rr->priority)
		goto again_goto_target;

	p = nmtst_get_rand_int () % 1000u;
	if (p < 50) {
		if (_rule_check_kernel_support (platform, FRA_L3MDEV)) {
			rr->l3mdev = TRUE;
			rr->table = RT_TABLE_UNSPEC;
		}
	}

again_table:
	if (!rr->l3mdev) {
		rr->table = nmtst_rand_select (RT_TABLE_UNSPEC,
		                               RT_TABLE_MAIN,
		                               10000 + nmtst_get_rand_int () % 10);
		if (   rr->action == FR_ACT_TO_TBL
		    && rr->table == RT_TABLE_UNSPEC)
			goto again_table;
	}

	rr->fwmark = nmtst_rand_select (0,
	                                nmtst_get_rand_int () % 100,
	                                nmtst_get_rand_int ());
	rr->fwmask = nmtst_rand_select (0u,
	                                0xFFFFFFFFu,
	                                nmtst_get_rand_int () % 100,
	                                nmtst_get_rand_int ());

	rr->flow = nmtst_rand_select (0u,
	                              0xFFFFFFFFu,
	                              nmtst_get_rand_int () % 100,
	                              nmtst_get_rand_int ());

	if (_rule_check_kernel_support (platform, FRA_PROTOCOL)) {
		rr->protocol = nmtst_rand_select (0u,
		                                  255u,
		                                  nmtst_get_rand_int () % 256);
	}

#define IPTOS_TOS_MASK 0x1E

again_tos:
	rr->tos = nmtst_rand_select (0u,
	                             255u,
	                             nmtst_get_rand_int () % 256);
	if (   rr->addr_family == AF_INET
	    && rr->tos & ~IPTOS_TOS_MASK)
		goto again_tos;

	if (_rule_check_kernel_support (platform, FRA_IP_PROTO)) {
		rr->ip_proto = nmtst_rand_select (0u,
		                                  255u,
		                                  nmtst_get_rand_int () % 256);
	}

	if (_rule_check_kernel_support (platform, FRA_SUPPRESS_PREFIXLEN)) {
		rr->suppress_prefixlen_inverse = ~((guint32) nmtst_rand_select (0u,
		                                                                0xFFFFFFFFu,
		                                                                nmtst_get_rand_int () % 100,
		                                                                nmtst_get_rand_int ()));
	}

	if (_rule_check_kernel_support (platform, FRA_SUPPRESS_IFGROUP)) {
		rr->suppress_ifgroup_inverse = ~((guint32) nmtst_rand_select (0u,
		                                                              0xFFFFFFFFu,
		                                                              nmtst_get_rand_int () % 100,
		                                                              nmtst_get_rand_int ()));
	}

again_uid_range:
	p = nmtst_get_rand_int () % 1000u;
	if (p < 100) {
		if (_rule_check_kernel_support (platform, FRA_UID_RANGE))
			rr->uid_range_has = TRUE;
	}

	rr->uid_range.start = nmtst_rand_select (0u, uids.uid, uids.euid);
	rr->uid_range.end   = nmtst_rand_select (0u, uids.uid, uids.euid);
	if (rr->uid_range.end < rr->uid_range.start)
		NMTST_SWAP (rr->uid_range.start, rr->uid_range.end);

	if (   rr->uid_range.start == ((guint32) -1)
	    || rr->uid_range.end   == ((guint32) -1))
		goto again_uid_range;

	for (i = 0; i < 2; i++) {
		NMFibRulePortRange *range = i ? &rr->sport_range : &rr->dport_range;
		int attribute             = i ? FRA_SPORT_RANGE  : FRA_DPORT_RANGE;

		if (!_rule_check_kernel_support (platform, attribute))
			continue;

		p = nmtst_get_rand_int ();
		if ((p % 1000u) < 100) {
			while (range->start == 0) {
				p = p ^ nmtst_get_rand_int ();
				range->start = nmtst_rand_select (1u, 0xFFFEu, ((p      ) % 0xFFFEu) + 1);
				range->end   = nmtst_rand_select (1u, 0xFFFEu, ((p >> 16) % 0xFFFEu) + 1, range->start);
				if (range->end < range->start)
					NMTST_SWAP (range->start, range->end);
			}
		}
	}

	p = nmtst_get_rand_int () % 1000u;
	if (p < 100)
		rr->flags |= FIB_RULE_INVERT;

	return obj;
}

static gboolean
_rule_fuzzy_equal (const NMPObject *obj,
                   const NMPObject *obj_comp,
                   int op_type)
{
	const NMPlatformRoutingRule *rr = NMP_OBJECT_CAST_ROUTING_RULE (obj);
	NMPlatformRoutingRule rr_co = *NMP_OBJECT_CAST_ROUTING_RULE (obj_comp);

	switch (op_type) {
	case RTM_NEWRULE:
		/* when adding rules with RTM_NEWRULE, kernel checks whether an existing
		 * rule already exists and may fail with EEXIST. This check has issues
		 * and reject legitimate rules (rh#1686075).
		 *
		 * Work around that. */
		if (rr->src_len == 0)
			rr_co.src_len = 0;
		if (rr->dst_len == 0)
			rr_co.dst_len = 0;
		if (rr->flow == 0)
			rr_co.flow = 0;
		if (rr->tos == 0)
			rr_co.tos = 0;
		break;
	case RTM_DELRULE:
		/* when deleting a rule with RTM_DELRULE, kernel tries to find the
		 * cadidate to delete. It might delete the wrong rule (rh#1685816). */
		if (rr->action == FR_ACT_UNSPEC)
			rr_co.action = FR_ACT_UNSPEC;
		if (rr->iifname[0] == '\0')
			rr_co.iifname[0] = '\0';
		if (rr->oifname[0] == '\0')
			rr_co.oifname[0] = '\0';
		if (rr->src_len == 0)
			rr_co.src_len = 0;
		if (rr->dst_len == 0)
			rr_co.dst_len = 0;
		if (rr->tun_id == 0)
			rr_co.tun_id = 0;
		if (rr->fwmark == 0)
			rr_co.fwmark = 0;
		if (rr->fwmask == 0)
			rr_co.fwmask = 0;
		if (rr->flow == 0)
			rr_co.flow = 0;
		if (rr->protocol == 0)
			rr_co.protocol = 0;
		if (rr->table == RT_TABLE_UNSPEC)
			rr_co.table = RT_TABLE_UNSPEC;
		if (rr->l3mdev == 0)
			rr_co.l3mdev = 0;
		if (rr->tos == 0)
			rr_co.tos = 0;
		if (rr->ip_proto == 0)
			rr_co.ip_proto = 0;
		if (rr->suppress_prefixlen_inverse == 0)
			rr_co.suppress_prefixlen_inverse = 0;
		if (rr->suppress_ifgroup_inverse == 0)
			rr_co.suppress_ifgroup_inverse = 0;
		if (!rr->uid_range_has)
			rr_co.uid_range_has = FALSE;
		if (rr->sport_range.start == 0 && rr->sport_range.end == 0) {
			rr_co.sport_range.start = 0;
			rr_co.sport_range.end   = 0;
		}
		if (rr->dport_range.start == 0 && rr->dport_range.end == 0) {
			rr_co.dport_range.start = 0;
			rr_co.dport_range.end   = 0;
		}
		if (!NM_FLAGS_HAS (rr->flags, FIB_RULE_INVERT))
			rr_co.flags &= ~((guint32) FIB_RULE_INVERT);
		else
			rr_co.flags |= ((guint32) FIB_RULE_INVERT);
		break;
	default:
		g_assert_not_reached ();
		break;
	}

	return nm_platform_routing_rule_cmp (rr, &rr_co, NM_PLATFORM_ROUTING_RULE_CMP_TYPE_ID) == 0;
}

static void
test_rule (gconstpointer test_data)
{
	const int TEST_IDX = GPOINTER_TO_INT (test_data);
1366
	const gboolean TEST_SYNC = (TEST_IDX == 4);
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	gs_unref_ptrarray GPtrArray *objs = NULL;
	gs_unref_ptrarray GPtrArray *objs_initial = NULL;
	NMPlatform *platform = NM_PLATFORM_GET;
	guint i, j, n;
	int r;

	nm_platform_process_events (platform);

	objs_initial = nmtstp_platform_routing_rules_get_all (platform, AF_UNSPEC);
	g_assert (objs_initial);
	g_assert_cmpint (objs_initial->len, ==, 5);

	nmtstp_run_command_check ("ip rule add table 766");
	nm_platform_process_events (platform);

	for (i = 6; i > 0; i--) {
		gs_unref_ptrarray GPtrArray *objs_extern = NULL;
		const NMPObject *obj;

		objs_extern = nmtstp_platform_routing_rules_get_all (platform, AF_UNSPEC);

		g_assert (objs_extern);
		g_assert_cmpint (objs_extern->len, ==, i);

		if (TEST_IDX != 1)
			nmtst_rand_perm (NULL, objs_extern->pdata, NULL, sizeof (gpointer), objs_extern->len);

		obj = objs_extern->pdata[0];

		r = nm_platform_object_delete (platform, obj);
		g_assert_cmpint (r, ==, TRUE);

		g_assert (!_platform_has_routing_rule (platform, obj));
	}

	g_assert_cmpint (nmtstp_platform_routing_rules_get_count (platform, AF_UNSPEC), ==, 0);

#define RR(...) \
	nmp_object_new (NMP_OBJECT_TYPE_ROUTING_RULE, \
	                (const NMPlatformObject *) &((NMPlatformRoutingRule) { \
	                  __VA_ARGS__ \
	                }))

	objs = g_ptr_array_new_with_free_func ((GDestroyNotify) nmp_object_unref);

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET,
		.priority    = 10,
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET,
		.priority    = 400,
		.action      = FR_ACT_GOTO,
		.goto_target = 10000,
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET6,
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET6,
		.action      = FR_ACT_TO_TBL,
		.table       = RT_TABLE_MAIN,
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET6,
		.priority    = 30,
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET6,
		.priority    = 50,
		.iifname     = "t-iif-1",
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET6,
		.priority    = 50,
		.iifname     = "t-oif-1",
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET,
		.priority    = 50,
		.iifname     = "t-oif-2",
	));

	g_ptr_array_add (objs, RR (
		.addr_family = AF_INET,
		.priority    = 51,
		.iifname     = DEVICE_NAME,
	));

	if (TEST_IDX == 1) {
		g_ptr_array_add (objs, RR (
			.addr_family = AF_INET,
			.table       = 10000,
		));
	}

	if (TEST_IDX != 1) {
		nmtst_rand_perm (NULL, objs->pdata, NULL, sizeof (gpointer), objs->len);
		g_ptr_array_set_size (objs, nmtst_get_rand_int () % (objs->len + 1));
	}

	n = (TEST_IDX != 1) ? nmtst_get_rand_int () % 50u : 0u;
	for (i = 0; i < n; i++) {
		nm_auto_nmpobj const NMPObject *o = NULL;
		guint try = 0;

again:
		o = _rule_create_random (platform);
		for (j = 0; j < objs->len; j++) {
			if (nm_platform_routing_rule_cmp (NMP_OBJECT_CAST_ROUTING_RULE (o),
			                                  NMP_OBJECT_CAST_ROUTING_RULE (objs->pdata[j]),
			                                  NM_PLATFORM_ROUTING_RULE_CMP_TYPE_ID) == 0)  {
				try++;
				g_assert (try < 200);
				nm_clear_pointer (&o, nmp_object_unref);
				goto again;
			}
		}
		g_ptr_array_add (objs, (gpointer) g_steal_pointer (&o));
	}

	if (TEST_IDX != 1)
		nmtst_rand_perm (NULL, objs->pdata, NULL, sizeof (gpointer), objs->len);

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
	if (TEST_SYNC) {
		gs_unref_hashtable GHashTable *unique_priorities = g_hash_table_new (NULL, NULL);
		nm_auto_unref_rules_manager NMPRulesManager *rules_manager = nmp_rules_manager_new (platform, FALSE);
		gs_unref_ptrarray GPtrArray *objs_sync = NULL;
		gconstpointer USER_TAG_1 = &platform;
		gconstpointer USER_TAG_2 = &unique_priorities;

		objs_sync = g_ptr_array_new_with_free_func ((GDestroyNotify) nmp_object_unref);

		/* ensure that priorities are unique. Otherwise it confuses the test, because
		 * kernel may wrongly be unable to add/delete routes based on a wrong match
		 * (rh#1685816, rh#1685816). */
		for (i = 0; i < objs->len; i++) {
			const NMPObject *obj = objs->pdata[i];
			guint32 prio = NMP_OBJECT_CAST_ROUTING_RULE (obj)->priority;

			if (   !NM_IN_SET (prio, 0, 32766, 32767)
			    && !g_hash_table_contains (unique_priorities, GUINT_TO_POINTER (prio))) {
				g_hash_table_add (unique_priorities, GUINT_TO_POINTER (prio));
				g_ptr_array_add (objs_sync, (gpointer) nmp_object_ref (obj));
			}
		}
1520

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
		for (i = 0; i < objs_sync->len; i++) {
			nmp_rules_manager_track (rules_manager,
			                         NMP_OBJECT_CAST_ROUTING_RULE (objs_sync->pdata[i]),
			                         1,
			                         USER_TAG_1);
			if (nmtst_get_rand_bool ()) {
				/* this has no effect, because a negative priority (of same absolute value)
				 * has lower priority than the positive priority above. */
				nmp_rules_manager_track (rules_manager,
				                         NMP_OBJECT_CAST_ROUTING_RULE (objs_sync->pdata[i]),
				                         -1,
				                         USER_TAG_2);
			}
			if (nmtst_get_rand_int () % objs_sync->len == 0) {
				nmp_rules_manager_sync (rules_manager);
				g_assert_cmpint (nmtstp_platform_routing_rules_get_count (platform, AF_UNSPEC), ==, i + 1);
			}
		}
1539

1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
		nmp_rules_manager_sync (rules_manager);
		g_assert_cmpint (nmtstp_platform_routing_rules_get_count (platform, AF_UNSPEC), ==, objs_sync->len);

		for (i = 0; i < objs_sync->len; i++) {
			switch (nmtst_get_rand_int () % 3) {
			case 0:
				nmp_rules_manager_untrack (rules_manager,
				                           NMP_OBJECT_CAST_ROUTING_RULE (objs_sync->pdata[i]),
				                           USER_TAG_1);
				nmp_rules_manager_untrack (rules_manager,
				                           NMP_OBJECT_CAST_ROUTING_RULE (objs_sync->pdata[i]),
				                           USER_TAG_1);
				break;
			case 1:
				nmp_rules_manager_track (rules_manager,
				                         NMP_OBJECT_CAST_ROUTING_RULE (objs_sync->pdata[i]),
				                         -1,
				                         USER_TAG_1);
				break;
			case 2:
				nmp_rules_manager_track (rules_manager,
				                         NMP_OBJECT_CAST_ROUTING_RULE (objs_sync->pdata[i]),
				                         -2,
				                         USER_TAG_2);
				break;
			}
			if (nmtst_get_rand_int () % objs_sync->len == 0) {
				nmp_rules_manager_sync (rules_manager);
				g_assert_cmpint (nmtstp_platform_routing_rules_get_count (platform, AF_UNSPEC), ==, objs_sync->len - i - 1);
			}
		}

		nmp_rules_manager_sync (rules_manager);

	} else {
		for (i = 0; i < objs->len;) {
			const NMPObject *obj = objs->pdata[i];

			for (j = 0; j < objs->len; j++)
				g_assert ((j < i) == (!!_platform_has_routing_rule (platform, objs->pdata[j])));

			r = nm_platform_routing_rule_add (platform, NMP_NLM_FLAG_ADD, NMP_OBJECT_CAST_ROUTING_RULE (obj));