summaryrefslogtreecommitdiffstats
path: root/bfdd/control.c
blob: 98fd813ef7216668a4360b473f07e33cffbd36d7 (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
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
// SPDX-License-Identifier: GPL-2.0-or-later
/*********************************************************************
 * Copyright 2017-2018 Network Device Education Foundation, Inc. ("NetDEF")
 *
 * control.c: implements the BFD daemon control socket. It will be used
 * to talk with clients daemon/scripts/consumers.
 *
 * Authors
 * -------
 * Rafael Zalamena <rzalamena@opensourcerouting.org>
 */

#include <zebra.h>

#include <fcntl.h>
#include <sys/stat.h>

#include <sys/un.h>

#include "bfd.h"

/*
 * Prototypes
 */
static int sock_set_nonblock(int fd);
struct bfd_control_queue *control_queue_new(struct bfd_control_socket *bcs);
static void control_queue_free(struct bfd_control_socket *bcs,
			       struct bfd_control_queue *bcq);
static int control_queue_dequeue(struct bfd_control_socket *bcs);
static int control_queue_enqueue(struct bfd_control_socket *bcs,
				 struct bfd_control_msg *bcm);
static int control_queue_enqueue_first(struct bfd_control_socket *bcs,
				       struct bfd_control_msg *bcm);
struct bfd_notify_peer *control_notifypeer_new(struct bfd_control_socket *bcs,
					       struct bfd_session *bs);
static void control_notifypeer_free(struct bfd_control_socket *bcs,
				    struct bfd_notify_peer *bnp);
struct bfd_notify_peer *control_notifypeer_find(struct bfd_control_socket *bcs,
						struct bfd_session *bs);


struct bfd_control_socket *control_new(int sd);
static void control_free(struct bfd_control_socket *bcs);
static void control_reset_buf(struct bfd_control_buffer *bcb);
static void control_read(struct event *t);
static void control_write(struct event *t);

static void control_handle_request_add(struct bfd_control_socket *bcs,
				       struct bfd_control_msg *bcm);
static void control_handle_request_del(struct bfd_control_socket *bcs,
				       struct bfd_control_msg *bcm);
static int notify_add_cb(struct bfd_peer_cfg *bpc, void *arg);
static int notify_del_cb(struct bfd_peer_cfg *bpc, void *arg);
static void control_handle_notify_add(struct bfd_control_socket *bcs,
				      struct bfd_control_msg *bcm);
static void control_handle_notify_del(struct bfd_control_socket *bcs,
				      struct bfd_control_msg *bcm);
static void _control_handle_notify(struct hash_bucket *hb, void *arg);
static void control_handle_notify(struct bfd_control_socket *bcs,
				  struct bfd_control_msg *bcm);
static void control_response(struct bfd_control_socket *bcs, uint16_t id,
			     const char *status, const char *error);

static void _control_notify_config(struct bfd_control_socket *bcs,
				   const char *op, struct bfd_session *bs);
static void _control_notify(struct bfd_control_socket *bcs,
			    struct bfd_session *bs);


/*
 * Functions
 */
static int sock_set_nonblock(int fd)
{
	int flags;

	flags = fcntl(fd, F_GETFL, 0);
	if (flags == -1) {
		zlog_warn("%s: fcntl F_GETFL: %s", __func__, strerror(errno));
		return -1;
	}

	flags |= O_NONBLOCK;
	if (fcntl(fd, F_SETFL, flags) == -1) {
		zlog_warn("%s: fcntl F_SETFL: %s", __func__, strerror(errno));
		return -1;
	}

	return 0;
}

int control_init(const char *path)
{
	int sd;
	mode_t umval;
	struct sockaddr_un sun_ = {
		.sun_family = AF_UNIX,
	};

	assert(path);

	strlcpy(sun_.sun_path, path, sizeof(sun_.sun_path));

	/* Remove previously created sockets. */
	unlink(sun_.sun_path);

	sd = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
	if (sd == -1) {
		zlog_err("%s: socket: %s", __func__, strerror(errno));
		return -1;
	}

	umval = umask(0);
	if (bind(sd, (struct sockaddr *)&sun_, sizeof(sun_)) == -1) {
		zlog_err("%s: bind: %s", __func__, strerror(errno));
		close(sd);
		return -1;
	}
	umask(umval);

	if (listen(sd, SOMAXCONN) == -1) {
		zlog_err("%s: listen: %s", __func__, strerror(errno));
		close(sd);
		return -1;
	}

	sock_set_nonblock(sd);

	bglobal.bg_csock = sd;

	return 0;
}

void control_shutdown(void)
{
	struct bfd_control_socket *bcs;

	event_cancel(&bglobal.bg_csockev);

	socket_close(&bglobal.bg_csock);

	while (!TAILQ_EMPTY(&bglobal.bg_bcslist)) {
		bcs = TAILQ_FIRST(&bglobal.bg_bcslist);
		control_free(bcs);
	}
}

void control_accept(struct event *t)
{
	int csock, sd = EVENT_FD(t);

	csock = accept(sd, NULL, 0);
	if (csock == -1) {
		zlog_warn("%s: accept: %s", __func__, strerror(errno));
		return;
	}

	control_new(csock);

	event_add_read(master, control_accept, NULL, sd, &bglobal.bg_csockev);
}


/*
 * Client handling
 */
struct bfd_control_socket *control_new(int sd)
{
	struct bfd_control_socket *bcs;

	bcs = XCALLOC(MTYPE_BFDD_CONTROL, sizeof(*bcs));

	/* Disable notifications by default. */
	bcs->bcs_notify = 0;

	bcs->bcs_sd = sd;
	event_add_read(master, control_read, bcs, sd, &bcs->bcs_ev);

	TAILQ_INIT(&bcs->bcs_bcqueue);
	TAILQ_INIT(&bcs->bcs_bnplist);
	TAILQ_INSERT_TAIL(&bglobal.bg_bcslist, bcs, bcs_entry);

	return bcs;
}

static void control_free(struct bfd_control_socket *bcs)
{
	struct bfd_control_queue *bcq;
	struct bfd_notify_peer *bnp;

	event_cancel(&(bcs->bcs_ev));
	event_cancel(&(bcs->bcs_outev));

	close(bcs->bcs_sd);

	TAILQ_REMOVE(&bglobal.bg_bcslist, bcs, bcs_entry);

	/* Empty output queue. */
	while (!TAILQ_EMPTY(&bcs->bcs_bcqueue)) {
		bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
		control_queue_free(bcs, bcq);
	}

	/* Empty notification list. */
	while (!TAILQ_EMPTY(&bcs->bcs_bnplist)) {
		bnp = TAILQ_FIRST(&bcs->bcs_bnplist);
		control_notifypeer_free(bcs, bnp);
	}

	control_reset_buf(&bcs->bcs_bin);
	XFREE(MTYPE_BFDD_CONTROL, bcs);
}

struct bfd_notify_peer *control_notifypeer_new(struct bfd_control_socket *bcs,
					       struct bfd_session *bs)
{
	struct bfd_notify_peer *bnp;

	bnp = control_notifypeer_find(bcs, bs);
	if (bnp)
		return bnp;

	bnp = XCALLOC(MTYPE_BFDD_CONTROL, sizeof(*bnp));

	TAILQ_INSERT_TAIL(&bcs->bcs_bnplist, bnp, bnp_entry);
	bnp->bnp_bs = bs;
	bs->refcount++;

	return bnp;
}

static void control_notifypeer_free(struct bfd_control_socket *bcs,
				    struct bfd_notify_peer *bnp)
{
	TAILQ_REMOVE(&bcs->bcs_bnplist, bnp, bnp_entry);
	bnp->bnp_bs->refcount--;
	XFREE(MTYPE_BFDD_CONTROL, bnp);
}

struct bfd_notify_peer *control_notifypeer_find(struct bfd_control_socket *bcs,
						struct bfd_session *bs)
{
	struct bfd_notify_peer *bnp;

	TAILQ_FOREACH (bnp, &bcs->bcs_bnplist, bnp_entry) {
		if (bnp->bnp_bs == bs)
			return bnp;
	}

	return NULL;
}

struct bfd_control_queue *control_queue_new(struct bfd_control_socket *bcs)
{
	struct bfd_control_queue *bcq;

	bcq = XCALLOC(MTYPE_BFDD_NOTIFICATION, sizeof(*bcq));

	control_reset_buf(&bcq->bcq_bcb);
	TAILQ_INSERT_TAIL(&bcs->bcs_bcqueue, bcq, bcq_entry);

	return bcq;
}

static void control_queue_free(struct bfd_control_socket *bcs,
			       struct bfd_control_queue *bcq)
{
	control_reset_buf(&bcq->bcq_bcb);
	TAILQ_REMOVE(&bcs->bcs_bcqueue, bcq, bcq_entry);
	XFREE(MTYPE_BFDD_NOTIFICATION, bcq);
}

static int control_queue_dequeue(struct bfd_control_socket *bcs)
{
	struct bfd_control_queue *bcq;

	/* List is empty, nothing to do. */
	if (TAILQ_EMPTY(&bcs->bcs_bcqueue))
		goto empty_list;

	bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
	control_queue_free(bcs, bcq);

	/* Get the next buffer to send. */
	if (TAILQ_EMPTY(&bcs->bcs_bcqueue))
		goto empty_list;

	bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
	bcs->bcs_bout = &bcq->bcq_bcb;

	bcs->bcs_outev = NULL;
	event_add_write(master, control_write, bcs, bcs->bcs_sd,
			&bcs->bcs_outev);

	return 1;

empty_list:
	event_cancel(&(bcs->bcs_outev));
	bcs->bcs_bout = NULL;
	return 0;
}

static int control_queue_enqueue(struct bfd_control_socket *bcs,
				 struct bfd_control_msg *bcm)
{
	struct bfd_control_queue *bcq;
	struct bfd_control_buffer *bcb;

	bcq = control_queue_new(bcs);

	bcb = &bcq->bcq_bcb;
	bcb->bcb_left = sizeof(struct bfd_control_msg) + ntohl(bcm->bcm_length);
	bcb->bcb_pos = 0;
	bcb->bcb_bcm = bcm;

	/* If this is the first item, then dequeue and start using it. */
	if (bcs->bcs_bout == NULL) {
		bcs->bcs_bout = bcb;

		/* New messages, active write events. */
		event_add_write(master, control_write, bcs, bcs->bcs_sd,
				&bcs->bcs_outev);
	}

	return 0;
}

static int control_queue_enqueue_first(struct bfd_control_socket *bcs,
				       struct bfd_control_msg *bcm)
{
	struct bfd_control_queue *bcq, *bcqn;
	struct bfd_control_buffer *bcb;

	/* Enqueue it somewhere. */
	if (control_queue_enqueue(bcs, bcm) == -1)
		return -1;

	/*
	 * The item is either the first or the last. So we must first
	 * check the best case where the item is already the first.
	 */
	bcq = TAILQ_FIRST(&bcs->bcs_bcqueue);
	bcb = &bcq->bcq_bcb;
	if (bcm == bcb->bcb_bcm)
		return 0;

	/*
	 * The item was not the first, so it is the last. We'll try to
	 * assign it to the head of the queue, however if there is a
	 * transfer in progress, then we have to make the item as the
	 * next one.
	 *
	 * Interrupting the transfer of in progress message will cause
	 * the client to lose track of the message position/data.
	 */
	bcqn = TAILQ_LAST(&bcs->bcs_bcqueue, bcqueue);
	TAILQ_REMOVE(&bcs->bcs_bcqueue, bcqn, bcq_entry);
	if (bcb->bcb_pos != 0) {
		/*
		 * First position is already being sent, insert into
		 * second position.
		 */
		TAILQ_INSERT_AFTER(&bcs->bcs_bcqueue, bcq, bcqn, bcq_entry);
	} else {
		/*
		 * Old message didn't start being sent, we still have
		 * time to put this one in the head of the queue.
		 */
		TAILQ_INSERT_HEAD(&bcs->bcs_bcqueue, bcqn, bcq_entry);
		bcb = &bcqn->bcq_bcb;
		bcs->bcs_bout = bcb;
	}

	return 0;
}

static void control_reset_buf(struct bfd_control_buffer *bcb)
{
	/* Get ride of old data. */
	XFREE(MTYPE_BFDD_NOTIFICATION, bcb->bcb_buf);
	bcb->bcb_pos = 0;
	bcb->bcb_left = 0;
}

static void control_read(struct event *t)
{
	struct bfd_control_socket *bcs = EVENT_ARG(t);
	struct bfd_control_buffer *bcb = &bcs->bcs_bin;
	int sd = bcs->bcs_sd;
	struct bfd_control_msg bcm;
	ssize_t bread;
	size_t plen;

	/*
	 * Check if we have already downloaded message content, if so then skip
	 * to
	 * download the rest of it and process.
	 *
	 * Otherwise download a new message header and allocate the necessary
	 * memory.
	 */
	if (bcb->bcb_buf != NULL)
		goto skip_header;

	bread = read(sd, &bcm, sizeof(bcm));
	if (bread == 0) {
		control_free(bcs);
		return;
	}
	if (bread < 0) {
		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
			goto schedule_next_read;

		zlog_warn("%s: read: %s", __func__, strerror(errno));
		control_free(bcs);
		return;
	}

	/* Validate header fields. */
	plen = ntohl(bcm.bcm_length);
	if (plen < 2) {
		zlog_debug("%s: client closed due small message length: %d",
			   __func__, bcm.bcm_length);
		control_free(bcs);
		return;
	}

#define FRR_BFD_MAXLEN 10 * 1024

	if (plen > FRR_BFD_MAXLEN) {
		zlog_debug("%s: client closed, invalid message length: %d",
			   __func__, bcm.bcm_length);
		control_free(bcs);
		return;
	}

	if (bcm.bcm_ver != BMV_VERSION_1) {
		zlog_debug("%s: client closed due bad version: %d", __func__,
			   bcm.bcm_ver);
		control_free(bcs);
		return;
	}

	/* Prepare the buffer to load the message. */
	bcs->bcs_version = bcm.bcm_ver;
	bcs->bcs_type = bcm.bcm_type;

	bcb->bcb_pos = sizeof(bcm);
	bcb->bcb_left = plen;
	bcb->bcb_buf = XMALLOC(MTYPE_BFDD_NOTIFICATION,
			       sizeof(bcm) + bcb->bcb_left + 1);
	if (bcb->bcb_buf == NULL) {
		zlog_warn("%s: not enough memory for message size: %zu",
			  __func__, bcb->bcb_left);
		control_free(bcs);
		return;
	}

	memcpy(bcb->bcb_buf, &bcm, sizeof(bcm));

	/* Terminate data string with NULL for later processing. */
	bcb->bcb_buf[sizeof(bcm) + bcb->bcb_left] = 0;

skip_header:
	/* Download the remaining data of the message and process it. */
	bread = read(sd, &bcb->bcb_buf[bcb->bcb_pos], bcb->bcb_left);
	if (bread == 0) {
		control_free(bcs);
		return;
	}
	if (bread < 0) {
		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
			goto schedule_next_read;

		zlog_warn("%s: read: %s", __func__, strerror(errno));
		control_free(bcs);
		return;
	}

	bcb->bcb_pos += bread;
	bcb->bcb_left -= bread;
	/* We need more data, return to wait more. */
	if (bcb->bcb_left > 0)
		goto schedule_next_read;

	switch (bcb->bcb_bcm->bcm_type) {
	case BMT_REQUEST_ADD:
		control_handle_request_add(bcs, bcb->bcb_bcm);
		break;
	case BMT_REQUEST_DEL:
		control_handle_request_del(bcs, bcb->bcb_bcm);
		break;
	case BMT_NOTIFY:
		control_handle_notify(bcs, bcb->bcb_bcm);
		break;
	case BMT_NOTIFY_ADD:
		control_handle_notify_add(bcs, bcb->bcb_bcm);
		break;
	case BMT_NOTIFY_DEL:
		control_handle_notify_del(bcs, bcb->bcb_bcm);
		break;

	default:
		zlog_debug("%s: unhandled message type: %d", __func__,
			   bcb->bcb_bcm->bcm_type);
		control_response(bcs, bcb->bcb_bcm->bcm_id, BCM_RESPONSE_ERROR,
				 "invalid message type");
		break;
	}

	bcs->bcs_version = 0;
	bcs->bcs_type = 0;
	control_reset_buf(bcb);

schedule_next_read:
	bcs->bcs_ev = NULL;
	event_add_read(master, control_read, bcs, sd, &bcs->bcs_ev);
}

static void control_write(struct event *t)
{
	struct bfd_control_socket *bcs = EVENT_ARG(t);
	struct bfd_control_buffer *bcb = bcs->bcs_bout;
	int sd = bcs->bcs_sd;
	ssize_t bwrite;

	bwrite = write(sd, &bcb->bcb_buf[bcb->bcb_pos], bcb->bcb_left);
	if (bwrite == 0) {
		control_free(bcs);
		return;
	}
	if (bwrite < 0) {
		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
			bcs->bcs_outev = NULL;
			event_add_write(master, control_write, bcs, bcs->bcs_sd,
					&bcs->bcs_outev);
			return;
		}

		zlog_warn("%s: write: %s", __func__, strerror(errno));
		control_free(bcs);
		return;
	}

	bcb->bcb_pos += bwrite;
	bcb->bcb_left -= bwrite;
	if (bcb->bcb_left > 0) {
		bcs->bcs_outev = NULL;
		event_add_write(master, control_write, bcs, bcs->bcs_sd,
				&bcs->bcs_outev);
		return;
	}

	control_queue_dequeue(bcs);
}


/*
 * Message processing
 */
static void control_handle_request_add(struct bfd_control_socket *bcs,
				       struct bfd_control_msg *bcm)
{
	const char *json = (const char *)bcm->bcm_data;

	if (config_request_add(json) == 0)
		control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
	else
		control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
				 "request add failed");
}

static void control_handle_request_del(struct bfd_control_socket *bcs,
				       struct bfd_control_msg *bcm)
{
	const char *json = (const char *)bcm->bcm_data;

	if (config_request_del(json) == 0)
		control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
	else
		control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
				 "request del failed");
}

static struct bfd_session *_notify_find_peer(struct bfd_peer_cfg *bpc)
{
	struct peer_label *pl;

	if (bpc->bpc_has_label) {
		pl = pl_find(bpc->bpc_label);
		if (pl)
			return pl->pl_bs;
	}

	return bs_peer_find(bpc);
}

static void _control_handle_notify(struct hash_bucket *hb, void *arg)
{
	struct bfd_control_socket *bcs = arg;
	struct bfd_session *bs = hb->data;

	/* Notify peer configuration. */
	if (bcs->bcs_notify & BCM_NOTIFY_CONFIG)
		_control_notify_config(bcs, BCM_NOTIFY_CONFIG_ADD, bs);

	/* Notify peer status. */
	if (bcs->bcs_notify & BCM_NOTIFY_PEER_STATE)
		_control_notify(bcs, bs);
}

static void control_handle_notify(struct bfd_control_socket *bcs,
				  struct bfd_control_msg *bcm)
{
	memcpy(&bcs->bcs_notify, bcm->bcm_data, sizeof(bcs->bcs_notify));

	control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);

	/*
	 * If peer asked for notification configuration, send everything that
	 * was configured until the moment to sync up.
	 */
	if (bcs->bcs_notify & (BCM_NOTIFY_CONFIG | BCM_NOTIFY_PEER_STATE))
		bfd_id_iterate(_control_handle_notify, bcs);
}

static int notify_add_cb(struct bfd_peer_cfg *bpc, void *arg)
{
	struct bfd_control_socket *bcs = arg;
	struct bfd_session *bs = _notify_find_peer(bpc);

	if (bs == NULL)
		return -1;

	control_notifypeer_new(bcs, bs);

	/* Notify peer status. */
	_control_notify(bcs, bs);

	return 0;
}

static int notify_del_cb(struct bfd_peer_cfg *bpc, void *arg)
{
	struct bfd_control_socket *bcs = arg;
	struct bfd_session *bs = _notify_find_peer(bpc);
	struct bfd_notify_peer *bnp;

	if (bs == NULL)
		return -1;

	bnp = control_notifypeer_find(bcs, bs);
	if (bnp)
		control_notifypeer_free(bcs, bnp);

	return 0;
}

static void control_handle_notify_add(struct bfd_control_socket *bcs,
				      struct bfd_control_msg *bcm)
{
	const char *json = (const char *)bcm->bcm_data;

	if (config_notify_request(bcs, json, notify_add_cb) == 0) {
		control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
		return;
	}

	control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
			 "failed to parse notify data");
}

static void control_handle_notify_del(struct bfd_control_socket *bcs,
				      struct bfd_control_msg *bcm)
{
	const char *json = (const char *)bcm->bcm_data;

	if (config_notify_request(bcs, json, notify_del_cb) == 0) {
		control_response(bcs, bcm->bcm_id, BCM_RESPONSE_OK, NULL);
		return;
	}

	control_response(bcs, bcm->bcm_id, BCM_RESPONSE_ERROR,
			 "failed to parse notify data");
}


/*
 * Internal functions used by the BFD daemon.
 */
static void control_response(struct bfd_control_socket *bcs, uint16_t id,
			     const char *status, const char *error)
{
	struct bfd_control_msg *bcm;
	char *jsonstr;
	size_t jsonstrlen;

	/* Generate JSON response. */
	jsonstr = config_response(status, error);
	if (jsonstr == NULL) {
		zlog_warn("%s: config_response: failed to get JSON str",
			  __func__);
		return;
	}

	/* Allocate data and answer. */
	jsonstrlen = strlen(jsonstr);
	bcm = XMALLOC(MTYPE_BFDD_NOTIFICATION,
		      sizeof(struct bfd_control_msg) + jsonstrlen);

	bcm->bcm_length = htonl(jsonstrlen);
	bcm->bcm_ver = BMV_VERSION_1;
	bcm->bcm_type = BMT_RESPONSE;
	bcm->bcm_id = id;
	memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
	XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);

	control_queue_enqueue_first(bcs, bcm);
}

static void _control_notify(struct bfd_control_socket *bcs,
			    struct bfd_session *bs)
{
	struct bfd_control_msg *bcm;
	char *jsonstr;
	size_t jsonstrlen;

	/* Generate JSON response. */
	jsonstr = config_notify(bs);
	if (jsonstr == NULL) {
		zlog_warn("%s: config_notify: failed to get JSON str",
			  __func__);
		return;
	}

	/* Allocate data and answer. */
	jsonstrlen = strlen(jsonstr);
	bcm = XMALLOC(MTYPE_BFDD_NOTIFICATION,
		      sizeof(struct bfd_control_msg) + jsonstrlen);

	bcm->bcm_length = htonl(jsonstrlen);
	bcm->bcm_ver = BMV_VERSION_1;
	bcm->bcm_type = BMT_NOTIFY;
	bcm->bcm_id = htons(BCM_NOTIFY_ID);
	memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
	XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);

	control_queue_enqueue(bcs, bcm);
}

int control_notify(struct bfd_session *bs, uint8_t notify_state)
{
	struct bfd_control_socket *bcs;
	struct bfd_notify_peer *bnp;

	/* Notify zebra listeners as well. */
	ptm_bfd_notify(bs, notify_state);

	/*
	 * PERFORMANCE: reuse the bfd_control_msg allocated data for
	 * all control sockets to avoid wasting memory.
	 */
	TAILQ_FOREACH (bcs, &bglobal.bg_bcslist, bcs_entry) {
		/*
		 * Test for all notifications first, then search for
		 * specific peers.
		 */
		if ((bcs->bcs_notify & BCM_NOTIFY_PEER_STATE) == 0) {
			bnp = control_notifypeer_find(bcs, bs);
			/*
			 * If the notification is not configured here,
			 * don't send it.
			 */
			if (bnp == NULL)
				continue;
		}

		_control_notify(bcs, bs);
	}

	return 0;
}

static void _control_notify_config(struct bfd_control_socket *bcs,
				   const char *op, struct bfd_session *bs)
{
	struct bfd_control_msg *bcm;
	char *jsonstr;
	size_t jsonstrlen;

	/* Generate JSON response. */
	jsonstr = config_notify_config(op, bs);
	if (jsonstr == NULL) {
		zlog_warn("%s: config_notify_config: failed to get JSON str",
			  __func__);
		return;
	}

	/* Allocate data and answer. */
	jsonstrlen = strlen(jsonstr);
	bcm = XMALLOC(MTYPE_BFDD_NOTIFICATION,
		      sizeof(struct bfd_control_msg) + jsonstrlen);

	bcm->bcm_length = htonl(jsonstrlen);
	bcm->bcm_ver = BMV_VERSION_1;
	bcm->bcm_type = BMT_NOTIFY;
	bcm->bcm_id = htons(BCM_NOTIFY_ID);
	memcpy(bcm->bcm_data, jsonstr, jsonstrlen);
	XFREE(MTYPE_BFDD_NOTIFICATION, jsonstr);

	control_queue_enqueue(bcs, bcm);
}

int control_notify_config(const char *op, struct bfd_session *bs)
{
	struct bfd_control_socket *bcs;
	struct bfd_notify_peer *bnp;

	/* Remove the control sockets notification for this peer. */
	if (strcmp(op, BCM_NOTIFY_CONFIG_DELETE) == 0 && bs->refcount > 0) {
		TAILQ_FOREACH (bcs, &bglobal.bg_bcslist, bcs_entry) {
			bnp = control_notifypeer_find(bcs, bs);
			if (bnp)
				control_notifypeer_free(bcs, bnp);
		}
	}

	/*
	 * PERFORMANCE: reuse the bfd_control_msg allocated data for
	 * all control sockets to avoid wasting memory.
	 */
	TAILQ_FOREACH (bcs, &bglobal.bg_bcslist, bcs_entry) {
		/*
		 * Test for all notifications first, then search for
		 * specific peers.
		 */
		if ((bcs->bcs_notify & BCM_NOTIFY_CONFIG) == 0)
			continue;

		_control_notify_config(bcs, op, bs);
	}

	return 0;
}