summaryrefslogtreecommitdiffstats
path: root/src/netlink.c
blob: 2a9216c3905807dc3b010d0ce2d7873a6abfc792 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
 * Copyright (c) 2020 Yubico AB. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <sys/socket.h>

#include <linux/genetlink.h>
#include <linux/netlink.h>
#include <linux/nfc.h>

#include <errno.h>
#include <limits.h>

#include "fido.h"
#include "netlink.h"

#ifdef FIDO_FUZZ
static ssize_t (*fuzz_read)(int, void *, size_t);
static ssize_t (*fuzz_write)(int, const void *, size_t);
# define READ	fuzz_read
# define WRITE	fuzz_write
#else
# define READ	read
# define WRITE	write
#endif

#ifndef SOL_NETLINK
#define SOL_NETLINK	270
#endif

#define NETLINK_POLL_MS	100

/* XXX avoid signed NLA_ALIGNTO */
#undef NLA_HDRLEN
#define NLA_HDRLEN	NLMSG_ALIGN(sizeof(struct nlattr))

typedef struct nlmsgbuf {
	size_t         siz; /* alloc size */
	size_t         len; /* of payload */
	unsigned char *ptr; /* in payload */
	union {
		struct nlmsghdr   nlmsg;
		char              buf[NLMSG_HDRLEN]; /* align */
	}              u;
	unsigned char  payload[];
} nlmsgbuf_t;

typedef struct genlmsgbuf {
	union {
		struct genlmsghdr genl;
		char              buf[GENL_HDRLEN];  /* align */
	}              u;
} genlmsgbuf_t;

typedef struct nlamsgbuf {
	size_t         siz; /* alloc size */
	size_t         len; /* of payload */
	unsigned char *ptr; /* in payload */
	union {
		struct nlattr     nla;
		char              buf[NLA_HDRLEN];   /* align */
	}              u;
	unsigned char  payload[];
} nlamsgbuf_t;

typedef struct nl_family {
	uint16_t id;
	uint32_t mcastgrp;
} nl_family_t;

typedef struct nl_poll {
	uint32_t     dev;
	unsigned int eventcnt;
} nl_poll_t;

typedef struct nl_target {
	int       found;
	uint32_t *value;
} nl_target_t;

static const void *
nlmsg_ptr(const nlmsgbuf_t *m)
{
	return (&m->u.nlmsg);
}

static size_t
nlmsg_len(const nlmsgbuf_t *m)
{
	return (m->u.nlmsg.nlmsg_len);
}

static uint16_t
nlmsg_type(const nlmsgbuf_t *m)
{
	return (m->u.nlmsg.nlmsg_type);
}

static nlmsgbuf_t *
nlmsg_new(uint16_t type, uint16_t flags, size_t len)
{
	nlmsgbuf_t *m;
	size_t siz;

	if (len > SIZE_MAX - sizeof(*m) ||
	    (siz = sizeof(*m) + len) > UINT16_MAX ||
	    (m = calloc(1, siz)) == NULL)
		return (NULL);

	m->siz = siz;
	m->len = len;
	m->ptr = m->payload;
	m->u.nlmsg.nlmsg_type = type;
	m->u.nlmsg.nlmsg_flags = NLM_F_REQUEST | flags;
	m->u.nlmsg.nlmsg_len = NLMSG_HDRLEN;

	return (m);
}

static nlamsgbuf_t *
nla_from_buf(const unsigned char **ptr, size_t *len)
{
	nlamsgbuf_t h, *a;
	size_t nlalen, skip;

	if (*len < sizeof(h.u))
		return (NULL);

	memset(&h, 0, sizeof(h));
	memcpy(&h.u, *ptr, sizeof(h.u));

	if ((nlalen = h.u.nla.nla_len) < sizeof(h.u) || nlalen > *len ||
	    nlalen - sizeof(h.u) > UINT16_MAX ||
	    nlalen > SIZE_MAX - sizeof(*a) ||
	    (skip = NLMSG_ALIGN(nlalen)) > *len ||
	    (a = calloc(1, sizeof(*a) + nlalen - sizeof(h.u))) == NULL)
		return (NULL);

	memcpy(&a->u, *ptr, nlalen);
	a->siz = sizeof(*a) + nlalen - sizeof(h.u);
	a->ptr = a->payload;
	a->len = nlalen - sizeof(h.u);
	*ptr += skip;
	*len -= skip;

	return (a);
}

static nlamsgbuf_t *
nla_getattr(nlamsgbuf_t *a)
{
	return (nla_from_buf((void *)&a->ptr, &a->len));
}

static uint16_t
nla_type(const nlamsgbuf_t *a)
{
	return (a->u.nla.nla_type);
}

static nlamsgbuf_t *
nlmsg_getattr(nlmsgbuf_t *m)
{
	return (nla_from_buf((void *)&m->ptr, &m->len));
}

static int
nla_read(nlamsgbuf_t *a, void *buf, size_t cnt)
{
	if (cnt > a->u.nla.nla_len ||
	    fido_buf_read((void *)&a->ptr, &a->len, buf, cnt) < 0)
		return (-1);

	a->u.nla.nla_len = (uint16_t)(a->u.nla.nla_len - cnt);

	return (0);
}

static nlmsgbuf_t *
nlmsg_from_buf(const unsigned char **ptr, size_t *len)
{
	nlmsgbuf_t h, *m;
	size_t msglen, skip;

	if (*len < sizeof(h.u))
		return (NULL);

	memset(&h, 0, sizeof(h));
	memcpy(&h.u, *ptr, sizeof(h.u));

	if ((msglen = h.u.nlmsg.nlmsg_len) < sizeof(h.u) || msglen > *len ||
	    msglen - sizeof(h.u) > UINT16_MAX ||
	    (skip = NLMSG_ALIGN(msglen)) > *len ||
	    (m = nlmsg_new(0, 0, msglen - sizeof(h.u))) == NULL)
		return (NULL);

	memcpy(&m->u, *ptr, msglen);
	*ptr += skip;
	*len -= skip;

	return (m);
}

static int
nlmsg_read(nlmsgbuf_t *m, void *buf, size_t cnt)
{
	if (cnt > m->u.nlmsg.nlmsg_len ||
	    fido_buf_read((void *)&m->ptr, &m->len, buf, cnt) < 0)
		return (-1);

	m->u.nlmsg.nlmsg_len = (uint32_t)(m->u.nlmsg.nlmsg_len - cnt);

	return (0);
}

static int
nlmsg_write(nlmsgbuf_t *m, const void *buf, size_t cnt)
{
	if (cnt > UINT32_MAX - m->u.nlmsg.nlmsg_len ||
	    fido_buf_write(&m->ptr, &m->len, buf, cnt) < 0)
		return (-1);

	m->u.nlmsg.nlmsg_len = (uint32_t)(m->u.nlmsg.nlmsg_len + cnt);

	return (0);
}

static int
nlmsg_set_genl(nlmsgbuf_t *m, uint8_t cmd)
{
	genlmsgbuf_t g;

	memset(&g, 0, sizeof(g));
	g.u.genl.cmd = cmd;
	g.u.genl.version = NFC_GENL_VERSION;

	return (nlmsg_write(m, &g, sizeof(g)));
}

static int
nlmsg_get_genl(nlmsgbuf_t *m, uint8_t cmd)
{
	genlmsgbuf_t g;

	memset(&g, 0, sizeof(g));

	if (nlmsg_read(m, &g, sizeof(g)) < 0 || g.u.genl.cmd != cmd)
		return (-1);

	return (0);
}

static int
nlmsg_get_status(nlmsgbuf_t *m)
{
	int status;

	if (nlmsg_read(m, &status, sizeof(status)) < 0 || status == INT_MIN)
		return (-1);
	if (status < 0)
		status = -status;

	return (status);
}

static int
nlmsg_setattr(nlmsgbuf_t *m, uint16_t type, const void *ptr, size_t len)
{
	int r;
	char *padding;
	size_t skip;
	nlamsgbuf_t a;

	if ((skip = NLMSG_ALIGN(len)) > UINT16_MAX - sizeof(a.u) ||
	    skip < len || (padding = calloc(1, skip - len)) == NULL)
		return (-1);

	memset(&a, 0, sizeof(a));
	a.u.nla.nla_type = type;
	a.u.nla.nla_len = (uint16_t)(len + sizeof(a.u));
	r = nlmsg_write(m, &a.u, sizeof(a.u)) < 0 ||
	    nlmsg_write(m, ptr, len) < 0 ||
	    nlmsg_write(m, padding, skip - len) < 0 ? -1 : 0;

	free(padding);

	return (r);
}

static int
nlmsg_set_u16(nlmsgbuf_t *m, uint16_t type, uint16_t val)
{
	return (nlmsg_setattr(m, type, &val, sizeof(val)));
}

static int
nlmsg_set_u32(nlmsgbuf_t *m, uint16_t type, uint32_t val)
{
	return (nlmsg_setattr(m, type, &val, sizeof(val)));
}

static int
nlmsg_set_str(nlmsgbuf_t *m, uint16_t type, const char *val)
{
	return (nlmsg_setattr(m, type, val, strlen(val) + 1));
}

static int
nla_get_u16(nlamsgbuf_t *a, uint16_t *v)
{
	return (nla_read(a, v, sizeof(*v)));
}

static int
nla_get_u32(nlamsgbuf_t *a, uint32_t *v)
{
	return (nla_read(a, v, sizeof(*v)));
}

static char *
nla_get_str(nlamsgbuf_t *a)
{
	size_t n;
	char *s = NULL;

	if ((n = a->len) < 1 || a->ptr[n - 1] != '\0' ||
	    (s = calloc(1, n)) == NULL || nla_read(a, s, n) < 0) {
		free(s);
		return (NULL);
	}
	s[n - 1] = '\0';

	return (s);
}

static int
nlmsg_tx(int fd, const nlmsgbuf_t *m)
{
	ssize_t r;

	if ((r = WRITE(fd, nlmsg_ptr(m), nlmsg_len(m))) == -1) {
		fido_log_error(errno, "%s: write", __func__);
		return (-1);
	}
	if (r < 0 || (size_t)r != nlmsg_len(m)) {
		fido_log_debug("%s: %zd != %zu", __func__, r, nlmsg_len(m));
		return (-1);
	}
	fido_log_xxd(nlmsg_ptr(m), nlmsg_len(m), "%s", __func__);

	return (0);
}

static ssize_t
nlmsg_rx(int fd, unsigned char *ptr, size_t len, int ms)
{
	ssize_t r;

	if (len > SSIZE_MAX) {
		fido_log_debug("%s: len", __func__);
		return (-1);
	}
	if (fido_hid_unix_wait(fd, ms, NULL) < 0) {
		fido_log_debug("%s: fido_hid_unix_wait", __func__);
		return (-1);
	}
	if ((r = READ(fd, ptr, len)) == -1) {
		fido_log_error(errno, "%s: read %zd", __func__, r);
		return (-1);
	}
	fido_log_xxd(ptr, (size_t)r, "%s", __func__);

	return (r);
}

static int
nlmsg_iter(nlmsgbuf_t *m, void *arg, int (*parser)(nlamsgbuf_t *, void *))
{
	nlamsgbuf_t *a;
	int r;

	while ((a = nlmsg_getattr(m)) != NULL) {
		r = parser(a, arg);
		free(a);
		if (r < 0) {
			fido_log_debug("%s: parser", __func__);
			return (-1);
		}
	}

	return (0);
}

static int
nla_iter(nlamsgbuf_t *g, void *arg, int (*parser)(nlamsgbuf_t *, void *))
{
	nlamsgbuf_t *a;
	int r;

	while ((a = nla_getattr(g)) != NULL) {
		r = parser(a, arg);
		free(a);
		if (r < 0) {
			fido_log_debug("%s: parser", __func__);
			return (-1);
		}
	}

	return (0);
}

static int
nl_parse_reply(const uint8_t *blob, size_t blob_len, uint16_t msg_type,
    uint8_t genl_cmd, void *arg, int (*parser)(nlamsgbuf_t *, void *))
{
	nlmsgbuf_t *m;
	int r;

	while (blob_len) {
		if ((m = nlmsg_from_buf(&blob, &blob_len)) == NULL) {
			fido_log_debug("%s: nlmsg", __func__);
			return (-1);
		}
		if (nlmsg_type(m) == NLMSG_ERROR) {
			r = nlmsg_get_status(m);
			free(m);
			return (r);
		}
		if (nlmsg_type(m) != msg_type ||
		    nlmsg_get_genl(m, genl_cmd) < 0) {
			fido_log_debug("%s: skipping", __func__);
			free(m);
			continue;
		}
		if (parser != NULL && nlmsg_iter(m, arg, parser) < 0) {
			fido_log_debug("%s: nlmsg_iter", __func__);
			free(m);
			return (-1);
		}
		free(m);
	}

	return (0);
}

static int
parse_mcastgrp(nlamsgbuf_t *a, void *arg)
{
	nl_family_t *family = arg;
	char *name;

	switch (nla_type(a)) {
	case CTRL_ATTR_MCAST_GRP_NAME:
		if ((name = nla_get_str(a)) == NULL ||
		    strcmp(name, NFC_GENL_MCAST_EVENT_NAME) != 0) {
			free(name);
			return (-1); /* XXX skip? */
		}
		free(name);
		return (0);
	case CTRL_ATTR_MCAST_GRP_ID:
		if (family->mcastgrp)
			break;
		if (nla_get_u32(a, &family->mcastgrp) < 0) {
			fido_log_debug("%s: group", __func__);
			return (-1);
		}
		return (0);
	}

	fido_log_debug("%s: ignoring nla 0x%x", __func__, nla_type(a));

	return (0);
}

static int
parse_mcastgrps(nlamsgbuf_t *a, void *arg)
{
	return (nla_iter(a, arg, parse_mcastgrp));
}

static int
parse_family(nlamsgbuf_t *a, void *arg)
{
	nl_family_t *family = arg;

	switch (nla_type(a)) {
	case CTRL_ATTR_FAMILY_ID:
		if (family->id)
			break;
		if (nla_get_u16(a, &family->id) < 0) {
			fido_log_debug("%s: id", __func__);
			return (-1);
		}
		return (0);
	case CTRL_ATTR_MCAST_GROUPS:
		return (nla_iter(a, family, parse_mcastgrps));
	}

	fido_log_debug("%s: ignoring nla 0x%x", __func__, nla_type(a));

	return (0);
}

static int
nl_get_nfc_family(int fd, uint16_t *type, uint32_t *mcastgrp)
{
	nlmsgbuf_t *m;
	uint8_t reply[512];
	nl_family_t family;
	ssize_t r;
	int ok;

	if ((m = nlmsg_new(GENL_ID_CTRL, 0, 64)) == NULL ||
	    nlmsg_set_genl(m, CTRL_CMD_GETFAMILY) < 0 ||
	    nlmsg_set_u16(m, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL) < 0 ||
	    nlmsg_set_str(m, CTRL_ATTR_FAMILY_NAME, NFC_GENL_NAME) < 0 ||
	    nlmsg_tx(fd, m) < 0) {
		free(m);
		return (-1);
	}
	free(m);
	memset(&family, 0, sizeof(family));
	if ((r = nlmsg_rx(fd, reply, sizeof(reply), -1)) < 0) {
		fido_log_debug("%s: nlmsg_rx", __func__);
		return (-1);
	}
	if ((ok = nl_parse_reply(reply, (size_t)r, GENL_ID_CTRL,
	    CTRL_CMD_NEWFAMILY, &family, parse_family)) != 0) {
		fido_log_debug("%s: nl_parse_reply: %d", __func__, ok);
		return (-1);
	}
	if (family.id == 0 || family.mcastgrp == 0) {
		fido_log_debug("%s: missing attr", __func__);
		return (-1);
	}
	*type = family.id;
	*mcastgrp = family.mcastgrp;

	return (0);
}

static int
parse_target(nlamsgbuf_t *a, void *arg)
{
	nl_target_t *t = arg;

	if (t->found || nla_type(a) != NFC_ATTR_TARGET_INDEX) {
		fido_log_debug("%s: ignoring nla 0x%x", __func__, nla_type(a));
		return (0);
	}
	if (nla_get_u32(a, t->value) < 0) {
		fido_log_debug("%s: target", __func__);
		return (-1);
	}
	t->found = 1;

	return (0);
}

int
fido_nl_power_nfc(fido_nl_t *nl, uint32_t dev)
{
	nlmsgbuf_t *m;
	uint8_t reply[512];
	ssize_t r;
	int ok;

	if ((m = nlmsg_new(nl->nfc_type, NLM_F_ACK, 64)) == NULL ||
	    nlmsg_set_genl(m, NFC_CMD_DEV_UP) < 0 ||
	    nlmsg_set_u32(m, NFC_ATTR_DEVICE_INDEX, dev) < 0 ||
	    nlmsg_tx(nl->fd, m) < 0) {
		free(m);
		return (-1);
	}
	free(m);
	if ((r = nlmsg_rx(nl->fd, reply, sizeof(reply), -1)) < 0) {
		fido_log_debug("%s: nlmsg_rx", __func__);
		return (-1);
	}
	if ((ok = nl_parse_reply(reply, (size_t)r, nl->nfc_type,
	    NFC_CMD_DEV_UP, NULL, NULL)) != 0 && ok != EALREADY) {
		fido_log_debug("%s: nl_parse_reply: %d", __func__, ok);
		return (-1);
	}

	return (0);
}

static int
nl_nfc_poll(fido_nl_t *nl, uint32_t dev)
{
	nlmsgbuf_t *m;
	uint8_t reply[512];
	ssize_t r;
	int ok;

	if ((m = nlmsg_new(nl->nfc_type, NLM_F_ACK, 64)) == NULL ||
	    nlmsg_set_genl(m, NFC_CMD_START_POLL) < 0 ||
	    nlmsg_set_u32(m, NFC_ATTR_DEVICE_INDEX, dev) < 0 ||
	    nlmsg_set_u32(m, NFC_ATTR_PROTOCOLS, NFC_PROTO_ISO14443_MASK) < 0 ||
	    nlmsg_tx(nl->fd, m) < 0) {
		free(m);
		return (-1);
	}
	free(m);
	if ((r = nlmsg_rx(nl->fd, reply, sizeof(reply), -1)) < 0) {
		fido_log_debug("%s: nlmsg_rx", __func__);
		return (-1);
	}
	if ((ok = nl_parse_reply(reply, (size_t)r, nl->nfc_type,
	    NFC_CMD_START_POLL, NULL, NULL)) != 0) {
		fido_log_debug("%s: nl_parse_reply: %d", __func__, ok);
		return (-1);
	}

	return (0);
}

static int
nl_dump_nfc_target(fido_nl_t *nl, uint32_t dev, uint32_t *target, int ms)
{
	nlmsgbuf_t *m;
	nl_target_t t;
	uint8_t reply[512];
	ssize_t r;
	int ok;

	if ((m = nlmsg_new(nl->nfc_type, NLM_F_DUMP, 64)) == NULL ||
	    nlmsg_set_genl(m, NFC_CMD_GET_TARGET) < 0 ||
	    nlmsg_set_u32(m, NFC_ATTR_DEVICE_INDEX, dev) < 0 ||
	    nlmsg_tx(nl->fd, m) < 0) {
		free(m);
		return (-1);
	}
	free(m);
	if ((r = nlmsg_rx(nl->fd, reply, sizeof(reply), ms)) < 0) {
		fido_log_debug("%s: nlmsg_rx", __func__);
		return (-1);
	}
	memset(&t, 0, sizeof(t));
	t.value = target;
	if ((ok = nl_parse_reply(reply, (size_t)r, nl->nfc_type,
	    NFC_CMD_GET_TARGET, &t, parse_target)) != 0) {
		fido_log_debug("%s: nl_parse_reply: %d", __func__, ok);
		return (-1);
	}
	if (!t.found) {
		fido_log_debug("%s: target not found", __func__);
		return (-1);
	}

	return (0);
}

static int
parse_nfc_event(nlamsgbuf_t *a, void *arg)
{
	nl_poll_t *ctx = arg;
	uint32_t dev;

	if (nla_type(a) != NFC_ATTR_DEVICE_INDEX) {
		fido_log_debug("%s: ignoring nla 0x%x", __func__, nla_type(a));
		return (0);
	}
	if (nla_get_u32(a, &dev) < 0) {
		fido_log_debug("%s: dev", __func__);
		return (-1);
	}
	if (dev == ctx->dev)
		ctx->eventcnt++;
	else
		fido_log_debug("%s: ignoring dev 0x%x", __func__, dev);

	return (0);
}

int
fido_nl_get_nfc_target(fido_nl_t *nl, uint32_t dev, uint32_t *target)
{
	uint8_t reply[512];
	nl_poll_t ctx;
	ssize_t r;
	int ok;

	if (nl_nfc_poll(nl, dev) < 0) {
		fido_log_debug("%s: nl_nfc_poll", __func__);
		return (-1);
	}
#ifndef FIDO_FUZZ
	if (setsockopt(nl->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
	    &nl->nfc_mcastgrp, sizeof(nl->nfc_mcastgrp)) == -1) {
		fido_log_error(errno, "%s: setsockopt add", __func__);
		return (-1);
	}
#endif
	r = nlmsg_rx(nl->fd, reply, sizeof(reply), NETLINK_POLL_MS);
#ifndef FIDO_FUZZ
	if (setsockopt(nl->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
	    &nl->nfc_mcastgrp, sizeof(nl->nfc_mcastgrp)) == -1) {
		fido_log_error(errno, "%s: setsockopt drop", __func__);
		return (-1);
	}
#endif
	if (r < 0) {
		fido_log_debug("%s: nlmsg_rx", __func__);
		return (-1);
	}
	memset(&ctx, 0, sizeof(ctx));
	ctx.dev = dev;
	if ((ok = nl_parse_reply(reply, (size_t)r, nl->nfc_type,
	    NFC_EVENT_TARGETS_FOUND, &ctx, parse_nfc_event)) != 0) {
		fido_log_debug("%s: nl_parse_reply: %d", __func__, ok);
		return (-1);
	}
	if (ctx.eventcnt == 0) {
		fido_log_debug("%s: dev 0x%x not observed", __func__, dev);
		return (-1);
	}
	if (nl_dump_nfc_target(nl, dev, target, -1) < 0) {
		fido_log_debug("%s: nl_dump_nfc_target", __func__);
		return (-1);
	}

	return (0);
}

void
fido_nl_free(fido_nl_t **nlp)
{
	fido_nl_t *nl;

	if (nlp == NULL || (nl = *nlp) == NULL)
		return;
	if (nl->fd != -1 && close(nl->fd) == -1)
		fido_log_error(errno, "%s: close", __func__);

	free(nl);
	*nlp = NULL;
}

fido_nl_t *
fido_nl_new(void)
{
	fido_nl_t *nl;
	int ok = -1;

	if ((nl = calloc(1, sizeof(*nl))) == NULL)
		return (NULL);
	if ((nl->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC,
	    NETLINK_GENERIC)) == -1) {
		fido_log_error(errno, "%s: socket", __func__);
		goto fail;
	}
	nl->saddr.nl_family = AF_NETLINK;
	if (bind(nl->fd, (struct sockaddr *)&nl->saddr,
	    sizeof(nl->saddr)) == -1) {
		fido_log_error(errno, "%s: bind", __func__);
		goto fail;
	}
	if (nl_get_nfc_family(nl->fd, &nl->nfc_type, &nl->nfc_mcastgrp) < 0) {
		fido_log_debug("%s: nl_get_nfc_family", __func__);
		goto fail;
	}

	ok = 0;
fail:
	if (ok < 0)
		fido_nl_free(&nl);

	return (nl);
}

#ifdef FIDO_FUZZ
void
set_netlink_io_functions(ssize_t (*read_f)(int, void *, size_t),
    ssize_t (*write_f)(int, const void *, size_t))
{
	fuzz_read = read_f;
	fuzz_write = write_f;
}
#endif