]> git.proxmox.com Git - mirror_frr.git/blame - ldpd/l2vpn.c
Merge pull request #5412 from opensourcerouting/bfdd-vrf-fix
[mirror_frr.git] / ldpd / l2vpn.c
CommitLineData
8429abe0
RW
1/* $OpenBSD$ */
2
3/*
4 * Copyright (c) 2015 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
6 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
7 * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
eac6e3f0 22#include <zebra.h>
8429abe0
RW
23
24#include "ldpd.h"
25#include "ldpe.h"
26#include "lde.h"
27#include "log.h"
28
90d7e7bd 29static void l2vpn_pw_fec(struct l2vpn_pw *, struct fec *);
45926e58
RZ
30static __inline int l2vpn_compare(const struct l2vpn *, const struct l2vpn *);
31static __inline int l2vpn_if_compare(const struct l2vpn_if *, const struct l2vpn_if *);
32static __inline int l2vpn_pw_compare(const struct l2vpn_pw *, const struct l2vpn_pw *);
90d7e7bd
RW
33
34RB_GENERATE(l2vpn_head, l2vpn, entry, l2vpn_compare)
029c1958 35RB_GENERATE(l2vpn_if_head, l2vpn_if, entry, l2vpn_if_compare)
20bacaeb 36RB_GENERATE(l2vpn_pw_head, l2vpn_pw, entry, l2vpn_pw_compare)
90d7e7bd
RW
37
38static __inline int
45926e58 39l2vpn_compare(const struct l2vpn *a, const struct l2vpn *b)
90d7e7bd
RW
40{
41 return (strcmp(a->name, b->name));
42}
8429abe0
RW
43
44struct l2vpn *
45l2vpn_new(const char *name)
46{
47 struct l2vpn *l2vpn;
48
49 if ((l2vpn = calloc(1, sizeof(*l2vpn))) == NULL)
50 fatal("l2vpn_new: calloc");
51
52 strlcpy(l2vpn->name, name, sizeof(l2vpn->name));
53
54 /* set default values */
55 l2vpn->mtu = DEFAULT_L2VPN_MTU;
56 l2vpn->pw_type = DEFAULT_PW_TYPE;
57
45926e58
RZ
58 RB_INIT(l2vpn_if_head, &l2vpn->if_tree);
59 RB_INIT(l2vpn_pw_head, &l2vpn->pw_tree);
60 RB_INIT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
8429abe0
RW
61
62 return (l2vpn);
63}
64
65struct l2vpn *
66l2vpn_find(struct ldpd_conf *xconf, const char *name)
67{
90d7e7bd
RW
68 struct l2vpn l2vpn;
69 strlcpy(l2vpn.name, name, sizeof(l2vpn.name));
70 return (RB_FIND(l2vpn_head, &xconf->l2vpn_tree, &l2vpn));
8429abe0
RW
71}
72
73void
74l2vpn_del(struct l2vpn *l2vpn)
75{
76 struct l2vpn_if *lif;
77 struct l2vpn_pw *pw;
78
55cd0f61
DS
79 while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) {
80 lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree);
81
029c1958 82 RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif);
8429abe0
RW
83 free(lif);
84 }
55cd0f61
DS
85 while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) {
86 pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree);
87
20bacaeb 88 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw);
8429abe0
RW
89 free(pw);
90 }
55cd0f61
DS
91 while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) {
92 pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree);
93
20bacaeb 94 RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw);
eac6e3f0
RW
95 free(pw);
96 }
8429abe0
RW
97
98 free(l2vpn);
99}
100
101void
102l2vpn_init(struct l2vpn *l2vpn)
103{
104 struct l2vpn_pw *pw;
105
20bacaeb 106 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree)
8429abe0
RW
107 l2vpn_pw_init(pw);
108}
109
110void
111l2vpn_exit(struct l2vpn *l2vpn)
112{
113 struct l2vpn_pw *pw;
114
20bacaeb 115 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree)
8429abe0
RW
116 l2vpn_pw_exit(pw);
117}
118
029c1958 119static __inline int
45926e58 120l2vpn_if_compare(const struct l2vpn_if *a, const struct l2vpn_if *b)
029c1958 121{
36de6e0e 122 return if_cmp_name_func(a->ifname, b->ifname);
029c1958
RW
123}
124
8429abe0 125struct l2vpn_if *
52b530fc 126l2vpn_if_new(struct l2vpn *l2vpn, const char *ifname)
8429abe0
RW
127{
128 struct l2vpn_if *lif;
129
130 if ((lif = calloc(1, sizeof(*lif))) == NULL)
131 fatal("l2vpn_if_new: calloc");
132
133 lif->l2vpn = l2vpn;
52b530fc 134 strlcpy(lif->ifname, ifname, sizeof(lif->ifname));
8429abe0
RW
135
136 return (lif);
137}
138
139struct l2vpn_if *
52bd4c23 140l2vpn_if_find(struct l2vpn *l2vpn, const char *ifname)
eac6e3f0 141{
029c1958
RW
142 struct l2vpn_if lif;
143 strlcpy(lif.ifname, ifname, sizeof(lif.ifname));
144 return (RB_FIND(l2vpn_if_head, &l2vpn->if_tree, &lif));
eac6e3f0
RW
145}
146
52b530fc
RW
147void
148l2vpn_if_update_info(struct l2vpn_if *lif, struct kif *kif)
149{
150 lif->ifindex = kif->ifindex;
988ded8d 151 lif->operative = kif->operative;
52b530fc
RW
152 memcpy(lif->mac, kif->mac, sizeof(lif->mac));
153}
154
26519d8c
RW
155void
156l2vpn_if_update(struct l2vpn_if *lif)
157{
158 struct l2vpn *l2vpn = lif->l2vpn;
159 struct l2vpn_pw *pw;
160 struct map fec;
161 struct nbr *nbr;
162
988ded8d 163 if (lif->operative)
26519d8c
RW
164 return;
165
166 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree) {
167 nbr = nbr_find_ldpid(pw->lsr_id.s_addr);
168 if (nbr == NULL)
169 continue;
170
171 memset(&fec, 0, sizeof(fec));
172 fec.type = MAP_TYPE_PWID;
173 fec.fec.pwid.type = l2vpn->pw_type;
174 fec.fec.pwid.group_id = 0;
175 fec.flags |= F_MAP_PW_ID;
176 fec.fec.pwid.pwid = pw->pwid;
177
178 send_mac_withdrawal(nbr, &fec, lif->mac);
179 }
180}
181
20bacaeb 182static __inline int
45926e58 183l2vpn_pw_compare(const struct l2vpn_pw *a, const struct l2vpn_pw *b)
20bacaeb 184{
36de6e0e 185 return if_cmp_name_func(a->ifname, b->ifname);
20bacaeb 186}
eac6e3f0 187
8429abe0 188struct l2vpn_pw *
52b530fc 189l2vpn_pw_new(struct l2vpn *l2vpn, const char *ifname)
8429abe0
RW
190{
191 struct l2vpn_pw *pw;
192
193 if ((pw = calloc(1, sizeof(*pw))) == NULL)
194 fatal("l2vpn_pw_new: calloc");
195
196 pw->l2vpn = l2vpn;
52b530fc 197 strlcpy(pw->ifname, ifname, sizeof(pw->ifname));
8429abe0
RW
198
199 return (pw);
200}
201
202struct l2vpn_pw *
52bd4c23 203l2vpn_pw_find(struct l2vpn *l2vpn, const char *ifname)
eac6e3f0
RW
204{
205 struct l2vpn_pw *pw;
20bacaeb 206 struct l2vpn_pw s;
eac6e3f0 207
20bacaeb
RW
208 strlcpy(s.ifname, ifname, sizeof(s.ifname));
209 pw = RB_FIND(l2vpn_pw_head, &l2vpn->pw_tree, &s);
210 if (pw)
211 return (pw);
212 return (RB_FIND(l2vpn_pw_head, &l2vpn->pw_inactive_tree, &s));
8429abe0
RW
213}
214
5c3f00af
RW
215struct l2vpn_pw *
216l2vpn_pw_find_active(struct l2vpn *l2vpn, const char *ifname)
217{
218 struct l2vpn_pw s;
219
220 strlcpy(s.ifname, ifname, sizeof(s.ifname));
221 return (RB_FIND(l2vpn_pw_head, &l2vpn->pw_tree, &s));
222}
223
224struct l2vpn_pw *
225l2vpn_pw_find_inactive(struct l2vpn *l2vpn, const char *ifname)
226{
227 struct l2vpn_pw s;
228
229 strlcpy(s.ifname, ifname, sizeof(s.ifname));
230 return (RB_FIND(l2vpn_pw_head, &l2vpn->pw_inactive_tree, &s));
231}
232
52b530fc
RW
233void
234l2vpn_pw_update_info(struct l2vpn_pw *pw, struct kif *kif)
235{
236 pw->ifindex = kif->ifindex;
237}
238
8429abe0
RW
239void
240l2vpn_pw_init(struct l2vpn_pw *pw)
241{
242 struct fec fec;
87b5f1b7 243 struct zapi_pw zpw;
8429abe0
RW
244
245 l2vpn_pw_reset(pw);
246
6e8cb226 247 pw2zpw(pw, &zpw);
248 lde_imsg_compose_parent(IMSG_KPW_ADD, 0, &zpw, sizeof(zpw));
249
8429abe0 250 l2vpn_pw_fec(pw, &fec);
88d88a9c 251 lde_kernel_insert(&fec, AF_INET, (union ldpd_addr*)&pw->lsr_id, 0, 0,
e132dea0 252 0, 0, (void *)pw);
8cb1fc45 253 lde_kernel_update(&fec);
8429abe0
RW
254}
255
256void
257l2vpn_pw_exit(struct l2vpn_pw *pw)
258{
259 struct fec fec;
87b5f1b7 260 struct zapi_pw zpw;
8429abe0
RW
261
262 l2vpn_pw_fec(pw, &fec);
e132dea0 263 lde_kernel_remove(&fec, AF_INET, (union ldpd_addr*)&pw->lsr_id, 0, 0, 0);
8cb1fc45 264 lde_kernel_update(&fec);
87b5f1b7
RW
265
266 pw2zpw(pw, &zpw);
267 lde_imsg_compose_parent(IMSG_KPW_DELETE, 0, &zpw, sizeof(zpw));
8429abe0
RW
268}
269
270static void
271l2vpn_pw_fec(struct l2vpn_pw *pw, struct fec *fec)
272{
273 memset(fec, 0, sizeof(*fec));
274 fec->type = FEC_TYPE_PWID;
275 fec->u.pwid.type = pw->l2vpn->pw_type;
276 fec->u.pwid.pwid = pw->pwid;
277 fec->u.pwid.lsr_id = pw->lsr_id;
278}
279
280void
281l2vpn_pw_reset(struct l2vpn_pw *pw)
282{
283 pw->remote_group = 0;
284 pw->remote_mtu = 0;
87b5f1b7
RW
285 pw->local_status = PW_FORWARDING;
286 pw->remote_status = PW_NOT_FORWARDING;
8429abe0
RW
287
288 if (pw->flags & F_PW_CWORD_CONF)
289 pw->flags |= F_PW_CWORD;
290 else
291 pw->flags &= ~F_PW_CWORD;
292
293 if (pw->flags & F_PW_STATUSTLV_CONF)
294 pw->flags |= F_PW_STATUSTLV;
295 else
296 pw->flags &= ~F_PW_STATUSTLV;
297}
298
299int
300l2vpn_pw_ok(struct l2vpn_pw *pw, struct fec_nh *fnh)
301{
8429abe0 302 /* check for a remote label */
34eeae65
RW
303 if (fnh->remote_label == NO_LABEL) {
304 log_warnx("%s: pseudowire %s: no remote label", __func__,
305 pw->ifname);
8429abe0 306 return (0);
34eeae65 307 }
8429abe0
RW
308
309 /* MTUs must match */
34eeae65
RW
310 if (pw->l2vpn->mtu != pw->remote_mtu) {
311 log_warnx("%s: pseudowire %s: MTU mismatch detected", __func__,
312 pw->ifname);
8429abe0 313 return (0);
34eeae65 314 }
8429abe0
RW
315
316 /* check pw status if applicable */
317 if ((pw->flags & F_PW_STATUSTLV) &&
34eeae65
RW
318 pw->remote_status != PW_FORWARDING) {
319 log_warnx("%s: pseudowire %s: remote end is down", __func__,
320 pw->ifname);
8429abe0 321 return (0);
34eeae65 322 }
8429abe0 323
8429abe0
RW
324 return (1);
325}
326
327int
328l2vpn_pw_negotiate(struct lde_nbr *ln, struct fec_node *fn, struct map *map)
329{
330 struct l2vpn_pw *pw;
331 struct status_tlv st;
332
333 /* NOTE: thanks martini & friends for all this mess */
334
335 pw = (struct l2vpn_pw *) fn->data;
336 if (pw == NULL)
337 /*
338 * pseudowire not configured, return and record
339 * the mapping later
340 */
341 return (0);
342
343 /* RFC4447 - Section 6.2: control word negotiation */
344 if (fec_find(&ln->sent_map, &fn->fec)) {
345 if ((map->flags & F_MAP_PW_CWORD) &&
346 !(pw->flags & F_PW_CWORD_CONF)) {
347 /* ignore the received label mapping */
348 return (1);
349 } else if (!(map->flags & F_MAP_PW_CWORD) &&
350 (pw->flags & F_PW_CWORD_CONF)) {
351 /* append a "Wrong C-bit" status code */
352 st.status_code = S_WRONG_CBIT;
353 st.msg_id = map->msg_id;
354 st.msg_type = htons(MSG_TYPE_LABELMAPPING);
0bcc2916 355 lde_send_labelwithdraw(ln, fn, NULL, &st);
8429abe0
RW
356
357 pw->flags &= ~F_PW_CWORD;
358 lde_send_labelmapping(ln, fn, 1);
359 }
360 } else if (map->flags & F_MAP_PW_CWORD) {
361 if (pw->flags & F_PW_CWORD_CONF)
362 pw->flags |= F_PW_CWORD;
363 else
364 /* act as if no label mapping had been received */
365 return (1);
366 } else
367 pw->flags &= ~F_PW_CWORD;
368
369 /* RFC4447 - Section 5.4.3: pseudowire status negotiation */
370 if (fec_find(&ln->recv_map, &fn->fec) == NULL &&
371 !(map->flags & F_MAP_PW_STATUS))
372 pw->flags &= ~F_PW_STATUSTLV;
373
374 return (0);
375}
376
377void
0bcc2916 378l2vpn_send_pw_status(struct lde_nbr *ln, uint32_t status, struct fec *fec)
8429abe0
RW
379{
380 struct notify_msg nm;
381
382 memset(&nm, 0, sizeof(nm));
383 nm.status_code = S_PW_STATUS;
384 nm.pw_status = status;
385 nm.flags |= F_NOTIF_PW_STATUS;
386 lde_fec2map(fec, &nm.fec);
387 nm.flags |= F_NOTIF_FEC;
388
0bcc2916
RW
389 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, &nm,
390 sizeof(nm));
391}
392
393void
394l2vpn_send_pw_status_wcard(struct lde_nbr *ln, uint32_t status,
395 uint16_t pw_type, uint32_t group_id)
396{
397 struct notify_msg nm;
398
399 memset(&nm, 0, sizeof(nm));
400 nm.status_code = S_PW_STATUS;
401 nm.pw_status = status;
402 nm.flags |= F_NOTIF_PW_STATUS;
403 nm.fec.type = MAP_TYPE_PWID;
404 nm.fec.fec.pwid.type = pw_type;
405 nm.fec.fec.pwid.group_id = group_id;
406 nm.flags |= F_NOTIF_FEC;
407
408 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND, ln->peerid, 0, &nm,
409 sizeof(nm));
8429abe0
RW
410}
411
412void
413l2vpn_recv_pw_status(struct lde_nbr *ln, struct notify_msg *nm)
414{
415 struct fec fec;
416 struct fec_node *fn;
417 struct fec_nh *fnh;
418 struct l2vpn_pw *pw;
419
aba50a83
RW
420 if (nm->fec.type == MAP_TYPE_TYPED_WCARD ||
421 !(nm->fec.flags & F_MAP_PW_ID)) {
0bcc2916 422 l2vpn_recv_pw_status_wcard(ln, nm);
8429abe0 423 return;
0bcc2916 424 }
8429abe0
RW
425
426 lde_map2fec(&nm->fec, ln->id, &fec);
427 fn = (struct fec_node *)fec_find(&ft, &fec);
428 if (fn == NULL)
429 /* unknown fec */
430 return;
431
432 pw = (struct l2vpn_pw *) fn->data;
433 if (pw == NULL)
434 return;
435
e132dea0 436 fnh = fec_nh_find(fn, AF_INET, (union ldpd_addr *)&ln->id, 0, 0, 0);
8429abe0
RW
437 if (fnh == NULL)
438 return;
439
440 /* remote status didn't change */
441 if (pw->remote_status == nm->pw_status)
442 return;
8429abe0
RW
443 pw->remote_status = nm->pw_status;
444
445 if (l2vpn_pw_ok(pw, fnh))
446 lde_send_change_klabel(fn, fnh);
447 else
448 lde_send_delete_klabel(fn, fnh);
449}
450
0bcc2916
RW
451/* RFC4447 PWid group wildcard */
452void
453l2vpn_recv_pw_status_wcard(struct lde_nbr *ln, struct notify_msg *nm)
454{
455 struct fec *f;
456 struct fec_node *fn;
457 struct fec_nh *fnh;
458 struct l2vpn_pw *pw;
aba50a83 459 struct map *wcard = &nm->fec;
0bcc2916
RW
460
461 RB_FOREACH(f, fec_tree, &ft) {
462 fn = (struct fec_node *)f;
463 if (fn->fec.type != FEC_TYPE_PWID)
464 continue;
0bcc2916
RW
465
466 pw = (struct l2vpn_pw *) fn->data;
467 if (pw == NULL)
468 continue;
aba50a83
RW
469
470 switch (wcard->type) {
471 case MAP_TYPE_TYPED_WCARD:
472 if (wcard->fec.twcard.u.pw_type != PW_TYPE_WILDCARD &&
473 wcard->fec.twcard.u.pw_type != fn->fec.u.pwid.type)
474 continue;
475 break;
476 case MAP_TYPE_PWID:
477 if (wcard->fec.pwid.type != fn->fec.u.pwid.type)
478 continue;
479 if (wcard->fec.pwid.group_id != pw->remote_group)
480 continue;
481 break;
482 }
0bcc2916
RW
483
484 fnh = fec_nh_find(fn, AF_INET, (union ldpd_addr *)&ln->id,
e132dea0 485 0, 0, 0);
0bcc2916
RW
486 if (fnh == NULL)
487 continue;
488
489 /* remote status didn't change */
490 if (pw->remote_status == nm->pw_status)
491 continue;
492 pw->remote_status = nm->pw_status;
493
494 if (l2vpn_pw_ok(pw, fnh))
495 lde_send_change_klabel(fn, fnh);
496 else
497 lde_send_delete_klabel(fn, fnh);
498 }
499}
500
87b5f1b7
RW
501int
502l2vpn_pw_status_update(struct zapi_pw_status *zpw)
503{
504 struct l2vpn *l2vpn;
505 struct l2vpn_pw *pw = NULL;
506 struct lde_nbr *ln;
507 struct fec fec;
508 uint32_t local_status;
509
510 RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree) {
511 pw = l2vpn_pw_find(l2vpn, zpw->ifname);
512 if (pw)
513 break;
514 }
515 if (!pw) {
516 log_warnx("%s: pseudowire %s not found", __func__, zpw->ifname);
517 return (1);
518 }
519
520 if (zpw->status == PW_STATUS_UP)
521 local_status = PW_FORWARDING;
522 else
523 local_status = PW_NOT_FORWARDING;
524
525 /* local status didn't change */
526 if (pw->local_status == local_status)
527 return (0);
528 pw->local_status = local_status;
529
530 /* notify remote peer about the status update */
531 ln = lde_nbr_find_by_lsrid(pw->lsr_id);
532 if (ln == NULL)
533 return (0);
534 l2vpn_pw_fec(pw, &fec);
535 if (pw->flags & F_PW_STATUSTLV)
536 l2vpn_send_pw_status(ln, local_status, &fec);
537 else {
538 struct fec_node *fn;
539 fn = (struct fec_node *)fec_find(&ft, &fec);
540 if (fn) {
541 if (pw->local_status == PW_FORWARDING)
542 lde_send_labelmapping(ln, fn, 1);
543 else
544 lde_send_labelwithdraw(ln, fn, NULL, NULL);
545 }
546 }
547
548 return (0);
549}
550
8429abe0
RW
551void
552l2vpn_pw_ctl(pid_t pid)
553{
554 struct l2vpn *l2vpn;
555 struct l2vpn_pw *pw;
556 static struct ctl_pw pwctl;
557
90d7e7bd 558 RB_FOREACH(l2vpn, l2vpn_head, &ldeconf->l2vpn_tree)
20bacaeb 559 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree) {
8429abe0 560 memset(&pwctl, 0, sizeof(pwctl));
eac6e3f0
RW
561 strlcpy(pwctl.l2vpn_name, pw->l2vpn->name,
562 sizeof(pwctl.l2vpn_name));
8429abe0
RW
563 strlcpy(pwctl.ifname, pw->ifname,
564 sizeof(pwctl.ifname));
565 pwctl.pwid = pw->pwid;
566 pwctl.lsr_id = pw->lsr_id;
3c5b5220
RW
567 if (pw->enabled &&
568 pw->local_status == PW_FORWARDING &&
87b5f1b7
RW
569 pw->remote_status == PW_FORWARDING)
570 pwctl.status = 1;
8429abe0
RW
571
572 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_PW, 0,
573 pid, &pwctl, sizeof(pwctl));
574 }
575}
576
577void
578l2vpn_binding_ctl(pid_t pid)
579{
580 struct fec *f;
581 struct fec_node *fn;
582 struct lde_map *me;
583 struct l2vpn_pw *pw;
584 static struct ctl_pw pwctl;
585
586 RB_FOREACH(f, fec_tree, &ft) {
587 if (f->type != FEC_TYPE_PWID)
588 continue;
589
590 fn = (struct fec_node *)f;
591 if (fn->local_label == NO_LABEL &&
45926e58 592 RB_EMPTY(lde_map_head, &fn->downstream))
8429abe0
RW
593 continue;
594
595 memset(&pwctl, 0, sizeof(pwctl));
596 pwctl.type = f->u.pwid.type;
597 pwctl.pwid = f->u.pwid.pwid;
598 pwctl.lsr_id = f->u.pwid.lsr_id;
599
600 pw = (struct l2vpn_pw *) fn->data;
601 if (pw) {
602 pwctl.local_label = fn->local_label;
603 pwctl.local_gid = 0;
604 pwctl.local_ifmtu = pw->l2vpn->mtu;
eac6e3f0
RW
605 pwctl.local_cword = (pw->flags & F_PW_CWORD_CONF) ?
606 1 : 0;
8429abe0
RW
607 } else
608 pwctl.local_label = NO_LABEL;
609
d3e1887a 610 RB_FOREACH(me, lde_map_head, &fn->downstream)
8429abe0
RW
611 if (f->u.pwid.lsr_id.s_addr == me->nexthop->id.s_addr)
612 break;
613
614 if (me) {
615 pwctl.remote_label = me->map.label;
616 pwctl.remote_gid = me->map.fec.pwid.group_id;
617 if (me->map.flags & F_MAP_PW_IFMTU)
618 pwctl.remote_ifmtu = me->map.fec.pwid.ifmtu;
eac6e3f0
RW
619 if (pw)
620 pwctl.remote_cword = (pw->flags & F_PW_CWORD) ?
621 1 : 0;
8429abe0
RW
622
623 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING,
624 0, pid, &pwctl, sizeof(pwctl));
625 } else if (pw) {
626 pwctl.remote_label = NO_LABEL;
627
628 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING,
629 0, pid, &pwctl, sizeof(pwctl));
630 }
631 }
632}
633
634/* ldpe */
635
636void
637ldpe_l2vpn_init(struct l2vpn *l2vpn)
638{
639 struct l2vpn_pw *pw;
640
20bacaeb 641 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree)
8429abe0
RW
642 ldpe_l2vpn_pw_init(pw);
643}
644
645void
646ldpe_l2vpn_exit(struct l2vpn *l2vpn)
647{
648 struct l2vpn_pw *pw;
649
20bacaeb 650 RB_FOREACH(pw, l2vpn_pw_head, &l2vpn->pw_tree)
8429abe0
RW
651 ldpe_l2vpn_pw_exit(pw);
652}
653
654void
655ldpe_l2vpn_pw_init(struct l2vpn_pw *pw)
656{
657 struct tnbr *tnbr;
658
659 tnbr = tnbr_find(leconf, pw->af, &pw->addr);
660 if (tnbr == NULL) {
eac6e3f0 661 tnbr = tnbr_new(pw->af, &pw->addr);
8429abe0 662 tnbr_update(tnbr);
7989cdba 663 RB_INSERT(tnbr_head, &leconf->tnbr_tree, tnbr);
8429abe0
RW
664 }
665
666 tnbr->pw_count++;
667}
668
669void
670ldpe_l2vpn_pw_exit(struct l2vpn_pw *pw)
671{
672 struct tnbr *tnbr;
673
674 tnbr = tnbr_find(leconf, pw->af, &pw->addr);
675 if (tnbr) {
676 tnbr->pw_count--;
7989cdba 677 tnbr_check(leconf, tnbr);
8429abe0
RW
678 }
679}