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
) {
299 log_warnx("%s: pseudowire %s: no remote label", __func__
,
304 /* MTUs must match */
305 if (pw
->l2vpn
->mtu
!= pw
->remote_mtu
) {
306 log_warnx("%s: pseudowire %s: MTU mismatch detected", __func__
,
311 /* check pw status if applicable */
312 if ((pw
->flags
& F_PW_STATUSTLV
) &&
313 pw
->remote_status
!= PW_FORWARDING
) {
314 log_warnx("%s: pseudowire %s: remote end is down", __func__
,
323 l2vpn_pw_negotiate(struct lde_nbr
*ln
, struct fec_node
*fn
, struct map
*map
)
326 struct status_tlv st
;
328 /* NOTE: thanks martini & friends for all this mess */
330 pw
= (struct l2vpn_pw
*) fn
->data
;
333 * pseudowire not configured, return and record
338 /* RFC4447 - Section 6.2: control word negotiation */
339 if (fec_find(&ln
->sent_map
, &fn
->fec
)) {
340 if ((map
->flags
& F_MAP_PW_CWORD
) &&
341 !(pw
->flags
& F_PW_CWORD_CONF
)) {
342 /* ignore the received label mapping */
344 } else if (!(map
->flags
& F_MAP_PW_CWORD
) &&
345 (pw
->flags
& F_PW_CWORD_CONF
)) {
346 /* append a "Wrong C-bit" status code */
347 st
.status_code
= S_WRONG_CBIT
;
348 st
.msg_id
= map
->msg_id
;
349 st
.msg_type
= htons(MSG_TYPE_LABELMAPPING
);
350 lde_send_labelwithdraw(ln
, fn
, NULL
, &st
);
352 pw
->flags
&= ~F_PW_CWORD
;
353 lde_send_labelmapping(ln
, fn
, 1);
355 } else if (map
->flags
& F_MAP_PW_CWORD
) {
356 if (pw
->flags
& F_PW_CWORD_CONF
)
357 pw
->flags
|= F_PW_CWORD
;
359 /* act as if no label mapping had been received */
362 pw
->flags
&= ~F_PW_CWORD
;
364 /* RFC4447 - Section 5.4.3: pseudowire status negotiation */
365 if (fec_find(&ln
->recv_map
, &fn
->fec
) == NULL
&&
366 !(map
->flags
& F_MAP_PW_STATUS
))
367 pw
->flags
&= ~F_PW_STATUSTLV
;
373 l2vpn_send_pw_status(struct lde_nbr
*ln
, uint32_t status
, struct fec
*fec
)
375 struct notify_msg nm
;
377 memset(&nm
, 0, sizeof(nm
));
378 nm
.status_code
= S_PW_STATUS
;
379 nm
.pw_status
= status
;
380 nm
.flags
|= F_NOTIF_PW_STATUS
;
381 lde_fec2map(fec
, &nm
.fec
);
382 nm
.flags
|= F_NOTIF_FEC
;
384 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
389 l2vpn_send_pw_status_wcard(struct lde_nbr
*ln
, uint32_t status
,
390 uint16_t pw_type
, uint32_t group_id
)
392 struct notify_msg nm
;
394 memset(&nm
, 0, sizeof(nm
));
395 nm
.status_code
= S_PW_STATUS
;
396 nm
.pw_status
= status
;
397 nm
.flags
|= F_NOTIF_PW_STATUS
;
398 nm
.fec
.type
= MAP_TYPE_PWID
;
399 nm
.fec
.fec
.pwid
.type
= pw_type
;
400 nm
.fec
.fec
.pwid
.group_id
= group_id
;
401 nm
.flags
|= F_NOTIF_FEC
;
403 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
408 l2vpn_recv_pw_status(struct lde_nbr
*ln
, struct notify_msg
*nm
)
415 if (nm
->fec
.type
== MAP_TYPE_TYPED_WCARD
||
416 !(nm
->fec
.flags
& F_MAP_PW_ID
)) {
417 l2vpn_recv_pw_status_wcard(ln
, nm
);
421 lde_map2fec(&nm
->fec
, ln
->id
, &fec
);
422 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
427 pw
= (struct l2vpn_pw
*) fn
->data
;
431 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
, 0, 0);
435 /* remote status didn't change */
436 if (pw
->remote_status
== nm
->pw_status
)
438 pw
->remote_status
= nm
->pw_status
;
440 if (l2vpn_pw_ok(pw
, fnh
))
441 lde_send_change_klabel(fn
, fnh
);
443 lde_send_delete_klabel(fn
, fnh
);
446 /* RFC4447 PWid group wildcard */
448 l2vpn_recv_pw_status_wcard(struct lde_nbr
*ln
, struct notify_msg
*nm
)
454 struct map
*wcard
= &nm
->fec
;
456 RB_FOREACH(f
, fec_tree
, &ft
) {
457 fn
= (struct fec_node
*)f
;
458 if (fn
->fec
.type
!= FEC_TYPE_PWID
)
461 pw
= (struct l2vpn_pw
*) fn
->data
;
465 switch (wcard
->type
) {
466 case MAP_TYPE_TYPED_WCARD
:
467 if (wcard
->fec
.twcard
.u
.pw_type
!= PW_TYPE_WILDCARD
&&
468 wcard
->fec
.twcard
.u
.pw_type
!= fn
->fec
.u
.pwid
.type
)
472 if (wcard
->fec
.pwid
.type
!= fn
->fec
.u
.pwid
.type
)
474 if (wcard
->fec
.pwid
.group_id
!= pw
->remote_group
)
479 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
,
484 /* remote status didn't change */
485 if (pw
->remote_status
== nm
->pw_status
)
487 pw
->remote_status
= nm
->pw_status
;
489 if (l2vpn_pw_ok(pw
, fnh
))
490 lde_send_change_klabel(fn
, fnh
);
492 lde_send_delete_klabel(fn
, fnh
);
497 l2vpn_pw_status_update(struct zapi_pw_status
*zpw
)
500 struct l2vpn_pw
*pw
= NULL
;
503 uint32_t local_status
;
505 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
) {
506 pw
= l2vpn_pw_find(l2vpn
, zpw
->ifname
);
511 log_warnx("%s: pseudowire %s not found", __func__
, zpw
->ifname
);
515 if (zpw
->status
== PW_STATUS_UP
)
516 local_status
= PW_FORWARDING
;
518 local_status
= PW_NOT_FORWARDING
;
520 /* local status didn't change */
521 if (pw
->local_status
== local_status
)
523 pw
->local_status
= local_status
;
525 /* notify remote peer about the status update */
526 ln
= lde_nbr_find_by_lsrid(pw
->lsr_id
);
529 l2vpn_pw_fec(pw
, &fec
);
530 if (pw
->flags
& F_PW_STATUSTLV
)
531 l2vpn_send_pw_status(ln
, local_status
, &fec
);
534 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
536 if (pw
->local_status
== PW_FORWARDING
)
537 lde_send_labelmapping(ln
, fn
, 1);
539 lde_send_labelwithdraw(ln
, fn
, NULL
, NULL
);
547 l2vpn_pw_ctl(pid_t pid
)
551 static struct ctl_pw pwctl
;
553 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
)
554 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
) {
555 memset(&pwctl
, 0, sizeof(pwctl
));
556 strlcpy(pwctl
.l2vpn_name
, pw
->l2vpn
->name
,
557 sizeof(pwctl
.l2vpn_name
));
558 strlcpy(pwctl
.ifname
, pw
->ifname
,
559 sizeof(pwctl
.ifname
));
560 pwctl
.pwid
= pw
->pwid
;
561 pwctl
.lsr_id
= pw
->lsr_id
;
563 pw
->local_status
== PW_FORWARDING
&&
564 pw
->remote_status
== PW_FORWARDING
)
567 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_PW
, 0,
568 pid
, &pwctl
, sizeof(pwctl
));
573 l2vpn_binding_ctl(pid_t pid
)
579 static struct ctl_pw pwctl
;
581 RB_FOREACH(f
, fec_tree
, &ft
) {
582 if (f
->type
!= FEC_TYPE_PWID
)
585 fn
= (struct fec_node
*)f
;
586 if (fn
->local_label
== NO_LABEL
&&
587 RB_EMPTY(lde_map_head
, &fn
->downstream
))
590 memset(&pwctl
, 0, sizeof(pwctl
));
591 pwctl
.type
= f
->u
.pwid
.type
;
592 pwctl
.pwid
= f
->u
.pwid
.pwid
;
593 pwctl
.lsr_id
= f
->u
.pwid
.lsr_id
;
595 pw
= (struct l2vpn_pw
*) fn
->data
;
597 pwctl
.local_label
= fn
->local_label
;
599 pwctl
.local_ifmtu
= pw
->l2vpn
->mtu
;
600 pwctl
.local_cword
= (pw
->flags
& F_PW_CWORD_CONF
) ?
603 pwctl
.local_label
= NO_LABEL
;
605 RB_FOREACH(me
, lde_map_head
, &fn
->downstream
)
606 if (f
->u
.pwid
.lsr_id
.s_addr
== me
->nexthop
->id
.s_addr
)
610 pwctl
.remote_label
= me
->map
.label
;
611 pwctl
.remote_gid
= me
->map
.fec
.pwid
.group_id
;
612 if (me
->map
.flags
& F_MAP_PW_IFMTU
)
613 pwctl
.remote_ifmtu
= me
->map
.fec
.pwid
.ifmtu
;
615 pwctl
.remote_cword
= (pw
->flags
& F_PW_CWORD
) ?
618 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
619 0, pid
, &pwctl
, sizeof(pwctl
));
621 pwctl
.remote_label
= NO_LABEL
;
623 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
624 0, pid
, &pwctl
, sizeof(pwctl
));
632 ldpe_l2vpn_init(struct l2vpn
*l2vpn
)
636 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
637 ldpe_l2vpn_pw_init(pw
);
641 ldpe_l2vpn_exit(struct l2vpn
*l2vpn
)
645 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
646 ldpe_l2vpn_pw_exit(pw
);
650 ldpe_l2vpn_pw_init(struct l2vpn_pw
*pw
)
654 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
656 tnbr
= tnbr_new(pw
->af
, &pw
->addr
);
658 RB_INSERT(tnbr_head
, &leconf
->tnbr_tree
, tnbr
);
665 ldpe_l2vpn_pw_exit(struct l2vpn_pw
*pw
)
669 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
672 tnbr_check(leconf
, tnbr
);