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 (strcmp(a
->ifname
, 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 (strcmp(a
->ifname
, 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
)
241 l2vpn_pw_fec(pw
, &fec
);
242 lde_kernel_insert(&fec
, AF_INET
, (union ldpd_addr
*)&pw
->lsr_id
, 0, 0,
244 lde_kernel_update(&fec
);
248 l2vpn_pw_exit(struct l2vpn_pw
*pw
)
252 l2vpn_pw_fec(pw
, &fec
);
253 lde_kernel_remove(&fec
, AF_INET
, (union ldpd_addr
*)&pw
->lsr_id
, 0, 0);
254 lde_kernel_update(&fec
);
258 l2vpn_pw_fec(struct l2vpn_pw
*pw
, struct fec
*fec
)
260 memset(fec
, 0, sizeof(*fec
));
261 fec
->type
= FEC_TYPE_PWID
;
262 fec
->u
.pwid
.type
= pw
->l2vpn
->pw_type
;
263 fec
->u
.pwid
.pwid
= pw
->pwid
;
264 fec
->u
.pwid
.lsr_id
= pw
->lsr_id
;
268 l2vpn_pw_reset(struct l2vpn_pw
*pw
)
270 pw
->remote_group
= 0;
272 pw
->remote_status
= 0;
274 if (pw
->flags
& F_PW_CWORD_CONF
)
275 pw
->flags
|= F_PW_CWORD
;
277 pw
->flags
&= ~F_PW_CWORD
;
279 if (pw
->flags
& F_PW_STATUSTLV_CONF
)
280 pw
->flags
|= F_PW_STATUSTLV
;
282 pw
->flags
&= ~F_PW_STATUSTLV
;
286 l2vpn_pw_ok(struct l2vpn_pw
*pw
, struct fec_nh
*fnh
)
288 /* check for a remote label */
289 if (fnh
->remote_label
== NO_LABEL
)
292 /* MTUs must match */
293 if (pw
->l2vpn
->mtu
!= pw
->remote_mtu
)
296 /* check pw status if applicable */
297 if ((pw
->flags
& F_PW_STATUSTLV
) &&
298 pw
->remote_status
!= PW_FORWARDING
)
305 l2vpn_pw_negotiate(struct lde_nbr
*ln
, struct fec_node
*fn
, struct map
*map
)
308 struct status_tlv st
;
310 /* NOTE: thanks martini & friends for all this mess */
312 pw
= (struct l2vpn_pw
*) fn
->data
;
315 * pseudowire not configured, return and record
320 /* RFC4447 - Section 6.2: control word negotiation */
321 if (fec_find(&ln
->sent_map
, &fn
->fec
)) {
322 if ((map
->flags
& F_MAP_PW_CWORD
) &&
323 !(pw
->flags
& F_PW_CWORD_CONF
)) {
324 /* ignore the received label mapping */
326 } else if (!(map
->flags
& F_MAP_PW_CWORD
) &&
327 (pw
->flags
& F_PW_CWORD_CONF
)) {
328 /* append a "Wrong C-bit" status code */
329 st
.status_code
= S_WRONG_CBIT
;
330 st
.msg_id
= map
->msg_id
;
331 st
.msg_type
= htons(MSG_TYPE_LABELMAPPING
);
332 lde_send_labelwithdraw(ln
, fn
, NULL
, &st
);
334 pw
->flags
&= ~F_PW_CWORD
;
335 lde_send_labelmapping(ln
, fn
, 1);
337 } else if (map
->flags
& F_MAP_PW_CWORD
) {
338 if (pw
->flags
& F_PW_CWORD_CONF
)
339 pw
->flags
|= F_PW_CWORD
;
341 /* act as if no label mapping had been received */
344 pw
->flags
&= ~F_PW_CWORD
;
346 /* RFC4447 - Section 5.4.3: pseudowire status negotiation */
347 if (fec_find(&ln
->recv_map
, &fn
->fec
) == NULL
&&
348 !(map
->flags
& F_MAP_PW_STATUS
))
349 pw
->flags
&= ~F_PW_STATUSTLV
;
355 l2vpn_send_pw_status(struct lde_nbr
*ln
, uint32_t status
, struct fec
*fec
)
357 struct notify_msg nm
;
359 memset(&nm
, 0, sizeof(nm
));
360 nm
.status_code
= S_PW_STATUS
;
361 nm
.pw_status
= status
;
362 nm
.flags
|= F_NOTIF_PW_STATUS
;
363 lde_fec2map(fec
, &nm
.fec
);
364 nm
.flags
|= F_NOTIF_FEC
;
366 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
371 l2vpn_send_pw_status_wcard(struct lde_nbr
*ln
, uint32_t status
,
372 uint16_t pw_type
, uint32_t group_id
)
374 struct notify_msg nm
;
376 memset(&nm
, 0, sizeof(nm
));
377 nm
.status_code
= S_PW_STATUS
;
378 nm
.pw_status
= status
;
379 nm
.flags
|= F_NOTIF_PW_STATUS
;
380 nm
.fec
.type
= MAP_TYPE_PWID
;
381 nm
.fec
.fec
.pwid
.type
= pw_type
;
382 nm
.fec
.fec
.pwid
.group_id
= group_id
;
383 nm
.flags
|= F_NOTIF_FEC
;
385 lde_imsg_compose_ldpe(IMSG_NOTIFICATION_SEND
, ln
->peerid
, 0, &nm
,
390 l2vpn_recv_pw_status(struct lde_nbr
*ln
, struct notify_msg
*nm
)
397 if (nm
->fec
.type
== MAP_TYPE_TYPED_WCARD
||
398 !(nm
->fec
.flags
& F_MAP_PW_ID
)) {
399 l2vpn_recv_pw_status_wcard(ln
, nm
);
403 lde_map2fec(&nm
->fec
, ln
->id
, &fec
);
404 fn
= (struct fec_node
*)fec_find(&ft
, &fec
);
409 pw
= (struct l2vpn_pw
*) fn
->data
;
413 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
, 0, 0);
417 /* remote status didn't change */
418 if (pw
->remote_status
== nm
->pw_status
)
420 pw
->remote_status
= nm
->pw_status
;
422 if (l2vpn_pw_ok(pw
, fnh
))
423 lde_send_change_klabel(fn
, fnh
);
425 lde_send_delete_klabel(fn
, fnh
);
428 /* RFC4447 PWid group wildcard */
430 l2vpn_recv_pw_status_wcard(struct lde_nbr
*ln
, struct notify_msg
*nm
)
436 struct map
*wcard
= &nm
->fec
;
438 RB_FOREACH(f
, fec_tree
, &ft
) {
439 fn
= (struct fec_node
*)f
;
440 if (fn
->fec
.type
!= FEC_TYPE_PWID
)
443 pw
= (struct l2vpn_pw
*) fn
->data
;
447 switch (wcard
->type
) {
448 case MAP_TYPE_TYPED_WCARD
:
449 if (wcard
->fec
.twcard
.u
.pw_type
!= PW_TYPE_WILDCARD
&&
450 wcard
->fec
.twcard
.u
.pw_type
!= fn
->fec
.u
.pwid
.type
)
454 if (wcard
->fec
.pwid
.type
!= fn
->fec
.u
.pwid
.type
)
456 if (wcard
->fec
.pwid
.group_id
!= pw
->remote_group
)
461 fnh
= fec_nh_find(fn
, AF_INET
, (union ldpd_addr
*)&ln
->id
,
466 /* remote status didn't change */
467 if (pw
->remote_status
== nm
->pw_status
)
469 pw
->remote_status
= nm
->pw_status
;
471 if (l2vpn_pw_ok(pw
, fnh
))
472 lde_send_change_klabel(fn
, fnh
);
474 lde_send_delete_klabel(fn
, fnh
);
479 l2vpn_pw_ctl(pid_t pid
)
483 static struct ctl_pw pwctl
;
485 RB_FOREACH(l2vpn
, l2vpn_head
, &ldeconf
->l2vpn_tree
)
486 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
) {
487 memset(&pwctl
, 0, sizeof(pwctl
));
488 strlcpy(pwctl
.l2vpn_name
, pw
->l2vpn
->name
,
489 sizeof(pwctl
.l2vpn_name
));
490 strlcpy(pwctl
.ifname
, pw
->ifname
,
491 sizeof(pwctl
.ifname
));
492 pwctl
.pwid
= pw
->pwid
;
493 pwctl
.lsr_id
= pw
->lsr_id
;
494 pwctl
.status
= pw
->flags
& F_PW_STATUS_UP
;
496 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_PW
, 0,
497 pid
, &pwctl
, sizeof(pwctl
));
502 l2vpn_binding_ctl(pid_t pid
)
508 static struct ctl_pw pwctl
;
510 RB_FOREACH(f
, fec_tree
, &ft
) {
511 if (f
->type
!= FEC_TYPE_PWID
)
514 fn
= (struct fec_node
*)f
;
515 if (fn
->local_label
== NO_LABEL
&&
516 RB_EMPTY(lde_map_head
, &fn
->downstream
))
519 memset(&pwctl
, 0, sizeof(pwctl
));
520 pwctl
.type
= f
->u
.pwid
.type
;
521 pwctl
.pwid
= f
->u
.pwid
.pwid
;
522 pwctl
.lsr_id
= f
->u
.pwid
.lsr_id
;
524 pw
= (struct l2vpn_pw
*) fn
->data
;
526 pwctl
.local_label
= fn
->local_label
;
528 pwctl
.local_ifmtu
= pw
->l2vpn
->mtu
;
529 pwctl
.local_cword
= (pw
->flags
& F_PW_CWORD_CONF
) ?
532 pwctl
.local_label
= NO_LABEL
;
534 RB_FOREACH(me
, lde_map_head
, &fn
->downstream
)
535 if (f
->u
.pwid
.lsr_id
.s_addr
== me
->nexthop
->id
.s_addr
)
539 pwctl
.remote_label
= me
->map
.label
;
540 pwctl
.remote_gid
= me
->map
.fec
.pwid
.group_id
;
541 if (me
->map
.flags
& F_MAP_PW_IFMTU
)
542 pwctl
.remote_ifmtu
= me
->map
.fec
.pwid
.ifmtu
;
544 pwctl
.remote_cword
= (pw
->flags
& F_PW_CWORD
) ?
547 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
548 0, pid
, &pwctl
, sizeof(pwctl
));
550 pwctl
.remote_label
= NO_LABEL
;
552 lde_imsg_compose_ldpe(IMSG_CTL_SHOW_L2VPN_BINDING
,
553 0, pid
, &pwctl
, sizeof(pwctl
));
561 ldpe_l2vpn_init(struct l2vpn
*l2vpn
)
565 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
566 ldpe_l2vpn_pw_init(pw
);
570 ldpe_l2vpn_exit(struct l2vpn
*l2vpn
)
574 RB_FOREACH(pw
, l2vpn_pw_head
, &l2vpn
->pw_tree
)
575 ldpe_l2vpn_pw_exit(pw
);
579 ldpe_l2vpn_pw_init(struct l2vpn_pw
*pw
)
583 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
585 tnbr
= tnbr_new(pw
->af
, &pw
->addr
);
587 RB_INSERT(tnbr_head
, &leconf
->tnbr_tree
, tnbr
);
594 ldpe_l2vpn_pw_exit(struct l2vpn_pw
*pw
)
598 tnbr
= tnbr_find(leconf
, pw
->af
, &pw
->addr
);
601 tnbr_check(leconf
, tnbr
);