forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlde.c
2501 lines (2170 loc) · 60.9 KB
/
lde.c
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
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
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
// SPDX-License-Identifier: ISC
/* $OpenBSD$ */
/*
* Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
* Copyright (c) 2004, 2005 Claudio Jeker <claudio@openbsd.org>
* Copyright (c) 2004 Esben Norby <norby@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*/
#include <zebra.h>
#include "ldp.h"
#include "ldpd.h"
#include "ldpe.h"
#include "log.h"
#include "lde.h"
#include "ldp_debug.h"
#include "rlfa.h"
#include <lib/log.h>
#include "memory.h"
#include "privs.h"
#include "sigevent.h"
#include "mpls.h"
#include <lib/linklist.h>
#include "zclient.h"
#include "stream.h"
#include "network.h"
#include "libfrr.h"
#include "zlog_live.h"
static void lde_shutdown(void);
static void lde_dispatch_imsg(struct event *thread);
static void lde_dispatch_parent(struct event *thread);
static __inline int lde_nbr_compare(const struct lde_nbr *,
const struct lde_nbr *);
static struct lde_nbr *lde_nbr_new(uint32_t, struct lde_nbr *);
static void lde_nbr_del(struct lde_nbr *);
static struct lde_nbr *lde_nbr_find(uint32_t);
static void lde_nbr_clear(void);
static void lde_nbr_addr_update(struct lde_nbr *,
struct lde_addr *, int);
static __inline int lde_map_compare(const struct lde_map *,
const struct lde_map *);
static void lde_map_free(void *);
static int lde_address_add(struct lde_nbr *, struct lde_addr *);
static int lde_address_del(struct lde_nbr *, struct lde_addr *);
static void lde_address_list_free(struct lde_nbr *);
static void zclient_sync_init(void);
static void lde_label_list_init(void);
static int lde_get_label_chunk(void);
static void on_get_label_chunk_response(uint32_t start, uint32_t end);
static uint32_t lde_get_next_label(void);
static bool lde_fec_connected(const struct fec_node *);
static bool lde_fec_outside_mpls_network(const struct fec_node *);
static void lde_check_filter_af(int, struct ldpd_af_conf *,
const char *);
RB_GENERATE(nbr_tree, lde_nbr, entry, lde_nbr_compare)
RB_GENERATE(lde_map_head, lde_map, entry, lde_map_compare)
struct ldpd_conf *ldeconf;
struct nbr_tree lde_nbrs = RB_INITIALIZER(&lde_nbrs);
static struct imsgev *iev_ldpe;
static struct imsgev iev_main_sync_data;
static struct imsgev *iev_main, *iev_main_sync;
/* lde privileges */
static zebra_capabilities_t _caps_p [] =
{
ZCAP_NET_ADMIN
};
static struct zebra_privs_t lde_privs =
{
#if defined(VTY_GROUP)
.vty_group = VTY_GROUP,
#endif
.caps_p = _caps_p,
.cap_num_p = array_size(_caps_p),
.cap_num_i = 0
};
/* List of chunks of labels externally assigned by Zebra */
static struct list *label_chunk_list;
static struct listnode *current_label_chunk;
/* Synchronous zclient to request labels */
struct zclient *zclient_sync;
/* SIGINT / SIGTERM handler. */
static void
sigint(void)
{
lde_shutdown();
}
static struct frr_signal_t lde_signals[] =
{
{
.signal = SIGHUP,
/* ignore */
},
{
.signal = SIGINT,
.handler = &sigint,
},
{
.signal = SIGTERM,
.handler = &sigint,
},
};
/* label decision engine */
void
lde(void)
{
static struct zlog_live_cfg child_log;
#ifdef HAVE_SETPROCTITLE
setproctitle("label decision engine");
#endif
ldpd_process = PROC_LDE_ENGINE;
log_procname = log_procnames[PROC_LDE_ENGINE];
master = frr_init();
zlog_live_open_fd(&child_log, LOG_DEBUG, LDPD_FD_LOG);
/* no frr_config_fork() here, allow frr_pthread to create threads */
frr_is_after_fork = true;
/* setup signal handler */
signal_init(master, array_size(lde_signals), lde_signals);
/* setup pipes and event handlers to the parent process */
if ((iev_main = calloc(1, sizeof(struct imsgev))) == NULL)
fatal(NULL);
imsg_init(&iev_main->ibuf, LDPD_FD_ASYNC);
iev_main->handler_read = lde_dispatch_parent;
event_add_read(master, iev_main->handler_read, iev_main,
iev_main->ibuf.fd, &iev_main->ev_read);
iev_main->handler_write = ldp_write_handler;
memset(&iev_main_sync_data, 0, sizeof(iev_main_sync_data));
iev_main_sync = &iev_main_sync_data;
imsg_init(&iev_main_sync->ibuf, LDPD_FD_SYNC);
/* create base configuration */
ldeconf = config_new_empty();
struct event thread;
while (event_fetch(master, &thread))
event_call(&thread);
/* NOTREACHED */
return;
}
void
lde_init(struct ldpd_init *init)
{
/* drop privileges */
lde_privs.user = init->user;
lde_privs.group = init->group;
zprivs_preinit(&lde_privs);
zprivs_init(&lde_privs);
/* start the LIB garbage collector */
lde_gc_start_timer();
/* Init synchronous zclient and label list */
frr_zclient_addr(&zclient_addr, &zclient_addr_len,
init->zclient_serv_path);
zclient_sync_init();
}
static void
lde_shutdown(void)
{
/* close pipes */
if (iev_ldpe) {
msgbuf_clear(&iev_ldpe->ibuf.w);
close(iev_ldpe->ibuf.fd);
iev_ldpe->ibuf.fd = -1;
}
msgbuf_clear(&iev_main->ibuf.w);
close(iev_main->ibuf.fd);
iev_main->ibuf.fd = -1;
msgbuf_clear(&iev_main_sync->ibuf.w);
close(iev_main_sync->ibuf.fd);
iev_main_sync->ibuf.fd = -1;
lde_gc_stop_timer();
lde_nbr_clear();
fec_tree_clear();
config_clear(ldeconf);
if (iev_ldpe)
free(iev_ldpe);
free(iev_main);
log_info("label decision engine exiting");
zlog_fini();
exit(0);
}
/* imesg */
int
lde_imsg_compose_parent(int type, pid_t pid, void *data, uint16_t datalen)
{
if (iev_main->ibuf.fd == -1)
return (0);
return (imsg_compose_event(iev_main, type, 0, pid, -1, data, datalen));
}
void
lde_imsg_compose_parent_sync(int type, pid_t pid, void *data, uint16_t datalen)
{
if (iev_main_sync->ibuf.fd == -1)
return;
imsg_compose_event(iev_main_sync, type, 0, pid, -1, data, datalen);
imsg_flush(&iev_main_sync->ibuf);
}
int
lde_imsg_compose_ldpe(int type, uint32_t peerid, pid_t pid, void *data,
uint16_t datalen)
{
if (iev_ldpe->ibuf.fd == -1)
return (0);
return (imsg_compose_event(iev_ldpe, type, peerid, pid,
-1, data, datalen));
}
/* ARGSUSED */
static void lde_dispatch_imsg(struct event *thread)
{
struct imsgev *iev = EVENT_ARG(thread);
struct imsgbuf *ibuf = &iev->ibuf;
struct imsg imsg;
struct lde_nbr *ln;
struct map *map;
struct lde_addr *lde_addr;
struct notify_msg *nm;
ssize_t n;
int shut = 0;
iev->ev_read = NULL;
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
fatal("imsg_read error");
if (n == 0) /* connection closed */
shut = 1;
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("lde_dispatch_imsg: imsg_get error");
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_LABEL_MAPPING_FULL:
ln = lde_nbr_find(imsg.hdr.peerid);
if (ln == NULL) {
log_debug("%s: cannot find lde neighbor", __func__);
break;
}
fec_snap(ln);
break;
case IMSG_LABEL_MAPPING:
case IMSG_LABEL_REQUEST:
case IMSG_LABEL_RELEASE:
case IMSG_LABEL_WITHDRAW:
case IMSG_LABEL_ABORT:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(struct map))
fatalx("lde_dispatch_imsg: wrong imsg len");
map = imsg.data;
ln = lde_nbr_find(imsg.hdr.peerid);
if (ln == NULL) {
log_debug("%s: cannot find lde neighbor", __func__);
break;
}
switch (imsg.hdr.type) {
case IMSG_LABEL_MAPPING:
lde_check_mapping(map, ln, 1);
break;
case IMSG_LABEL_REQUEST:
lde_check_request(map, ln);
break;
case IMSG_LABEL_RELEASE:
lde_check_release(map, ln);
break;
case IMSG_LABEL_WITHDRAW:
lde_check_withdraw(map, ln);
break;
case IMSG_LABEL_ABORT:
/* not necessary */
break;
}
break;
case IMSG_ADDRESS_ADD:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(struct lde_addr))
fatalx("lde_dispatch_imsg: wrong imsg len");
lde_addr = imsg.data;
ln = lde_nbr_find(imsg.hdr.peerid);
if (ln == NULL) {
log_debug("%s: cannot find lde neighbor", __func__);
break;
}
if (lde_address_add(ln, lde_addr) < 0) {
log_debug("%s: cannot add address %s, it already exists", __func__,
log_addr(lde_addr->af, &lde_addr->addr));
}
break;
case IMSG_ADDRESS_DEL:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(struct lde_addr))
fatalx("lde_dispatch_imsg: wrong imsg len");
lde_addr = imsg.data;
ln = lde_nbr_find(imsg.hdr.peerid);
if (ln == NULL) {
log_debug("%s: cannot find lde neighbor", __func__);
break;
}
if (lde_address_del(ln, lde_addr) < 0) {
log_debug("%s: cannot delete address %s, it does not exist", __func__,
log_addr(lde_addr->af, &lde_addr->addr));
}
break;
case IMSG_NOTIFICATION:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(struct notify_msg))
fatalx("lde_dispatch_imsg: wrong imsg len");
nm = imsg.data;
ln = lde_nbr_find(imsg.hdr.peerid);
if (ln == NULL) {
log_debug("%s: cannot find lde neighbor", __func__);
break;
}
switch (nm->status_code) {
case S_PW_STATUS:
l2vpn_recv_pw_status(ln, nm);
break;
case S_ENDOFLIB:
/*
* Do nothing for now. Should be useful in
* the future when we implement LDP-IGP
* Synchronization (RFC 5443) and Graceful
* Restart (RFC 3478).
*/
default:
break;
}
break;
case IMSG_NEIGHBOR_UP:
if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(struct lde_nbr))
fatalx("lde_dispatch_imsg: wrong imsg len");
if (lde_nbr_find(imsg.hdr.peerid))
fatalx("lde_dispatch_imsg: neighbor already exists");
lde_nbr_new(imsg.hdr.peerid, imsg.data);
break;
case IMSG_NEIGHBOR_DOWN:
lde_nbr_del(lde_nbr_find(imsg.hdr.peerid));
break;
case IMSG_CTL_SHOW_LIB:
rt_dump(imsg.hdr.pid);
lde_imsg_compose_ldpe(IMSG_CTL_END, 0,
imsg.hdr.pid, NULL, 0);
break;
case IMSG_CTL_SHOW_L2VPN_PW:
l2vpn_pw_ctl(imsg.hdr.pid);
lde_imsg_compose_ldpe(IMSG_CTL_END, 0, imsg.hdr.pid, NULL, 0);
break;
case IMSG_CTL_SHOW_L2VPN_BINDING:
l2vpn_binding_ctl(imsg.hdr.pid);
lde_imsg_compose_ldpe(IMSG_CTL_END, 0, imsg.hdr.pid, NULL, 0);
break;
default:
log_debug("%s: unexpected imsg %d", __func__, imsg.hdr.type);
break;
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* this pipe is dead, so remove the event handlers and exit */
EVENT_OFF(iev->ev_read);
EVENT_OFF(iev->ev_write);
lde_shutdown();
}
}
/* ARGSUSED */
static void lde_dispatch_parent(struct event *thread)
{
static struct ldpd_conf *nconf;
struct iface *iface, *niface;
struct tnbr *ntnbr;
struct nbr_params *nnbrp;
static struct l2vpn *l2vpn, *nl2vpn;
struct l2vpn_if *lif, *nlif;
struct l2vpn_pw *pw, *npw;
struct imsg imsg;
struct kif *kif;
struct kroute *kr;
int fd;
struct imsgev *iev = EVENT_ARG(thread);
struct imsgbuf *ibuf = &iev->ibuf;
ssize_t n;
int shut = 0;
struct fec fec;
struct ldp_access *laccess;
struct ldp_rlfa_node *rnode, *rntmp;
struct ldp_rlfa_client *rclient;
struct zapi_rlfa_request *rlfa_req;
struct zapi_rlfa_igp *rlfa_igp;
iev->ev_read = NULL;
if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
fatal("imsg_read error");
if (n == 0) /* connection closed */
shut = 1;
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal("lde_dispatch_parent: imsg_get error");
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_IFSTATUS:
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(struct kif))
fatalx("IFSTATUS imsg with wrong len");
kif = imsg.data;
iface = if_lookup_name(ldeconf, kif->ifname);
if (iface) {
if_update_info(iface, kif);
/* if up see if any labels need to be updated */
if (kif->operative)
lde_route_update(iface, AF_UNSPEC);
break;
}
RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) {
lif = l2vpn_if_find(l2vpn, kif->ifname);
if (lif) {
l2vpn_if_update_info(lif, kif);
break;
}
pw = l2vpn_pw_find(l2vpn, kif->ifname);
if (pw) {
l2vpn_pw_update_info(pw, kif);
break;
}
}
break;
case IMSG_PW_UPDATE:
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(struct zapi_pw_status))
fatalx("PW_UPDATE imsg with wrong len");
if (l2vpn_pw_status_update(imsg.data) != 0)
log_warnx("%s: error updating PW status", __func__);
break;
case IMSG_NETWORK_ADD:
case IMSG_NETWORK_UPDATE:
if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(struct kroute)) {
log_warnx("%s: wrong imsg len", __func__);
break;
}
kr = imsg.data;
switch (kr->af) {
case AF_INET:
fec.type = FEC_TYPE_IPV4;
fec.u.ipv4.prefix = kr->prefix.v4;
fec.u.ipv4.prefixlen = kr->prefixlen;
break;
case AF_INET6:
fec.type = FEC_TYPE_IPV6;
fec.u.ipv6.prefix = kr->prefix.v6;
fec.u.ipv6.prefixlen = kr->prefixlen;
break;
default:
fatalx("lde_dispatch_parent: unknown af");
}
switch (imsg.hdr.type) {
case IMSG_NETWORK_ADD:
lde_kernel_insert(&fec, kr->af, &kr->nexthop,
kr->ifindex, kr->route_type, kr->route_instance,
CHECK_FLAG(kr->flags, F_CONNECTED), NULL);
break;
case IMSG_NETWORK_UPDATE:
lde_kernel_update(&fec);
break;
}
break;
case IMSG_SOCKET_IPC:
if (iev_ldpe) {
log_warnx("%s: received unexpected imsg fd to ldpe", __func__);
break;
}
if ((fd = imsg.fd) == -1) {
log_warnx("%s: expected to receive imsg fd to ldpe but didn't receive any", __func__);
break;
}
if ((iev_ldpe = malloc(sizeof(struct imsgev))) == NULL)
fatal(NULL);
imsg_init(&iev_ldpe->ibuf, fd);
iev_ldpe->handler_read = lde_dispatch_imsg;
event_add_read(master, iev_ldpe->handler_read, iev_ldpe,
iev_ldpe->ibuf.fd, &iev_ldpe->ev_read);
iev_ldpe->handler_write = ldp_write_handler;
iev_ldpe->ev_write = NULL;
break;
case IMSG_INIT:
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct ldpd_init))
fatalx("INIT imsg with wrong len");
memcpy(&init, imsg.data, sizeof(init));
lde_init(&init);
break;
case IMSG_AGENTX_ENABLED:
ldp_agentx_enabled();
break;
case IMSG_RECONF_CONF:
if ((nconf = malloc(sizeof(struct ldpd_conf))) == NULL)
fatal(NULL);
memcpy(nconf, imsg.data, sizeof(struct ldpd_conf));
RB_INIT(iface_head, &nconf->iface_tree);
RB_INIT(tnbr_head, &nconf->tnbr_tree);
RB_INIT(nbrp_head, &nconf->nbrp_tree);
RB_INIT(l2vpn_head, &nconf->l2vpn_tree);
break;
case IMSG_RECONF_IFACE:
if ((niface = malloc(sizeof(struct iface))) == NULL)
fatal(NULL);
memcpy(niface, imsg.data, sizeof(struct iface));
RB_INSERT(iface_head, &nconf->iface_tree, niface);
break;
case IMSG_RECONF_TNBR:
if ((ntnbr = malloc(sizeof(struct tnbr))) == NULL)
fatal(NULL);
memcpy(ntnbr, imsg.data, sizeof(struct tnbr));
RB_INSERT(tnbr_head, &nconf->tnbr_tree, ntnbr);
break;
case IMSG_RECONF_NBRP:
if ((nnbrp = malloc(sizeof(struct nbr_params))) == NULL)
fatal(NULL);
memcpy(nnbrp, imsg.data, sizeof(struct nbr_params));
RB_INSERT(nbrp_head, &nconf->nbrp_tree, nnbrp);
break;
case IMSG_RECONF_L2VPN:
if ((nl2vpn = malloc(sizeof(struct l2vpn))) == NULL)
fatal(NULL);
memcpy(nl2vpn, imsg.data, sizeof(struct l2vpn));
RB_INIT(l2vpn_if_head, &nl2vpn->if_tree);
RB_INIT(l2vpn_pw_head, &nl2vpn->pw_tree);
RB_INIT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree);
RB_INSERT(l2vpn_head, &nconf->l2vpn_tree, nl2vpn);
break;
case IMSG_RECONF_L2VPN_IF:
if ((nlif = malloc(sizeof(struct l2vpn_if))) == NULL)
fatal(NULL);
memcpy(nlif, imsg.data, sizeof(struct l2vpn_if));
RB_INSERT(l2vpn_if_head, &nl2vpn->if_tree, nlif);
break;
case IMSG_RECONF_L2VPN_PW:
if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
fatal(NULL);
memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_tree, npw);
break;
case IMSG_RECONF_L2VPN_IPW:
if ((npw = malloc(sizeof(struct l2vpn_pw))) == NULL)
fatal(NULL);
memcpy(npw, imsg.data, sizeof(struct l2vpn_pw));
RB_INSERT(l2vpn_pw_head, &nl2vpn->pw_inactive_tree, npw);
break;
case IMSG_RECONF_END:
merge_config(ldeconf, nconf);
ldp_clear_config(nconf);
nconf = NULL;
break;
case IMSG_DEBUG_UPDATE:
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(ldp_debug)) {
log_warnx("%s: wrong imsg len", __func__);
break;
}
memcpy(&ldp_debug, imsg.data, sizeof(ldp_debug));
break;
case IMSG_FILTER_UPDATE:
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct ldp_access)) {
log_warnx("%s: wrong imsg len", __func__);
break;
}
laccess = imsg.data;
lde_check_filter_af(AF_INET, &ldeconf->ipv4,
laccess->name);
lde_check_filter_af(AF_INET6, &ldeconf->ipv6,
laccess->name);
break;
case IMSG_RLFA_REG:
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct zapi_rlfa_request)) {
log_warnx("%s: wrong imsg len", __func__);
break;
}
rlfa_req = imsg.data;
rnode = rlfa_node_find(&rlfa_req->destination,
rlfa_req->pq_address);
if (!rnode)
rnode = rlfa_node_new(&rlfa_req->destination,
rlfa_req->pq_address);
rclient = rlfa_client_find(rnode, &rlfa_req->igp);
if (rclient)
/* RLFA already registered - do nothing */
break;
rclient = rlfa_client_new(rnode, &rlfa_req->igp);
lde_rlfa_check(rclient);
break;
case IMSG_RLFA_UNREG_ALL:
if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct zapi_rlfa_igp)) {
log_warnx("%s: wrong imsg len", __func__);
break;
}
rlfa_igp = imsg.data;
RB_FOREACH_SAFE (rnode, ldp_rlfa_node_head,
&rlfa_node_tree, rntmp) {
rclient = rlfa_client_find(rnode, rlfa_igp);
if (!rclient)
continue;
rlfa_client_del(rclient);
}
break;
default:
log_debug("%s: unexpected imsg %d", __func__, imsg.hdr.type);
break;
}
imsg_free(&imsg);
}
if (!shut)
imsg_event_add(iev);
else {
/* this pipe is dead, so remove the event handlers and exit */
EVENT_OFF(iev->ev_read);
EVENT_OFF(iev->ev_write);
lde_shutdown();
}
}
int
lde_acl_check(char *acl_name, int af, union ldpd_addr *addr, uint8_t prefixlen)
{
return ldp_acl_request(iev_main_sync, acl_name, af, addr, prefixlen);
}
static bool lde_fec_connected(const struct fec_node *fn)
{
struct fec_nh *fnh;
LIST_FOREACH(fnh, &fn->nexthops, entry)
if (CHECK_FLAG(fnh->flags, F_FEC_NH_CONNECTED))
return true;
return false;
}
static bool lde_fec_outside_mpls_network(const struct fec_node *fn)
{
struct fec_nh *fnh;
LIST_FOREACH(fnh, &fn->nexthops, entry)
if (!CHECK_FLAG(fnh->flags, F_FEC_NH_NO_LDP))
return false;
return true;
}
uint32_t
lde_update_label(struct fec_node *fn)
{
/* should we allocate a label for this fec? */
switch (fn->fec.type) {
case FEC_TYPE_IPV4:
if (CHECK_FLAG(ldeconf->ipv4.flags, F_LDPD_AF_ALLOCHOSTONLY)
&& fn->fec.u.ipv4.prefixlen != IPV4_MAX_BITLEN)
return (NO_LABEL);
if (lde_acl_check(ldeconf->ipv4.acl_label_allocate_for,
AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
return (NO_LABEL);
break;
case FEC_TYPE_IPV6:
if (CHECK_FLAG(ldeconf->ipv6.flags, F_LDPD_AF_ALLOCHOSTONLY)
&& fn->fec.u.ipv6.prefixlen != IPV6_MAX_BITLEN)
return (NO_LABEL);
if (lde_acl_check(ldeconf->ipv6.acl_label_allocate_for,
AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
return (NO_LABEL);
break;
case FEC_TYPE_PWID:
break;
}
/*
* If connected interface act as egress for fec.
* If LDP is not configured on an interface but there
* are other NHs with interfaces configured with LDP
* then don't act as an egress for the fec, otherwise
* act as an egress for the fec
*/
if (lde_fec_connected(fn) || lde_fec_outside_mpls_network(fn)) {
/* choose implicit or explicit-null depending on configuration */
switch (fn->fec.type) {
case FEC_TYPE_IPV4:
if (!CHECK_FLAG(ldeconf->ipv4.flags, F_LDPD_AF_EXPNULL))
return (MPLS_LABEL_IMPLICIT_NULL);
if (lde_acl_check(ldeconf->ipv4.acl_label_expnull_for,
AF_INET, (union ldpd_addr *)&fn->fec.u.ipv4.prefix,
fn->fec.u.ipv4.prefixlen) != FILTER_PERMIT)
return (MPLS_LABEL_IMPLICIT_NULL);
return MPLS_LABEL_IPV4_EXPLICIT_NULL;
case FEC_TYPE_IPV6:
if (!CHECK_FLAG(ldeconf->ipv6.flags, F_LDPD_AF_EXPNULL))
return (MPLS_LABEL_IMPLICIT_NULL);
if (lde_acl_check(ldeconf->ipv6.acl_label_expnull_for,
AF_INET6, (union ldpd_addr *)&fn->fec.u.ipv6.prefix,
fn->fec.u.ipv6.prefixlen) != FILTER_PERMIT)
return (MPLS_LABEL_IMPLICIT_NULL);
return MPLS_LABEL_IPV6_EXPLICIT_NULL;
case FEC_TYPE_PWID:
break;
}
}
/* preserve current label if there's no need to update it */
if (fn->local_label != NO_LABEL &&
fn->local_label > MPLS_LABEL_RESERVED_MAX)
return (fn->local_label);
return (lde_get_next_label());
}
void
lde_send_change_klabel(struct fec_node *fn, struct fec_nh *fnh)
{
struct kroute kr;
struct zapi_pw zpw;
struct l2vpn_pw *pw;
/*
* Ordered Control: don't program label into HW until a
* labelmap msg has been received from upstream router
*/
if (CHECK_FLAG(fnh->flags, F_FEC_NH_DEFER))
return;
switch (fn->fec.type) {
case FEC_TYPE_IPV4:
memset(&kr, 0, sizeof(kr));
kr.af = AF_INET;
kr.prefix.v4 = fn->fec.u.ipv4.prefix;
kr.prefixlen = fn->fec.u.ipv4.prefixlen;
kr.nexthop.v4 = fnh->nexthop.v4;
kr.ifindex = fnh->ifindex;
kr.local_label = fn->local_label;
kr.remote_label = fnh->remote_label;
kr.route_type = fnh->route_type;
kr.route_instance = fnh->route_instance;
lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr, sizeof(kr));
break;
case FEC_TYPE_IPV6:
memset(&kr, 0, sizeof(kr));
kr.af = AF_INET6;
kr.prefix.v6 = fn->fec.u.ipv6.prefix;
kr.prefixlen = fn->fec.u.ipv6.prefixlen;
kr.nexthop.v6 = fnh->nexthop.v6;
kr.ifindex = fnh->ifindex;
kr.local_label = fn->local_label;
kr.remote_label = fnh->remote_label;
kr.route_type = fnh->route_type;
kr.route_instance = fnh->route_instance;
lde_imsg_compose_parent(IMSG_KLABEL_CHANGE, 0, &kr, sizeof(kr));
break;
case FEC_TYPE_PWID:
pw = (struct l2vpn_pw *) fn->data;
if (!pw || fn->local_label == NO_LABEL ||
fnh->remote_label == NO_LABEL)
return;
pw->enabled = true;
pw2zpw(pw, &zpw);
zpw.local_label = fn->local_label;
zpw.remote_label = fnh->remote_label;
lde_imsg_compose_parent(IMSG_KPW_SET, 0, &zpw, sizeof(zpw));
break;
}
}
void
lde_send_delete_klabel(struct fec_node *fn, struct fec_nh *fnh)
{
struct kroute kr;
struct zapi_pw zpw;
struct l2vpn_pw *pw;
switch (fn->fec.type) {
case FEC_TYPE_IPV4:
memset(&kr, 0, sizeof(kr));
kr.af = AF_INET;
kr.prefix.v4 = fn->fec.u.ipv4.prefix;
kr.prefixlen = fn->fec.u.ipv4.prefixlen;
kr.nexthop.v4 = fnh->nexthop.v4;
kr.ifindex = fnh->ifindex;
kr.local_label = fn->local_label;
kr.remote_label = fnh->remote_label;
kr.route_type = fnh->route_type;
kr.route_instance = fnh->route_instance;
lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr, sizeof(kr));
break;
case FEC_TYPE_IPV6:
memset(&kr, 0, sizeof(kr));
kr.af = AF_INET6;
kr.prefix.v6 = fn->fec.u.ipv6.prefix;
kr.prefixlen = fn->fec.u.ipv6.prefixlen;
kr.nexthop.v6 = fnh->nexthop.v6;
kr.ifindex = fnh->ifindex;
kr.local_label = fn->local_label;
kr.remote_label = fnh->remote_label;
kr.route_type = fnh->route_type;
kr.route_instance = fnh->route_instance;
lde_imsg_compose_parent(IMSG_KLABEL_DELETE, 0, &kr, sizeof(kr));
break;
case FEC_TYPE_PWID:
pw = (struct l2vpn_pw *) fn->data;
if (!pw)
return;
pw->enabled = false;
pw2zpw(pw, &zpw);
zpw.local_label = fn->local_label;
zpw.remote_label = fnh->remote_label;
lde_imsg_compose_parent(IMSG_KPW_UNSET, 0, &zpw, sizeof(zpw));
break;
}
}
void
lde_fec2prefix(const struct fec *fec, struct prefix *prefix)
{
memset(prefix, 0, sizeof(*prefix));
switch (fec->type) {
case FEC_TYPE_IPV4:
prefix->family = AF_INET;
prefix->u.prefix4 = fec->u.ipv4.prefix;
prefix->prefixlen = fec->u.ipv4.prefixlen;
break;
case FEC_TYPE_IPV6:
prefix->family = AF_INET6;
prefix->u.prefix6 = fec->u.ipv6.prefix;
prefix->prefixlen = fec->u.ipv6.prefixlen;
break;
case FEC_TYPE_PWID:
prefix->family = AF_UNSPEC;
break;
}
}
void
lde_prefix2fec(const struct prefix *prefix, struct fec *fec)
{
memset(fec, 0, sizeof(*fec));
switch (prefix->family) {
case AF_INET:
fec->type = FEC_TYPE_IPV4;
fec->u.ipv4.prefix = prefix->u.prefix4;
fec->u.ipv4.prefixlen = prefix->prefixlen;
break;
case AF_INET6:
fec->type = FEC_TYPE_IPV6;
fec->u.ipv6.prefix = prefix->u.prefix6;
fec->u.ipv6.prefixlen = prefix->prefixlen;
break;
default:
fatalx("lde_prefix2fec: unknown af");
break;
}
}
void
lde_fec2map(struct fec *fec, struct map *map)
{
memset(map, 0, sizeof(*map));
switch (fec->type) {
case FEC_TYPE_IPV4:
map->type = MAP_TYPE_PREFIX;
map->fec.prefix.af = AF_INET;
map->fec.prefix.prefix.v4 = fec->u.ipv4.prefix;
map->fec.prefix.prefixlen = fec->u.ipv4.prefixlen;
break;
case FEC_TYPE_IPV6:
map->type = MAP_TYPE_PREFIX;
map->fec.prefix.af = AF_INET6;
map->fec.prefix.prefix.v6 = fec->u.ipv6.prefix;
map->fec.prefix.prefixlen = fec->u.ipv6.prefixlen;
break;
case FEC_TYPE_PWID:
map->type = MAP_TYPE_PWID;
map->fec.pwid.type = fec->u.pwid.type;
map->fec.pwid.group_id = 0;
SET_FLAG(map->flags, F_MAP_PW_ID);
map->fec.pwid.pwid = fec->u.pwid.pwid;
break;
}
}
void
lde_map2fec(struct map *map, struct in_addr lsr_id, struct fec *fec)
{
memset(fec, 0, sizeof(*fec));
switch (map->type) {
case MAP_TYPE_PREFIX:
switch (map->fec.prefix.af) {
case AF_INET:
fec->type = FEC_TYPE_IPV4;
fec->u.ipv4.prefix = map->fec.prefix.prefix.v4;
fec->u.ipv4.prefixlen = map->fec.prefix.prefixlen;
break;
case AF_INET6:
fec->type = FEC_TYPE_IPV6;
fec->u.ipv6.prefix = map->fec.prefix.prefix.v6;
fec->u.ipv6.prefixlen = map->fec.prefix.prefixlen;
break;
default:
fatalx("lde_map2fec: unknown af");
break;
}
break;
case MAP_TYPE_PWID:
fec->type = FEC_TYPE_PWID;
fec->u.pwid.type = map->fec.pwid.type;
fec->u.pwid.pwid = map->fec.pwid.pwid;
fec->u.pwid.lsr_id = lsr_id;
break;
}
}
void
lde_send_labelmapping(struct lde_nbr *ln, struct fec_node *fn, int single)
{
struct lde_wdraw *lw;
struct lde_map *me;
struct lde_req *lre;