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(struct l2vpn
*, struct l2vpn
*);
31 static __inline
int l2vpn_if_compare(struct l2vpn_if
*, struct l2vpn_if
*);
32 static __inline
int l2vpn_pw_compare(struct l2vpn_pw
*, 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(struct l2vpn
*a
, 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_tree
);
59 RB_INIT(&l2vpn
->pw_tree
);
60 RB_INIT(&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_tree
)) != NULL
) {
80 RB_REMOVE(l2vpn_if_head
, &l2vpn
->if_tree
, lif
);
83 while ((pw
= RB_ROOT(&l2vpn
->pw_tree
)) != NULL
) {
84 RB_REMOVE(l2vpn_pw_head
, &l2vpn
->pw_tree
, pw
);
87 while ((pw
= RB_ROOT(&l2vpn
->pw_inactive_tree
)) != NULL
) {
88 RB_REMOVE(l2vpn_pw_head
, &l2vpn
->pw_inactive_tree
, pw
);
96 l2vpn_init(struct l2vpn
*l2vpn
)
100 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
105 l2vpn_exit(struct l2vpn
*l2vpn
)
109 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
114 l2vpn_if_compare(struct l2vpn_if
*a
, struct l2vpn_if
*b
)
116 return (strcmp(a
->ifname
, b
->ifname
));
120 l2vpn_if_new(struct l2vpn
*l2vpn
, const char *ifname
)
122 struct l2vpn_if
*lif
;
124 if ((lif
= calloc(1, sizeof(*lif
))) == NULL
)
125 fatal("l2vpn_if_new: calloc");
128 strlcpy(lif
->ifname
, ifname
, sizeof(lif
->ifname
));
134 l2vpn_if_find(struct l2vpn
*l2vpn
, const char *ifname
)
137 strlcpy(lif
.ifname
, ifname
, sizeof(lif
.ifname
));
138 return (RB_FIND(l2vpn_if_head
, &l2vpn
->if_tree
, &lif
));
142 l2vpn_if_update_info(struct l2vpn_if
*lif
, struct kif
*kif
)
144 lif
->ifindex
= kif
->ifindex
;
145 lif
->operative
= kif
->operative
;
146 memcpy(lif
->mac
, kif
->mac
, sizeof(lif
->mac
));
150 l2vpn_if_update(struct l2vpn_if
*lif
)
152 struct l2vpn
*l2vpn
= lif
->l2vpn
;
160 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
) {
161 nbr
= nbr_find_ldpid(pw
->lsr_id
.s_addr
);
165 memset(&fec
, 0, sizeof(fec
));
166 fec
.type
= MAP_TYPE_PWID
;
167 fec
.fec
.pwid
.type
= l2vpn
->pw_type
;
168 fec
.fec
.pwid
.group_id
= 0;
169 fec
.flags
|= F_MAP_PW_ID
;
170 fec
.fec
.pwid
.pwid
= pw
->pwid
;
172 send_mac_withdrawal(nbr
, &fec
, lif
->mac
);
177 l2vpn_pw_compare(struct l2vpn_pw
*a
, struct l2vpn_pw
*b
)
179 return (strcmp(a
->ifname
, b
->ifname
));
183 l2vpn_pw_new(struct l2vpn
*l2vpn
, const char *ifname
)
187 if ((pw
= calloc(1, sizeof(*pw
))) == NULL
)
188 fatal("l2vpn_pw_new: calloc");
191 strlcpy(pw
->ifname
, ifname
, sizeof(pw
->ifname
));
197 l2vpn_pw_find(struct l2vpn
*l2vpn
, const char *ifname
)
202 strlcpy(s
.ifname
, ifname
, sizeof(s
.ifname
));
203 pw
= RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_tree
, &s
);
206 return (RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_inactive_tree
, &s
));
210 l2vpn_pw_find_active(struct l2vpn
*l2vpn
, const char *ifname
)
214 strlcpy(s
.ifname
, ifname
, sizeof(s
.ifname
));
215 return (RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_tree
, &s
));
219 l2vpn_pw_find_inactive(struct l2vpn
*l2vpn
, const char *ifname
)
223 strlcpy(s
.ifname
, ifname
, sizeof(s
.ifname
));
224 return (RB_FIND(l2vpn_pw_head
, &l2vpn
->pw_inactive_tree
, &s
));
228 l2vpn_pw_update_info(struct l2vpn_pw
*pw
, struct kif
*kif
)
230 pw
->ifindex
= kif
->ifindex
;
234 l2vpn_pw_init(struct l2vpn_pw
*pw
)
240 l2vpn_pw_fec(pw
, &fec
);
241 lde_kernel_insert(&fec
, AF_INET
, (union ldpd_addr
*)&pw
->lsr_id
, 0, 0,
243 lde_kernel_update(&fec
);
247 l2vpn_pw_exit(struct l2vpn_pw
*pw
)
251 l2vpn_pw_fec(pw
, &fec
);
252 lde_kernel_remove(&fec
, AF_INET
, (union ldpd_addr
*)&pw
->lsr_id
, 0, 0);
253 lde_kernel_update(&fec
);
257 l2vpn_pw_fec(struct l2vpn_pw
*pw
, struct fec
*fec
)
259 memset(fec
, 0, sizeof(*fec
));
260 fec
->type
= FEC_TYPE_PWID
;
261 fec
->u
.pwid
.type
= pw
->l2vpn
->pw_type
;
262 fec
->u
.pwid
.pwid
= pw
->pwid
;
263 fec
->u
.pwid
.lsr_id
= pw
->lsr_id
;
267 l2vpn_pw_reset(struct l2vpn_pw
*pw
)
269 pw
->remote_group
= 0;
271 pw
->remote_status
= 0;
273 if (pw
->flags
& F_PW_CWORD_CONF
)
274 pw
->flags
|= F_PW_CWORD
;
276 pw
->flags
&= ~F_PW_CWORD
;
278 if (pw
->flags
& F_PW_STATUSTLV_CONF
)
279 pw
->flags
|= F_PW_STATUSTLV
;
281 pw
->flags
&= ~F_PW_STATUSTLV
;
285 l2vpn_pw_ok(struct l2vpn_pw
*pw
, struct fec_nh
*fnh
)
287 /* check for a remote label */
288 if (fnh
->remote_label
== NO_LABEL
)
291 /* MTUs must match */
292 if (pw
->l2vpn
->mtu
!= pw
->remote_mtu
)
295 /* check pw status if applicable */
296 if ((pw
->flags
& F_PW_STATUSTLV
) &&
297 pw
->remote_status
!= PW_FORWARDING
)
304 l2vpn_pw_negotiate(struct lde_nbr
*ln
, struct fec_node
*fn
, struct map
*map
)
307 struct status_tlv st
;
309 /* NOTE: thanks martini & friends for all this mess */
311 pw
= (struct l2vpn_pw
*) fn
->data
;
314 * pseudowire not configured, return and record
319 /* RFC4447 - Section 6.2: control word negotiation */
320 if (fec_find(&ln
->sent_map
, &fn
->fec
)) {
321 if ((map
->flags
& F_MAP_PW_CWORD
) &&
322 !(pw
->flags
& F_PW_CWORD_CONF
)) {
323 /* ignore the received label mapping */
325 } else if (!(map
->flags
& F_MAP_PW_CWORD
) &&
326 (pw
->flags
& F_PW_CWORD_CONF
)) {
327 /* append a "Wrong C-bit" status code */
328 st
.status_code
= S_WRONG_CBIT
;
329 st
.msg_id
= map
->msg_id
;
330 st
.msg_type
= htons(MSG_TYPE_LABELMAPPING
);
331 lde_send_labelwithdraw(ln
, fn
, NULL
, &st
);
333 pw
->flags
&= ~F_PW_CWORD
;
334 lde_send_labelmapping(ln
, fn
, 1);
336 } else if (map
->flags
& F_MAP_PW_CWORD
) {
337 if (pw
->flags
& F_PW_CWORD_CONF
)
338 pw
->flags
|= F_PW_CWORD
;
340 /* act as if no label mapping had been received */
343 pw
->flags
&= ~F_PW_CWORD
;
345 /* RFC4447 - Section 5.4.3: pseudowire status negotiation */
346 if (fec_find(&ln
->recv_map
, &fn
->fec
) == NULL
&&
347 !(map
->flags
& F_MAP_PW_STATUS
))
348 pw
->flags
&= ~F_PW_STATUSTLV
;
354 l2vpn_send_pw_status(struct lde_nbr
*ln
, uint32_t status
, struct fec
*fec
)
356 struct notify_msg nm
;
358 memset(&nm
, 0, sizeof(nm
));
359 nm
.status_code
= S_PW_STATUS
;
360 nm
.pw_status
= status
;
361 nm
.flags
|= F_NOTIF_PW_STATUS
;
362 lde_fec2map(fec
, &nm
.fec
);
363 nm
.flags
|= F_NOTIF_FEC
;
365 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
370 l2vpn_send_pw_status_wcard(struct lde_nbr
*ln
, uint32_t status
,
371 uint16_t pw_type
, uint32_t group_id
)
373 struct notify_msg nm
;
375 memset(&nm
, 0, sizeof(nm
));
376 nm
.status_code
= S_PW_STATUS
;
377 nm
.pw_status
= status
;
378 nm
.flags
|= F_NOTIF_PW_STATUS
;
379 nm
.fec
.type
= MAP_TYPE_PWID
;
380 nm
.fec
.fec
.pwid
.type
= pw_type
;
381 nm
.fec
.fec
.pwid
.group_id
= group_id
;
382 nm
.flags
|= F_NOTIF_FEC
;
384 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
389 l2vpn_recv_pw_status(struct lde_nbr
*ln
, struct notify_msg
*nm
)
396 if (nm
->fec
.type
== MAP_TYPE_TYPED_WCARD
||
397 !(nm
->fec
.flags
& F_MAP_PW_ID
)) {
398 l2vpn_recv_pw_status_wcard(ln
, nm
);
402 lde_map2fec(&nm
->fec
, ln
->id
, &fec
);
403 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
408 pw
= (struct l2vpn_pw
*) fn
->data
;
412 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
, 0, 0);
416 /* remote status didn't change */
417 if (pw
->remote_status
== nm
->pw_status
)
419 pw
->remote_status
= nm
->pw_status
;
421 if (l2vpn_pw_ok(pw
, fnh
))
422 lde_send_change_klabel(fn
, fnh
);
424 lde_send_delete_klabel(fn
, fnh
);
427 /* RFC4447 PWid group wildcard */
429 l2vpn_recv_pw_status_wcard(struct lde_nbr
*ln
, struct notify_msg
*nm
)
435 struct map
*wcard
= &nm
->fec
;
437 RB_FOREACH(f
, fec_tree
, &ft
) {
438 fn
= (struct fec_node
*)f
;
439 if (fn
->fec
.type
!= FEC_TYPE_PWID
)
442 pw
= (struct l2vpn_pw
*) fn
->data
;
446 switch (wcard
->type
) {
447 case MAP_TYPE_TYPED_WCARD
:
448 if (wcard
->fec
.twcard
.u
.pw_type
!= PW_TYPE_WILDCARD
&&
449 wcard
->fec
.twcard
.u
.pw_type
!= fn
->fec
.u
.pwid
.type
)
453 if (wcard
->fec
.pwid
.type
!= fn
->fec
.u
.pwid
.type
)
455 if (wcard
->fec
.pwid
.group_id
!= pw
->remote_group
)
460 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
,
465 /* remote status didn't change */
466 if (pw
->remote_status
== nm
->pw_status
)
468 pw
->remote_status
= nm
->pw_status
;
470 if (l2vpn_pw_ok(pw
, fnh
))
471 lde_send_change_klabel(fn
, fnh
);
473 lde_send_delete_klabel(fn
, fnh
);
478 l2vpn_pw_ctl(pid_t pid
)
482 static struct ctl_pw pwctl
;
484 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
)
485 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
) {
486 memset(&pwctl
, 0, sizeof(pwctl
));
487 strlcpy(pwctl
.l2vpn_name
, pw
->l2vpn
->name
,
488 sizeof(pwctl
.l2vpn_name
));
489 strlcpy(pwctl
.ifname
, pw
->ifname
,
490 sizeof(pwctl
.ifname
));
491 pwctl
.pwid
= pw
->pwid
;
492 pwctl
.lsr_id
= pw
->lsr_id
;
493 pwctl
.status
= pw
->flags
& F_PW_STATUS_UP
;
495 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_PW
, 0,
496 pid
, &pwctl
, sizeof(pwctl
));
501 l2vpn_binding_ctl(pid_t pid
)
507 static struct ctl_pw pwctl
;
509 RB_FOREACH(f
, fec_tree
, &ft
) {
510 if (f
->type
!= FEC_TYPE_PWID
)
513 fn
= (struct fec_node
*)f
;
514 if (fn
->local_label
== NO_LABEL
&&
515 RB_EMPTY(&fn
->downstream
))
518 memset(&pwctl
, 0, sizeof(pwctl
));
519 pwctl
.type
= f
->u
.pwid
.type
;
520 pwctl
.pwid
= f
->u
.pwid
.pwid
;
521 pwctl
.lsr_id
= f
->u
.pwid
.lsr_id
;
523 pw
= (struct l2vpn_pw
*) fn
->data
;
525 pwctl
.local_label
= fn
->local_label
;
527 pwctl
.local_ifmtu
= pw
->l2vpn
->mtu
;
528 pwctl
.local_cword
= (pw
->flags
& F_PW_CWORD_CONF
) ?
531 pwctl
.local_label
= NO_LABEL
;
533 RB_FOREACH(me
, lde_map_head
, &fn
->downstream
)
534 if (f
->u
.pwid
.lsr_id
.s_addr
== me
->nexthop
->id
.s_addr
)
538 pwctl
.remote_label
= me
->map
.label
;
539 pwctl
.remote_gid
= me
->map
.fec
.pwid
.group_id
;
540 if (me
->map
.flags
& F_MAP_PW_IFMTU
)
541 pwctl
.remote_ifmtu
= me
->map
.fec
.pwid
.ifmtu
;
543 pwctl
.remote_cword
= (pw
->flags
& F_PW_CWORD
) ?
546 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
547 0, pid
, &pwctl
, sizeof(pwctl
));
549 pwctl
.remote_label
= NO_LABEL
;
551 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
552 0, pid
, &pwctl
, sizeof(pwctl
));
560 ldpe_l2vpn_init(struct l2vpn
*l2vpn
)
564 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
565 ldpe_l2vpn_pw_init(pw
);
569 ldpe_l2vpn_exit(struct l2vpn
*l2vpn
)
573 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
574 ldpe_l2vpn_pw_exit(pw
);
578 ldpe_l2vpn_pw_init(struct l2vpn_pw
*pw
)
582 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
584 tnbr
= tnbr_new(pw
->af
, &pw
->addr
);
586 RB_INSERT(tnbr_head
, &leconf
->tnbr_tree
, tnbr
);
593 ldpe_l2vpn_pw_exit(struct l2vpn_pw
*pw
)
597 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
600 tnbr_check(leconf
, tnbr
);