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>
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.
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.
29 static void l2vpn_pw_fec(struct l2vpn_pw
*, struct fec
*);
30 static __inline
int l2vpn_compare(const struct l2vpn
*, const struct l2vpn
*);
31 static __inline
int l2vpn_if_compare(const struct l2vpn_if
*, const struct l2vpn_if
*);
32 static __inline
int l2vpn_pw_compare(const struct l2vpn_pw
*, const struct l2vpn_pw
*);
34 RB_GENERATE(l2vpn_head
, l2vpn
, entry
, l2vpn_compare
)
35 RB_GENERATE(l2vpn_if_head
, l2vpn_if
, entry
, l2vpn_if_compare
)
36 RB_GENERATE(l2vpn_pw_head
, l2vpn_pw
, entry
, l2vpn_pw_compare
)
39 l2vpn_compare(const struct l2vpn
*a
, const struct l2vpn
*b
)
41 return (strcmp(a
->name
, b
->name
));
45 l2vpn_new(const char *name
)
49 if ((l2vpn
= calloc(1, sizeof(*l2vpn
))) == NULL
)
50 fatal("l2vpn_new: calloc");
52 strlcpy(l2vpn
->name
, name
, sizeof(l2vpn
->name
));
54 /* set default values */
55 l2vpn
->mtu
= DEFAULT_L2VPN_MTU
;
56 l2vpn
->pw_type
= DEFAULT_PW_TYPE
;
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
);
66 l2vpn_find(struct ldpd_conf
*xconf
, const char *name
)
69 strlcpy(l2vpn
.name
, name
, sizeof(l2vpn
.name
));
70 return (RB_FIND(l2vpn_head
, &xconf
->l2vpn_tree
, &l2vpn
));
74 l2vpn_del(struct l2vpn
*l2vpn
)
79 while ((lif
= RB_ROOT(l2vpn_if_head
, &l2vpn
->if_tree
)) != NULL
) {
80 RB_REMOVE(l2vpn_if_head
, &l2vpn
->if_tree
, lif
);
83 while ((pw
= RB_ROOT(l2vpn_pw_head
, &l2vpn
->pw_tree
)) != NULL
) {
84 RB_REMOVE(l2vpn_pw_head
, &l2vpn
->pw_tree
, pw
);
87 while ((pw
= RB_ROOT(l2vpn_pw_head
,
88 &l2vpn
->pw_inactive_tree
)) != NULL
) {
89 RB_REMOVE(l2vpn_pw_head
, &l2vpn
->pw_inactive_tree
, pw
);
97 l2vpn_init(struct l2vpn
*l2vpn
)
101 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
106 l2vpn_exit(struct l2vpn
*l2vpn
)
110 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
115 l2vpn_if_compare(const struct l2vpn_if
*a
, const struct l2vpn_if
*b
)
117 return (if_cmp_name_func((char *)a
->ifname
, (char *)b
->ifname
));
121 l2vpn_if_new(struct l2vpn
*l2vpn
, const char *ifname
)
123 struct l2vpn_if
*lif
;
125 if ((lif
= calloc(1, sizeof(*lif
))) == NULL
)
126 fatal("l2vpn_if_new: calloc");
129 strlcpy(lif
->ifname
, ifname
, sizeof(lif
->ifname
));
135 l2vpn_if_find(struct l2vpn
*l2vpn
, const char *ifname
)
138 strlcpy(lif
.ifname
, ifname
, sizeof(lif
.ifname
));
139 return (RB_FIND(l2vpn_if_head
, &l2vpn
->if_tree
, &lif
));
143 l2vpn_if_update_info(struct l2vpn_if
*lif
, struct kif
*kif
)
145 lif
->ifindex
= kif
->ifindex
;
146 lif
->operative
= kif
->operative
;
147 memcpy(lif
->mac
, kif
->mac
, sizeof(lif
->mac
));
151 l2vpn_if_update(struct l2vpn_if
*lif
)
153 struct l2vpn
*l2vpn
= lif
->l2vpn
;
161 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
) {
162 nbr
= nbr_find_ldpid(pw
->lsr_id
.s_addr
);
166 memset(&fec
, 0, sizeof(fec
));
167 fec
.type
= MAP_TYPE_PWID
;
168 fec
.fec
.pwid
.type
= l2vpn
->pw_type
;
169 fec
.fec
.pwid
.group_id
= 0;
170 fec
.flags
|= F_MAP_PW_ID
;
171 fec
.fec
.pwid
.pwid
= pw
->pwid
;
173 send_mac_withdrawal(nbr
, &fec
, lif
->mac
);
178 l2vpn_pw_compare(const struct l2vpn_pw
*a
, const struct l2vpn_pw
*b
)
180 return (if_cmp_name_func((char *)a
->ifname
, (char *)b
->ifname
));
184 l2vpn_pw_new(struct l2vpn
*l2vpn
, const char *ifname
)
188 if ((pw
= calloc(1, sizeof(*pw
))) == NULL
)
189 fatal("l2vpn_pw_new: calloc");
192 strlcpy(pw
->ifname
, ifname
, sizeof(pw
->ifname
));
198 l2vpn_pw_find(struct l2vpn
*l2vpn
, const char *ifname
)
203 strlcpy(s
.ifname
, ifname
, sizeof(s
.ifname
));
204 pw
= RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_tree
, &s
);
207 return (RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_inactive_tree
, &s
));
211 l2vpn_pw_find_active(struct l2vpn
*l2vpn
, const char *ifname
)
215 strlcpy(s
.ifname
, ifname
, sizeof(s
.ifname
));
216 return (RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_tree
, &s
));
220 l2vpn_pw_find_inactive(struct l2vpn
*l2vpn
, const char *ifname
)
224 strlcpy(s
.ifname
, ifname
, sizeof(s
.ifname
));
225 return (RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_inactive_tree
, &s
));
229 l2vpn_pw_update_info(struct l2vpn_pw
*pw
, struct kif
*kif
)
231 pw
->ifindex
= kif
->ifindex
;
235 l2vpn_pw_init(struct l2vpn_pw
*pw
)
243 lde_imsg_compose_parent(IMSG_KPW_ADD
, 0, &zpw
, sizeof(zpw
));
245 l2vpn_pw_fec(pw
, &fec
);
246 lde_kernel_insert(&fec
, AF_INET
, (union ldpd_addr
*)&pw
->lsr_id
, 0, 0,
248 lde_kernel_update(&fec
);
252 l2vpn_pw_exit(struct l2vpn_pw
*pw
)
257 l2vpn_pw_fec(pw
, &fec
);
258 lde_kernel_remove(&fec
, AF_INET
, (union ldpd_addr
*)&pw
->lsr_id
, 0, 0);
259 lde_kernel_update(&fec
);
262 lde_imsg_compose_parent(IMSG_KPW_DELETE
, 0, &zpw
, sizeof(zpw
));
266 l2vpn_pw_fec(struct l2vpn_pw
*pw
, struct fec
*fec
)
268 memset(fec
, 0, sizeof(*fec
));
269 fec
->type
= FEC_TYPE_PWID
;
270 fec
->u
.pwid
.type
= pw
->l2vpn
->pw_type
;
271 fec
->u
.pwid
.pwid
= pw
->pwid
;
272 fec
->u
.pwid
.lsr_id
= pw
->lsr_id
;
276 l2vpn_pw_reset(struct l2vpn_pw
*pw
)
278 pw
->remote_group
= 0;
280 pw
->local_status
= PW_FORWARDING
;
281 pw
->remote_status
= PW_NOT_FORWARDING
;
283 if (pw
->flags
& F_PW_CWORD_CONF
)
284 pw
->flags
|= F_PW_CWORD
;
286 pw
->flags
&= ~F_PW_CWORD
;
288 if (pw
->flags
& F_PW_STATUSTLV_CONF
)
289 pw
->flags
|= F_PW_STATUSTLV
;
291 pw
->flags
&= ~F_PW_STATUSTLV
;
295 l2vpn_pw_ok(struct l2vpn_pw
*pw
, struct fec_nh
*fnh
)
297 /* check for a remote label */
298 if (fnh
->remote_label
== NO_LABEL
)
301 /* MTUs must match */
302 if (pw
->l2vpn
->mtu
!= pw
->remote_mtu
)
305 /* check pw status if applicable */
306 if ((pw
->flags
& F_PW_STATUSTLV
) &&
307 pw
->remote_status
!= PW_FORWARDING
)
314 l2vpn_pw_negotiate(struct lde_nbr
*ln
, struct fec_node
*fn
, struct map
*map
)
317 struct status_tlv st
;
319 /* NOTE: thanks martini & friends for all this mess */
321 pw
= (struct l2vpn_pw
*) fn
->data
;
324 * pseudowire not configured, return and record
329 /* RFC4447 - Section 6.2: control word negotiation */
330 if (fec_find(&ln
->sent_map
, &fn
->fec
)) {
331 if ((map
->flags
& F_MAP_PW_CWORD
) &&
332 !(pw
->flags
& F_PW_CWORD_CONF
)) {
333 /* ignore the received label mapping */
335 } else if (!(map
->flags
& F_MAP_PW_CWORD
) &&
336 (pw
->flags
& F_PW_CWORD_CONF
)) {
337 /* append a "Wrong C-bit" status code */
338 st
.status_code
= S_WRONG_CBIT
;
339 st
.msg_id
= map
->msg_id
;
340 st
.msg_type
= htons(MSG_TYPE_LABELMAPPING
);
341 lde_send_labelwithdraw(ln
, fn
, NULL
, &st
);
343 pw
->flags
&= ~F_PW_CWORD
;
344 lde_send_labelmapping(ln
, fn
, 1);
346 } else if (map
->flags
& F_MAP_PW_CWORD
) {
347 if (pw
->flags
& F_PW_CWORD_CONF
)
348 pw
->flags
|= F_PW_CWORD
;
350 /* act as if no label mapping had been received */
353 pw
->flags
&= ~F_PW_CWORD
;
355 /* RFC4447 - Section 5.4.3: pseudowire status negotiation */
356 if (fec_find(&ln
->recv_map
, &fn
->fec
) == NULL
&&
357 !(map
->flags
& F_MAP_PW_STATUS
))
358 pw
->flags
&= ~F_PW_STATUSTLV
;
364 l2vpn_send_pw_status(struct lde_nbr
*ln
, uint32_t status
, struct fec
*fec
)
366 struct notify_msg nm
;
368 memset(&nm
, 0, sizeof(nm
));
369 nm
.status_code
= S_PW_STATUS
;
370 nm
.pw_status
= status
;
371 nm
.flags
|= F_NOTIF_PW_STATUS
;
372 lde_fec2map(fec
, &nm
.fec
);
373 nm
.flags
|= F_NOTIF_FEC
;
375 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
380 l2vpn_send_pw_status_wcard(struct lde_nbr
*ln
, uint32_t status
,
381 uint16_t pw_type
, uint32_t group_id
)
383 struct notify_msg nm
;
385 memset(&nm
, 0, sizeof(nm
));
386 nm
.status_code
= S_PW_STATUS
;
387 nm
.pw_status
= status
;
388 nm
.flags
|= F_NOTIF_PW_STATUS
;
389 nm
.fec
.type
= MAP_TYPE_PWID
;
390 nm
.fec
.fec
.pwid
.type
= pw_type
;
391 nm
.fec
.fec
.pwid
.group_id
= group_id
;
392 nm
.flags
|= F_NOTIF_FEC
;
394 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
399 l2vpn_recv_pw_status(struct lde_nbr
*ln
, struct notify_msg
*nm
)
406 if (nm
->fec
.type
== MAP_TYPE_TYPED_WCARD
||
407 !(nm
->fec
.flags
& F_MAP_PW_ID
)) {
408 l2vpn_recv_pw_status_wcard(ln
, nm
);
412 lde_map2fec(&nm
->fec
, ln
->id
, &fec
);
413 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
418 pw
= (struct l2vpn_pw
*) fn
->data
;
422 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
, 0, 0);
426 /* remote status didn't change */
427 if (pw
->remote_status
== nm
->pw_status
)
429 pw
->remote_status
= nm
->pw_status
;
431 if (l2vpn_pw_ok(pw
, fnh
))
432 lde_send_change_klabel(fn
, fnh
);
434 lde_send_delete_klabel(fn
, fnh
);
437 /* RFC4447 PWid group wildcard */
439 l2vpn_recv_pw_status_wcard(struct lde_nbr
*ln
, struct notify_msg
*nm
)
445 struct map
*wcard
= &nm
->fec
;
447 RB_FOREACH(f
, fec_tree
, &ft
) {
448 fn
= (struct fec_node
*)f
;
449 if (fn
->fec
.type
!= FEC_TYPE_PWID
)
452 pw
= (struct l2vpn_pw
*) fn
->data
;
456 switch (wcard
->type
) {
457 case MAP_TYPE_TYPED_WCARD
:
458 if (wcard
->fec
.twcard
.u
.pw_type
!= PW_TYPE_WILDCARD
&&
459 wcard
->fec
.twcard
.u
.pw_type
!= fn
->fec
.u
.pwid
.type
)
463 if (wcard
->fec
.pwid
.type
!= fn
->fec
.u
.pwid
.type
)
465 if (wcard
->fec
.pwid
.group_id
!= pw
->remote_group
)
470 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
,
475 /* remote status didn't change */
476 if (pw
->remote_status
== nm
->pw_status
)
478 pw
->remote_status
= nm
->pw_status
;
480 if (l2vpn_pw_ok(pw
, fnh
))
481 lde_send_change_klabel(fn
, fnh
);
483 lde_send_delete_klabel(fn
, fnh
);
488 l2vpn_pw_status_update(struct zapi_pw_status
*zpw
)
491 struct l2vpn_pw
*pw
= NULL
;
494 uint32_t local_status
;
496 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
497 pw
= l2vpn_pw_find(l2vpn
, zpw
->ifname
);
502 log_warnx("%s: pseudowire %s not found", __func__
, zpw
->ifname
);
506 if (zpw
->status
== PW_STATUS_UP
)
507 local_status
= PW_FORWARDING
;
509 local_status
= PW_NOT_FORWARDING
;
511 /* local status didn't change */
512 if (pw
->local_status
== local_status
)
514 pw
->local_status
= local_status
;
516 /* notify remote peer about the status update */
517 ln
= lde_nbr_find_by_lsrid(pw
->lsr_id
);
520 l2vpn_pw_fec(pw
, &fec
);
521 if (pw
->flags
& F_PW_STATUSTLV
)
522 l2vpn_send_pw_status(ln
, local_status
, &fec
);
525 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
527 if (pw
->local_status
== PW_FORWARDING
)
528 lde_send_labelmapping(ln
, fn
, 1);
530 lde_send_labelwithdraw(ln
, fn
, NULL
, NULL
);
538 l2vpn_pw_ctl(pid_t pid
)
542 static struct ctl_pw pwctl
;
544 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
)
545 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
) {
546 memset(&pwctl
, 0, sizeof(pwctl
));
547 strlcpy(pwctl
.l2vpn_name
, pw
->l2vpn
->name
,
548 sizeof(pwctl
.l2vpn_name
));
549 strlcpy(pwctl
.ifname
, pw
->ifname
,
550 sizeof(pwctl
.ifname
));
551 pwctl
.pwid
= pw
->pwid
;
552 pwctl
.lsr_id
= pw
->lsr_id
;
554 pw
->local_status
== PW_FORWARDING
&&
555 pw
->remote_status
== PW_FORWARDING
)
558 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_PW
, 0,
559 pid
, &pwctl
, sizeof(pwctl
));
564 l2vpn_binding_ctl(pid_t pid
)
570 static struct ctl_pw pwctl
;
572 RB_FOREACH(f
, fec_tree
, &ft
) {
573 if (f
->type
!= FEC_TYPE_PWID
)
576 fn
= (struct fec_node
*)f
;
577 if (fn
->local_label
== NO_LABEL
&&
578 RB_EMPTY(lde_map_head
, &fn
->downstream
))
581 memset(&pwctl
, 0, sizeof(pwctl
));
582 pwctl
.type
= f
->u
.pwid
.type
;
583 pwctl
.pwid
= f
->u
.pwid
.pwid
;
584 pwctl
.lsr_id
= f
->u
.pwid
.lsr_id
;
586 pw
= (struct l2vpn_pw
*) fn
->data
;
588 pwctl
.local_label
= fn
->local_label
;
590 pwctl
.local_ifmtu
= pw
->l2vpn
->mtu
;
591 pwctl
.local_cword
= (pw
->flags
& F_PW_CWORD_CONF
) ?
594 pwctl
.local_label
= NO_LABEL
;
596 RB_FOREACH(me
, lde_map_head
, &fn
->downstream
)
597 if (f
->u
.pwid
.lsr_id
.s_addr
== me
->nexthop
->id
.s_addr
)
601 pwctl
.remote_label
= me
->map
.label
;
602 pwctl
.remote_gid
= me
->map
.fec
.pwid
.group_id
;
603 if (me
->map
.flags
& F_MAP_PW_IFMTU
)
604 pwctl
.remote_ifmtu
= me
->map
.fec
.pwid
.ifmtu
;
606 pwctl
.remote_cword
= (pw
->flags
& F_PW_CWORD
) ?
609 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
610 0, pid
, &pwctl
, sizeof(pwctl
));
612 pwctl
.remote_label
= NO_LABEL
;
614 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
615 0, pid
, &pwctl
, sizeof(pwctl
));
623 ldpe_l2vpn_init(struct l2vpn
*l2vpn
)
627 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
628 ldpe_l2vpn_pw_init(pw
);
632 ldpe_l2vpn_exit(struct l2vpn
*l2vpn
)
636 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
637 ldpe_l2vpn_pw_exit(pw
);
641 ldpe_l2vpn_pw_init(struct l2vpn_pw
*pw
)
645 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
647 tnbr
= tnbr_new(pw
->af
, &pw
->addr
);
649 RB_INSERT(tnbr_head
, &leconf
->tnbr_tree
, tnbr
);
656 ldpe_l2vpn_pw_exit(struct l2vpn_pw
*pw
)
660 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
663 tnbr_check(leconf
, tnbr
);