summaryrefslogtreecommitdiffstats
path: root/ldpd/ldp_zebra.c
blob: 0fd5d4613c0825388fd9894059715af34097d361 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2016 by Open Source Routing.
 */

#include <zebra.h>

#include "prefix.h"
#include "stream.h"
#include "memory.h"
#include "zclient.h"
#include "command.h"
#include "network.h"
#include "linklist.h"
#include "mpls.h"

#include "ldpd.h"
#include "ldpe.h"
#include "lde.h"
#include "ldp_sync.h"
#include "log.h"
#include "ldp_debug.h"

static void	 ifp2kif(struct interface *, struct kif *);
static void	 ifc2kaddr(struct interface *, struct connected *, struct kaddr *);
static int	 ldp_zebra_send_mpls_labels(int, struct kroute *);
static int	 ldp_router_id_update(ZAPI_CALLBACK_ARGS);
static int	 ldp_interface_address_add(ZAPI_CALLBACK_ARGS);
static int	 ldp_interface_address_delete(ZAPI_CALLBACK_ARGS);
static int	 ldp_zebra_read_route(ZAPI_CALLBACK_ARGS);
static int	 ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS);
static void	 ldp_zebra_connected(struct zclient *);
static void	 ldp_zebra_filter_update(struct access_list *access);

static void 	ldp_zebra_opaque_register(void);
static void 	ldp_zebra_opaque_unregister(void);
static int 	ldp_sync_zebra_send_announce(void);
static int 	ldp_zebra_opaque_msg_handler(ZAPI_CALLBACK_ARGS);
static void 	ldp_sync_zebra_init(void);

static struct zclient	*zclient;
extern struct zclient *zclient_sync;
static bool zebra_registered = false;

static void
ifp2kif(struct interface *ifp, struct kif *kif)
{
	memset(kif, 0, sizeof(*kif));
	strlcpy(kif->ifname, ifp->name, sizeof(kif->ifname));
	kif->ifindex = ifp->ifindex;
	kif->operative = if_is_operative(ifp);
	if (ifp->ll_type == ZEBRA_LLT_ETHER)
		memcpy(kif->mac, ifp->hw_addr, ETH_ALEN);
}

static void
ifc2kaddr(struct interface *ifp, struct connected *ifc, struct kaddr *ka)
{
	memset(ka, 0, sizeof(*ka));
	strlcpy(ka->ifname, ifp->name, sizeof(ka->ifname));
	ka->ifindex = ifp->ifindex;
	ka->af = ifc->address->family;
	ka->prefixlen = ifc->address->prefixlen;

	switch (ka->af) {
	case AF_INET:
		ka->addr.v4 = ifc->address->u.prefix4;
		if (ifc->destination)
			ka->dstbrd.v4 = ifc->destination->u.prefix4;
		break;
	case AF_INET6:
		ka->addr.v6 = ifc->address->u.prefix6;
		if (ifc->destination)
			ka->dstbrd.v6 = ifc->destination->u.prefix6;
		break;
	default:
		break;
	}
}

void
pw2zpw(struct l2vpn_pw *pw, struct zapi_pw *zpw)
{
	memset(zpw, 0, sizeof(*zpw));
	strlcpy(zpw->ifname, pw->ifname, sizeof(zpw->ifname));
	zpw->ifindex = pw->ifindex;
	zpw->type = pw->l2vpn->pw_type;
	zpw->af = pw->af;
	zpw->nexthop.ipv6 = pw->addr.v6;
	zpw->local_label = NO_LABEL;
	zpw->remote_label = NO_LABEL;
	if (CHECK_FLAG(pw->flags, F_PW_CWORD))
		zpw->flags = F_PSEUDOWIRE_CWORD;
	zpw->data.ldp.lsr_id = pw->lsr_id;
	zpw->data.ldp.pwid = pw->pwid;
	strlcpy(zpw->data.ldp.vpn_name, pw->l2vpn->name,
	    sizeof(zpw->data.ldp.vpn_name));
}

static void
ldp_zebra_opaque_register(void)
{
	zclient_register_opaque(zclient, LDP_IGP_SYNC_IF_STATE_REQUEST);
	zclient_register_opaque(zclient, LDP_RLFA_REGISTER);
	zclient_register_opaque(zclient, LDP_RLFA_UNREGISTER_ALL);
}

static void
ldp_zebra_opaque_unregister(void)
{
	zclient_unregister_opaque(zclient, LDP_IGP_SYNC_IF_STATE_REQUEST);
	zclient_unregister_opaque(zclient, LDP_RLFA_REGISTER);
	zclient_unregister_opaque(zclient, LDP_RLFA_UNREGISTER_ALL);
}

int
ldp_sync_zebra_send_state_update(struct ldp_igp_sync_if_state *state)
{
	if (zclient_send_opaque(zclient, LDP_IGP_SYNC_IF_STATE_UPDATE,
				(const uint8_t *)state, sizeof(*state))
	    == ZCLIENT_SEND_FAILURE)
		return -1;
	else
		return 0;
}

static int
ldp_sync_zebra_send_announce(void)
{
	struct ldp_igp_sync_announce announce;
	announce.proto = ZEBRA_ROUTE_LDP;

	if (zclient_send_opaque(zclient, LDP_IGP_SYNC_ANNOUNCE_UPDATE,
				(const uint8_t *)&announce, sizeof(announce))
	    == ZCLIENT_SEND_FAILURE)
		return -1;
	else
		return 0;
}

int ldp_zebra_send_rlfa_labels(struct zapi_rlfa_response *rlfa_labels)
{
	int ret;

	ret = zclient_send_opaque(zclient, LDP_RLFA_LABELS,
				  (const uint8_t *)rlfa_labels,
				  sizeof(*rlfa_labels));
	if (ret == ZCLIENT_SEND_FAILURE) {
		log_warn("failed to send RLFA labels to IGP");
		return -1;
	}

	return 0;
}

static int
ldp_zebra_opaque_msg_handler(ZAPI_CALLBACK_ARGS)
{
	struct stream *s;
	struct zapi_opaque_msg info;
	struct ldp_igp_sync_if_state_req state_req;
	struct zapi_rlfa_igp igp;
	struct zapi_rlfa_request rlfa;

	s = zclient->ibuf;

	if(zclient_opaque_decode(s, &info) != 0)
		return -1;

	switch (info.type) {
	case LDP_IGP_SYNC_IF_STATE_REQUEST:
		STREAM_GET(&state_req, s, sizeof(state_req));
		main_imsg_compose_ldpe(IMSG_LDP_SYNC_IF_STATE_REQUEST, 0, &state_req,
			    sizeof(state_req));
		break;
	case LDP_RLFA_REGISTER:
		STREAM_GET(&rlfa, s, sizeof(rlfa));
		main_imsg_compose_both(IMSG_RLFA_REG, &rlfa, sizeof(rlfa));
		break;
	case LDP_RLFA_UNREGISTER_ALL:
		STREAM_GET(&igp, s, sizeof(igp));
		main_imsg_compose_both(IMSG_RLFA_UNREG_ALL, &igp, sizeof(igp));
		break;
	default:
		break;
	}

stream_failure:
        return 0;
}

static void
ldp_sync_zebra_init(void)
{
	ldp_sync_zebra_send_announce();
}

static int
ldp_zebra_send_mpls_labels(int cmd, struct kroute *kr)
{
	struct zapi_labels zl = {};
	struct zapi_nexthop *znh;

	if (kr->local_label < MPLS_LABEL_RESERVED_MAX)
		return (0);

	debug_zebra_out("prefix %s/%u nexthop %s ifindex %u labels %s/%s (%s)",
	    log_addr(kr->af, &kr->prefix), kr->prefixlen,
	    log_addr(kr->af, &kr->nexthop), kr->ifindex,
	    log_label(kr->local_label), log_label(kr->remote_label),
	    (cmd == ZEBRA_MPLS_LABELS_ADD) ? "add" : "delete");

	zl.type = ZEBRA_LSP_LDP;
	zl.local_label = kr->local_label;

	/* Set prefix. */
	if (kr->remote_label != NO_LABEL) {
		SET_FLAG(zl.message, ZAPI_LABELS_FTN);
		zl.route.prefix.family = kr->af;
		switch (kr->af) {
		case AF_INET:
			zl.route.prefix.u.prefix4 = kr->prefix.v4;
			break;
		case AF_INET6:
			zl.route.prefix.u.prefix6 = kr->prefix.v6;
			break;
		default:
			fatalx("ldp_zebra_send_mpls_labels: unknown af");
		}
		zl.route.prefix.prefixlen = kr->prefixlen;
		zl.route.type = kr->route_type;
		zl.route.instance = kr->route_instance;
	}

	/* If allow-broken-lsps is enabled then if an lsp is received with
	 * no remote label, instruct the forwarding plane to pop the top-level
	 * label and forward packets normally. This is a best-effort attempt
	 * to deliver labeled IP packets to their final destination (instead of
	 * dropping them).
	 */
	if (kr->remote_label == NO_LABEL
	    && !CHECK_FLAG(ldpd_conf->flags, F_LDPD_ALLOW_BROKEN_LSP)
	    && cmd == ZEBRA_MPLS_LABELS_ADD)
		return 0;

	if (kr->remote_label == NO_LABEL)
		kr->remote_label = MPLS_LABEL_IMPLICIT_NULL;

	/* Set nexthop. */
	zl.nexthop_num = 1;
	znh = &zl.nexthops[0];
	switch (kr->af) {
	case AF_INET:
		znh->gate.ipv4 = kr->nexthop.v4;
		if (kr->ifindex)
			znh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
		else
			znh->type = NEXTHOP_TYPE_IPV4;
		break;
	case AF_INET6:
		znh->gate.ipv6 = kr->nexthop.v6;
		if (kr->ifindex)
			znh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
		else
			znh->type = NEXTHOP_TYPE_IPV6;
		break;
	default:
		break;
	}
	znh->ifindex = kr->ifindex;
	znh->label_num = 1;
	znh->labels[0] = kr->remote_label;

	if (zebra_send_mpls_labels(zclient, cmd, &zl) == ZCLIENT_SEND_FAILURE)
		return -1;

	return 0;
}

int
kr_change(struct kroute *kr)
{
	return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_ADD, kr));
}

int
kr_delete(struct kroute *kr)
{
	return (ldp_zebra_send_mpls_labels(ZEBRA_MPLS_LABELS_DELETE, kr));
}

int
kmpw_add(struct zapi_pw *zpw)
{
	debug_zebra_out("pseudowire %s nexthop %s (add)",
	    zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));

	return zebra_send_pw(zclient, ZEBRA_PW_ADD, zpw) == ZCLIENT_SEND_FAILURE;
}

int
kmpw_del(struct zapi_pw *zpw)
{
	debug_zebra_out("pseudowire %s nexthop %s (del)",
	    zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));

	return zebra_send_pw(zclient, ZEBRA_PW_DELETE, zpw) == ZCLIENT_SEND_FAILURE;
}

int
kmpw_set(struct zapi_pw *zpw)
{
	debug_zebra_out("pseudowire %s nexthop %s labels %u/%u (set)",
	    zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop),
	    zpw->local_label, zpw->remote_label);

	return zebra_send_pw(zclient, ZEBRA_PW_SET, zpw) == ZCLIENT_SEND_FAILURE;
}

int
kmpw_unset(struct zapi_pw *zpw)
{
	debug_zebra_out("pseudowire %s nexthop %s (unset)",
	    zpw->ifname, log_addr(zpw->af, (union ldpd_addr *)&zpw->nexthop));

	return zebra_send_pw(zclient, ZEBRA_PW_UNSET, zpw) == ZCLIENT_SEND_FAILURE;
}

void
kif_redistribute(const char *ifname)
{
	struct vrf		*vrf = vrf_lookup_by_id(VRF_DEFAULT);
	struct listnode		*cnode;
	struct interface	*ifp;
	struct connected	*ifc;
	struct kif		 kif;
	struct kaddr		 ka;

	FOR_ALL_INTERFACES (vrf, ifp) {
		if (ifname && strcmp(ifname, ifp->name) != 0)
			continue;

		ifp2kif(ifp, &kif);
		main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));

		for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, ifc)) {
			ifc2kaddr(ifp, ifc, &ka);
			main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));
		}
	}
}

static int
ldp_router_id_update(ZAPI_CALLBACK_ARGS)
{
	struct prefix	 router_id;

	zebra_router_id_update_read(zclient->ibuf, &router_id);

	if (bad_addr_v4(router_id.u.prefix4))
		return (0);

	debug_zebra_in("router-id update %pI4", &router_id.u.prefix4);

	global.rtr_id.s_addr = router_id.u.prefix4.s_addr;
	main_imsg_compose_ldpe(IMSG_RTRID_UPDATE, 0, &global.rtr_id,
	    sizeof(global.rtr_id));

	return (0);
}

static int
ldp_ifp_create(struct interface *ifp)
{
	struct kif		 kif;

	debug_zebra_in("interface add %s index %d mtu %d", ifp->name,
	    ifp->ifindex, ifp->mtu);

	ifp2kif(ifp, &kif);
	main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));

	return 0;
}

static int
ldp_ifp_destroy(struct interface *ifp)
{
	struct kif		 kif;

	debug_zebra_in("interface delete %s index %d mtu %d", ifp->name,
	    ifp->ifindex, ifp->mtu);

	ifp2kif(ifp, &kif);
	main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));

	return (0);
}

static int
ldp_interface_status_change(struct interface *ifp)
{
	struct listnode		*node;
	struct connected	*ifc;
	struct kif		 kif;
	struct kaddr		 ka;

	debug_zebra_in("interface %s state update", ifp->name);

	ifp2kif(ifp, &kif);
	main_imsg_compose_both(IMSG_IFSTATUS, &kif, sizeof(kif));

	if (if_is_operative(ifp)) {
		for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
			ifc2kaddr(ifp, ifc, &ka);
			main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));
		}
	} else {
		for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
			ifc2kaddr(ifp, ifc, &ka);
			main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka));
		}
	}

	return (0);
}

static int ldp_ifp_up(struct interface *ifp)
{
	return ldp_interface_status_change(ifp);
}

static int ldp_ifp_down(struct interface *ifp)
{
	return ldp_interface_status_change(ifp);
}

static int
ldp_interface_address_add(ZAPI_CALLBACK_ARGS)
{
	struct connected	*ifc;
	struct interface	*ifp;
	struct kaddr		 ka;

	ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
	if (ifc == NULL)
		return (0);

	ifp = ifc->ifp;
	ifc2kaddr(ifp, ifc, &ka);

	/* Filter invalid addresses.  */
	if (bad_addr(ka.af, &ka.addr))
		return (0);

	debug_zebra_in("address add %s/%u interface %s",
	    log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);

	/* notify ldpe about new address */
	main_imsg_compose_ldpe(IMSG_NEWADDR, 0, &ka, sizeof(ka));

	return (0);
}

static int
ldp_interface_address_delete(ZAPI_CALLBACK_ARGS)
{
	struct connected	*ifc;
	struct interface	*ifp;
	struct kaddr		 ka;

	ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
	if (ifc == NULL)
		return (0);

	ifp = ifc->ifp;
	ifc2kaddr(ifp, ifc, &ka);
	connected_free(&ifc);

	/* Filter invalid addresses.  */
	if (bad_addr(ka.af, &ka.addr))
		return (0);

	debug_zebra_in("address delete %s/%u interface %s",
	    log_addr(ka.af, &ka.addr), ka.prefixlen, ifp->name);

	/* notify ldpe about removed address */
	main_imsg_compose_ldpe(IMSG_DELADDR, 0, &ka, sizeof(ka));

	return (0);
}

static int
ldp_zebra_read_route(ZAPI_CALLBACK_ARGS)
{
	struct zapi_route	 api;
	struct zapi_nexthop	*api_nh;
	struct kroute		 kr;
	int			 i, add = 0;

	if (zapi_route_decode(zclient->ibuf, &api) < 0)
		return -1;

	/* we completely ignore srcdest routes for now. */
	if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
		return (0);

	memset(&kr, 0, sizeof(kr));
	kr.af = api.prefix.family;
	switch (kr.af) {
	case AF_INET:
		kr.prefix.v4 = api.prefix.u.prefix4;
		break;
	case AF_INET6:
		kr.prefix.v6 = api.prefix.u.prefix6;
		break;
	default:
		break;
	}
	kr.prefixlen = api.prefix.prefixlen;
	kr.route_type = api.type;
	kr.route_instance = api.instance;

	switch (api.type) {
	case ZEBRA_ROUTE_CONNECT:
		SET_FLAG(kr.flags, F_CONNECTED);
		break;
	case ZEBRA_ROUTE_BGP:
		/* LDP should follow the IGP and ignore BGP routes */
		return (0);
	default:
		break;
	}

	if (bad_addr(kr.af, &kr.prefix) ||
	    (kr.af == AF_INET6 && IN6_IS_SCOPE_EMBED(&kr.prefix.v6)))
		return (0);

	if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
		add = 1;

	if (api.nexthop_num == 0)
		debug_zebra_in("route %s %s/%d (%s)", (add) ? "add" : "delete",
		    log_addr(kr.af, &kr.prefix), kr.prefixlen,
		    zebra_route_string(api.type));

	/* loop through all the nexthops */
	for (i = 0; i < api.nexthop_num; i++) {
		api_nh = &api.nexthops[i];
		switch (api_nh->type) {
		case NEXTHOP_TYPE_IPV4:
			if (kr.af != AF_INET)
				continue;
			kr.nexthop.v4 = api_nh->gate.ipv4;
			kr.ifindex = 0;
			break;
		case NEXTHOP_TYPE_IPV4_IFINDEX:
			if (kr.af != AF_INET)
				continue;
			kr.nexthop.v4 = api_nh->gate.ipv4;
			kr.ifindex = api_nh->ifindex;
			break;
		case NEXTHOP_TYPE_IPV6:
			if (kr.af != AF_INET6)
				continue;
			kr.nexthop.v6 = api_nh->gate.ipv6;
			kr.ifindex = 0;
			break;
		case NEXTHOP_TYPE_IPV6_IFINDEX:
			if (kr.af != AF_INET6)
				continue;
			kr.nexthop.v6 = api_nh->gate.ipv6;
			kr.ifindex = api_nh->ifindex;
			break;
		case NEXTHOP_TYPE_IFINDEX:
			if (!CHECK_FLAG(kr.flags, F_CONNECTED))
				continue;
			break;
		case NEXTHOP_TYPE_BLACKHOLE:
			continue;
		}

		debug_zebra_in("route %s %s/%d nexthop %s ifindex %u (%s)",
		    (add) ? "add" : "delete", log_addr(kr.af, &kr.prefix),
		    kr.prefixlen, log_addr(kr.af, &kr.nexthop), kr.ifindex,
		    zebra_route_string(api.type));

		if (add)
			main_imsg_compose_lde(IMSG_NETWORK_ADD, 0, &kr, sizeof(kr));
	}

	main_imsg_compose_lde(IMSG_NETWORK_UPDATE, 0, &kr, sizeof(kr));

	return (0);
}

/*
 * Receive PW status update from Zebra and send it to LDE process.
 */
static int
ldp_zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS)
{
	struct zapi_pw_status	 zpw;

	zebra_read_pw_status_update(cmd, zclient, length, vrf_id, &zpw);

	debug_zebra_in("pseudowire %s status %s 0x%x", zpw.ifname,
	    (zpw.status == PW_FORWARDING) ? "up" : "down",
	    zpw.status);

	main_imsg_compose_lde(IMSG_PW_UPDATE, 0, &zpw, sizeof(zpw));

	return (0);
}

void ldp_zebra_regdereg_zebra_info(bool want_register)
{
	if (zebra_registered == want_register)
		return;

	log_debug("%s to receive default VRF information",
		  want_register ? "Register" : "De-register");

	if (want_register) {
		zclient_send_reg_requests(zclient, VRF_DEFAULT);
		zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
					ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
		zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient,
					AFI_IP6, ZEBRA_ROUTE_ALL, 0,
					VRF_DEFAULT);
	} else {
		zclient_send_dereg_requests(zclient, VRF_DEFAULT);
		zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
					AFI_IP, ZEBRA_ROUTE_ALL, 0,
					VRF_DEFAULT);
		zebra_redistribute_send(ZEBRA_REDISTRIBUTE_DELETE, zclient,
					AFI_IP6, ZEBRA_ROUTE_ALL, 0,
					VRF_DEFAULT);
	}
	zebra_registered = want_register;
}

static void
ldp_zebra_connected(struct zclient *zclient)
{
	zebra_registered = false;

	/* if MPLS was already enabled and we are re-connecting, register again
	 */
	if (CHECK_FLAG(vty_conf->flags, F_LDPD_ENABLED))
		ldp_zebra_regdereg_zebra_info(true);

	ldp_zebra_opaque_register();

	ldp_sync_zebra_init();
}

static void
ldp_zebra_filter_update(struct access_list *access)
{
	struct ldp_access laccess;

	if (access && access->name[0] != '\0') {
		strlcpy(laccess.name, access->name, sizeof(laccess.name));
		debug_evt("%s ACL update filter name %s", __func__, access->name);

		main_imsg_compose_both(IMSG_FILTER_UPDATE, &laccess, sizeof(laccess));
	}
}

extern struct zebra_privs_t ldpd_privs;

static zclient_handler *const ldp_handlers[] = {
	[ZEBRA_ROUTER_ID_UPDATE] = ldp_router_id_update,
	[ZEBRA_INTERFACE_ADDRESS_ADD] = ldp_interface_address_add,
	[ZEBRA_INTERFACE_ADDRESS_DELETE] = ldp_interface_address_delete,
	[ZEBRA_REDISTRIBUTE_ROUTE_ADD] = ldp_zebra_read_route,
	[ZEBRA_REDISTRIBUTE_ROUTE_DEL] = ldp_zebra_read_route,
	[ZEBRA_PW_STATUS_UPDATE] = ldp_zebra_read_pw_status_update,
	[ZEBRA_OPAQUE_MESSAGE] = ldp_zebra_opaque_msg_handler,
};

void ldp_zebra_init(struct event_loop *master)
{
	if_zapi_callbacks(ldp_ifp_create, ldp_ifp_up, ldp_ifp_down, ldp_ifp_destroy);

	/* Set default values. */
	zclient = zclient_new(master, &zclient_options_default, ldp_handlers,
			      array_size(ldp_handlers));
	zclient_init(zclient, ZEBRA_ROUTE_LDP, 0, &ldpd_privs);

	/* set callbacks */
	zclient->zebra_connected = ldp_zebra_connected;

	/* Access list initialize. */
	access_list_add_hook(ldp_zebra_filter_update);
	access_list_delete_hook(ldp_zebra_filter_update);
}

void
ldp_zebra_destroy(void)
{
	ldp_zebra_opaque_unregister();
	zclient_stop(zclient);
	zclient_free(zclient);
	zclient = NULL;

	if (zclient_sync == NULL)
		return;
	zclient_stop(zclient_sync);
	zclient_free(zclient_sync);
	zclient_sync = NULL;
}