summaryrefslogtreecommitdiff
path: root/src/devices/nm-acd-manager.c
blob: 9d1342e2be0093306f41893b2369892738db9b8f (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// SPDX-License-Identifier: GPL-2.0+
/* NetworkManager -- Network link manager
 *
 * Copyright (C) 2015-2018 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-acd-manager.h"

#include <netinet/in.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "platform/nm-platform.h"
#include "nm-utils.h"
#include "NetworkManagerUtils.h"
#include "n-acd/src/n-acd.h"

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

typedef enum {
	STATE_INIT,
	STATE_PROBING,
	STATE_PROBE_DONE,
	STATE_ANNOUNCING,
} State;

typedef struct {
	in_addr_t address;
	gboolean duplicate;
	NAcdProbe *probe;
} AddressInfo;

struct _NMAcdManager {
	int            ifindex;
	guint8         hwaddr[ETH_ALEN];
	State          state;
	GHashTable    *addresses;
	guint          completed;
	NAcd          *acd;
	GIOChannel    *channel;
	guint          event_id;

	NMAcdCallbacks callbacks;
	gpointer user_data;
};

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

#define _NMLOG_DOMAIN         LOGD_IP4
#define _NMLOG_PREFIX_NAME    "acd"
#define _NMLOG(level, ...) \
    G_STMT_START { \
        char _sbuf[64]; \
        \
        nm_log ((level), _NMLOG_DOMAIN, \
                self && self->ifindex > 0 ? nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex) : NULL, \
                NULL, \
                "%s%s: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
                _NMLOG_PREFIX_NAME, \
                self ? nm_sprintf_buf (_sbuf, "[%p,%d]", self, self->ifindex) : "" \
                _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
    } G_STMT_END

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

static const char *
_acd_event_to_string (unsigned int event)
{
	switch (event) {
	case N_ACD_EVENT_READY:
		return "ready";
	case N_ACD_EVENT_USED:
		return "used";
	case N_ACD_EVENT_DEFENDED:
		return "defended";
	case N_ACD_EVENT_CONFLICT:
		return "conflict";
	case N_ACD_EVENT_DOWN:
		return "down";
	}
	return NULL;
}

#define acd_event_to_string_a(event) NM_UTILS_LOOKUP_STR_A (_acd_event_to_string, event)

static const char *
acd_error_to_string (int error)
{
	if (error < 0)
		return nm_strerror_native (-error);

	switch (error) {
	case _N_ACD_E_SUCCESS:
		return "success";
	case N_ACD_E_PREEMPTED:
		return "preempted";
	case N_ACD_E_INVALID_ARGUMENT:
		return "invalid argument";
	}

	g_return_val_if_reached (NULL);
}

static int
acd_error_to_nmerr (int error, gboolean always_fail)
{
	if (error < 0)
		return -nm_errno_native (error);

	if (always_fail) {
		if (NM_IN_SET (error, N_ACD_E_PREEMPTED,
		                      N_ACD_E_INVALID_ARGUMENT))
			return -NME_UNSPEC;
		g_return_val_if_reached (-NME_UNSPEC);
	}

	/* so, @error is either zero (indicating success) or one
	 * of the special status codes like N_ACD_E_*. In both cases,
	 * return the positive value here. */
	if (NM_IN_SET (error, _N_ACD_E_SUCCESS,
	                      N_ACD_E_PREEMPTED,
	                      N_ACD_E_INVALID_ARGUMENT))
		return error;

	g_return_val_if_reached (error);
}

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

/**
 * nm_acd_manager_add_address:
 * @self: a #NMAcdManager
 * @address: an IP address
 *
 * Add @address to the list of IP addresses to probe.

 * Returns: %TRUE on success, %FALSE if the address was already in the list
 */
gboolean
nm_acd_manager_add_address (NMAcdManager *self, in_addr_t address)
{
	AddressInfo *info;

	g_return_val_if_fail (self, FALSE);
	g_return_val_if_fail (self->state == STATE_INIT, FALSE);

	if (g_hash_table_lookup (self->addresses, GUINT_TO_POINTER (address)))
		return FALSE;

	info = g_slice_new0 (AddressInfo);
	info->address = address;

	g_hash_table_insert (self->addresses, GUINT_TO_POINTER (address), info);

	return TRUE;
}

static gboolean
acd_event (GIOChannel *source, GIOCondition condition, gpointer data)
{
	NMAcdManager *self = data;
	NAcdEvent *event;
	AddressInfo *info;
	gboolean emit_probe_terminated = FALSE;
	char address_str[INET_ADDRSTRLEN];
	int r;

	if (n_acd_dispatch (self->acd))
		return G_SOURCE_CONTINUE;

	while (   !n_acd_pop_event (self->acd, &event)
	       && event) {
		gs_free char *hwaddr_str = NULL;
		gboolean check_probing_done = FALSE;

		switch (event->event) {
		case N_ACD_EVENT_READY:
			n_acd_probe_get_userdata (event->ready.probe, (void **) &info);
			info->duplicate = FALSE;
			if (self->state == STATE_ANNOUNCING) {
				/* fake probe ended, start announcing */
				r = n_acd_probe_announce (info->probe, N_ACD_DEFEND_ONCE);
				if (r) {
					_LOGW ("couldn't announce address %s on interface '%s': %s",
					       nm_utils_inet4_ntop (info->address, address_str),
					       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex),
					       acd_error_to_string (r));
				} else {
					_LOGD ("announcing address %s",
					       nm_utils_inet4_ntop (info->address, address_str));
				}
			}
			check_probing_done = TRUE;
			break;
		case N_ACD_EVENT_USED:
			n_acd_probe_get_userdata (event->used.probe, (void **) &info);
			info->duplicate = TRUE;
			check_probing_done = TRUE;
			break;
		case N_ACD_EVENT_DEFENDED:
			n_acd_probe_get_userdata (event->defended.probe, (void **) &info);
			_LOGD ("defended address %s from host %s",
			       nm_utils_inet4_ntop (info->address, address_str),
			       (hwaddr_str = nm_utils_hwaddr_ntoa (event->defended.sender,
			                                           event->defended.n_sender)));
			break;
		case N_ACD_EVENT_CONFLICT:
			n_acd_probe_get_userdata (event->conflict.probe, (void **) &info);
			_LOGW ("conflict for address %s detected with host %s on interface '%s'",
			       nm_utils_inet4_ntop (info->address, address_str),
			       (hwaddr_str = nm_utils_hwaddr_ntoa (event->defended.sender,
			                                           event->defended.n_sender)),
			       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex));
			break;
		default:
			_LOGD ("unhandled event '%s'", acd_event_to_string_a (event->event));
			break;
		}

		if (   check_probing_done
		    && self->state == STATE_PROBING
		    && ++self->completed == g_hash_table_size (self->addresses)) {
			self->state = STATE_PROBE_DONE;
			emit_probe_terminated = TRUE;
		}
	}

	if (emit_probe_terminated) {
		if (self->callbacks.probe_terminated_callback) {
			self->callbacks.probe_terminated_callback (self,
			                                           self->user_data);
		}
	}

	return G_SOURCE_CONTINUE;
}

static gboolean
acd_probe_add (NMAcdManager *self,
               AddressInfo *info,
               guint64 timeout)
{
	NAcdProbeConfig *probe_config;
	int r;
	char sbuf[NM_UTILS_INET_ADDRSTRLEN];

	r = n_acd_probe_config_new (&probe_config);
	if (r) {
		_LOGW ("could not create probe config for %s on interface '%s': %s",
		       nm_utils_inet4_ntop (info->address, sbuf),
		       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex),
		       acd_error_to_string (r));
		return FALSE;
	}

	n_acd_probe_config_set_ip (probe_config, (struct in_addr) { info->address });
	n_acd_probe_config_set_timeout (probe_config, timeout);

	r = n_acd_probe (self->acd, &info->probe, probe_config);
	if (r) {
		_LOGW ("could not start probe for %s on interface '%s': %s",
		       nm_utils_inet4_ntop (info->address, sbuf),
		       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex),
		       acd_error_to_string (r));
		n_acd_probe_config_free (probe_config);
		return FALSE;
	}

	n_acd_probe_set_userdata (info->probe, info);
	n_acd_probe_config_free (probe_config);

	return TRUE;
}

static int
acd_init (NMAcdManager *self)
{
	NAcdConfig *config;
	int r;

	if (self->acd)
		return 0;

	r = n_acd_config_new (&config);
	if (r)
		return r;

	n_acd_config_set_ifindex (config, self->ifindex);
	n_acd_config_set_transport (config, N_ACD_TRANSPORT_ETHERNET);
	n_acd_config_set_mac (config, self->hwaddr, ETH_ALEN);

	r = n_acd_new (&self->acd, config);
	n_acd_config_free (config);
	return r;
}

/**
 * nm_acd_manager_start_probe:
 * @self: a #NMAcdManager
 * @timeout: maximum probe duration in milliseconds
 * @error: location to store error, or %NULL
 *
 * Start probing IP addresses for duplicates; when the probe terminates a
 * PROBE_TERMINATED signal is emitted.
 *
 * Returns: 0 on success or a negative NetworkManager error code (NME_*).
 */
int
nm_acd_manager_start_probe (NMAcdManager *self, guint timeout)
{
	GHashTableIter iter;
	AddressInfo *info;
	gboolean success = FALSE;
	int fd, r;

	g_return_val_if_fail (self, FALSE);
	g_return_val_if_fail (self->state == STATE_INIT, FALSE);

	r = acd_init (self);
	if (r) {
		_LOGW ("couldn't init ACD for probing on interface '%s': %s",
		       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex),
		       acd_error_to_string (r));
		return acd_error_to_nmerr (r, TRUE);
	}

	self->completed = 0;

	g_hash_table_iter_init (&iter, self->addresses);
	while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info))
		success |= acd_probe_add (self, info, timeout);

	if (success)
		self->state = STATE_PROBING;

	n_acd_get_fd (self->acd, &fd);
	self->channel = g_io_channel_unix_new (fd);
	self->event_id = g_io_add_watch (self->channel, G_IO_IN, acd_event, self);

	return success ? 0 : -NME_UNSPEC;
}

/**
 * nm_acd_manager_check_address:
 * @self: a #NMAcdManager
 * @address: an IP address
 *
 * Check if an IP address is duplicate. @address must have been added with
 * nm_acd_manager_add_address().
 *
 * Returns: %TRUE if the address is not duplicate, %FALSE otherwise
 */
gboolean
nm_acd_manager_check_address (NMAcdManager *self, in_addr_t address)
{
	AddressInfo *info;

	g_return_val_if_fail (self, FALSE);
	g_return_val_if_fail (NM_IN_SET (self->state, STATE_INIT, STATE_PROBE_DONE), FALSE);

	info = g_hash_table_lookup (self->addresses, GUINT_TO_POINTER (address));
	g_return_val_if_fail (info, FALSE);

	return !info->duplicate;
}

/**
 * nm_acd_manager_announce_addresses:
 * @self: a #NMAcdManager
 *
 * Start announcing addresses.
 *
 * Returns: a negative NetworkManager error number or zero on success.
 */
int
nm_acd_manager_announce_addresses (NMAcdManager *self)
{
	GHashTableIter iter;
	AddressInfo *info;
	int r;
	gboolean success = TRUE;

	r = acd_init (self);
	if (r) {
		_LOGW ("couldn't init ACD for announcing addresses on interface '%s': %s",
		       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex),
		       acd_error_to_string (r));
		return acd_error_to_nmerr (r, TRUE);
	}

	if (self->state == STATE_INIT) {
		/* n-acd can't announce without probing, therefore let's
		 * start a fake probe with zero timeout and then perform
		 * the announcement. */
		g_hash_table_iter_init (&iter, self->addresses);
		while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info)) {
			if (!acd_probe_add (self, info, 0))
				success = FALSE;
		}
		self->state = STATE_ANNOUNCING;
	} else if (self->state == STATE_ANNOUNCING) {
		char sbuf[NM_UTILS_INET_ADDRSTRLEN];

		g_hash_table_iter_init (&iter, self->addresses);
		while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info)) {
			if (info->duplicate)
				continue;
			r = n_acd_probe_announce (info->probe, N_ACD_DEFEND_ONCE);
			if (r) {
				_LOGW ("couldn't announce address %s on interface '%s': %s",
				       nm_utils_inet4_ntop (info->address, sbuf),
				       nm_platform_link_get_name (NM_PLATFORM_GET, self->ifindex),
				       acd_error_to_string (r));
				success = FALSE;
			} else
				_LOGD ("announcing address %s", nm_utils_inet4_ntop (info->address, sbuf));
		}
	}

	return success ? 0 : -NME_UNSPEC;
}

static void
destroy_address_info (gpointer data)
{
	AddressInfo *info = (AddressInfo *) data;

	n_acd_probe_free (info->probe);

	g_slice_free (AddressInfo, info);
}

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

NMAcdManager *
nm_acd_manager_new (int ifindex,
                    const guint8 *hwaddr,
                    guint hwaddr_len,
                    const NMAcdCallbacks *callbacks,
                    gpointer user_data)
{
	NMAcdManager *self;

	g_return_val_if_fail (ifindex > 0, NULL);
	g_return_val_if_fail (hwaddr, NULL);
	g_return_val_if_fail (hwaddr_len == ETH_ALEN, NULL);

	self = g_slice_new0 (NMAcdManager);

	if (callbacks)
		self->callbacks = *callbacks;
	self->user_data = user_data;

	self->addresses = g_hash_table_new_full (nm_direct_hash, NULL,
	                                         NULL, destroy_address_info);
	self->state = STATE_INIT;
	self->ifindex = ifindex;
	memcpy (self->hwaddr, hwaddr, ETH_ALEN);
	return self;
}

void
nm_acd_manager_free (NMAcdManager *self)
{
	g_return_if_fail (self);

	if (self->callbacks.user_data_destroy)
		self->callbacks.user_data_destroy (self->user_data);

	nm_clear_pointer (&self->addresses, g_hash_table_destroy);
	nm_clear_pointer (&self->channel, g_io_channel_unref);
	nm_clear_g_source (&self->event_id);
	nm_clear_pointer (&self->acd, n_acd_unref);

	g_slice_free (NMAcdManager, self);
}