2013-07-18 15:45:36 +04:00
|
|
|
/* $NetBSD: label.c,v 1.9 2013/07/18 11:45:36 kefren Exp $ */
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Mihai Chelaru <kefren@NetBSD.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netmpls/mpls.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ldp.h"
|
|
|
|
#include "tlv_stack.h"
|
|
|
|
#include "mpls_routes.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "ldp_errors.h"
|
|
|
|
|
2010-12-30 14:29:21 +03:00
|
|
|
int min_label = MIN_LABEL, max_label = MAX_LABEL;
|
|
|
|
|
2010-12-08 10:20:14 +03:00
|
|
|
void
|
|
|
|
label_init()
|
|
|
|
{
|
|
|
|
SLIST_INIT(&label_head);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if binding == 0 it receives a free one
|
|
|
|
*/
|
|
|
|
struct label *
|
2013-07-11 14:46:19 +04:00
|
|
|
label_add(const union sockunion * so_dest, const union sockunion * so_pref,
|
|
|
|
const union sockunion * so_gate, uint32_t binding,
|
|
|
|
const struct ldp_peer * p, uint32_t label)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct label *l;
|
|
|
|
char spreftmp[INET_ADDRSTRLEN];
|
|
|
|
|
2010-12-09 03:10:59 +03:00
|
|
|
l = calloc(1, sizeof(*l));
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
if (!l) {
|
|
|
|
fatalp("label_add: malloc problem\n");
|
|
|
|
return NULL;
|
2010-12-09 03:10:59 +03:00
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
assert(so_dest);
|
|
|
|
assert(so_pref);
|
|
|
|
assert(so_dest->sa.sa_family == so_pref->sa.sa_family);
|
2013-07-18 10:07:45 +04:00
|
|
|
assert(label_get(so_dest, so_pref) == NULL);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-07-18 10:07:45 +04:00
|
|
|
memcpy(&l->so_dest, so_dest, so_dest->sa.sa_len);
|
|
|
|
memcpy(&l->so_pref, so_pref, so_pref->sa.sa_len);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
|
|
|
if (so_gate)
|
2013-07-18 10:07:45 +04:00
|
|
|
memcpy(&l->so_gate, so_gate, so_gate->sa.sa_len);
|
2010-12-08 10:20:14 +03:00
|
|
|
if (binding)
|
|
|
|
l->binding = binding;
|
|
|
|
else
|
|
|
|
l->binding = get_free_local_label();
|
|
|
|
l->p = p;
|
|
|
|
l->label = label;
|
|
|
|
|
|
|
|
SLIST_INSERT_HEAD(&label_head, l, labels);
|
|
|
|
|
2013-07-16 06:54:32 +04:00
|
|
|
strlcpy(spreftmp, satos(&so_pref->sa), INET_ADDRSTRLEN);
|
2010-12-08 10:20:14 +03:00
|
|
|
warnp("[label_add] added binding %d for %s/%s\n", l->binding,
|
2013-07-16 06:54:32 +04:00
|
|
|
satos(&so_dest->sa), spreftmp);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-01-26 21:29:55 +04:00
|
|
|
send_label_tlv_to_all(&(so_dest->sa),
|
2010-12-08 10:20:14 +03:00
|
|
|
from_union_to_cidr(so_pref), l->binding);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlink a label */
|
|
|
|
void
|
|
|
|
label_del(struct label * l)
|
|
|
|
{
|
|
|
|
warnp("[label_del] deleted binding %d for %s\n", l->binding,
|
2013-07-16 06:54:32 +04:00
|
|
|
satos(&l->so_dest.sa));
|
2010-12-08 10:20:14 +03:00
|
|
|
SLIST_REMOVE(&label_head, l, label, labels);
|
|
|
|
free(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-07-18 15:45:36 +04:00
|
|
|
* Delete or Reuse the old IPv4 route, delete MPLS route
|
|
|
|
* readd = REATT_INET_CHANGE -> delete and recreate the INET route
|
|
|
|
* readd = REATT_INET_DEL -> deletes INET route
|
|
|
|
* readd = REATT_INET_NODEL -> doesn't touch the INET route
|
2010-12-08 10:20:14 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
label_reattach_route(struct label *l, int readd)
|
|
|
|
{
|
|
|
|
|
|
|
|
warnp("[label_reattach_route] binding %d deleted\n",
|
|
|
|
l->binding);
|
|
|
|
|
|
|
|
/* No gateway ? */
|
2013-07-18 15:45:36 +04:00
|
|
|
if (l->so_gate.sa.sa_len == 0)
|
2010-12-08 10:20:14 +03:00
|
|
|
return;
|
|
|
|
|
2013-07-18 15:45:36 +04:00
|
|
|
if (readd == REATT_INET_CHANGE) {
|
|
|
|
/* Delete the tagged route and re-add IPv4 route */
|
|
|
|
delete_route(&l->so_dest,
|
|
|
|
l->so_pref.sa.sa_len != 0 ? &l->so_pref : NULL, NO_FREESO);
|
|
|
|
add_route(&l->so_dest,
|
|
|
|
l->so_pref.sa.sa_len != 0 ? &l->so_pref : NULL, &l->so_gate,
|
|
|
|
NULL, NULL, NO_FREESO, RTM_READD);
|
|
|
|
} else if (readd == REATT_INET_DEL)
|
|
|
|
delete_route(&l->so_dest, &l->so_pref, NO_FREESO);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-07-18 15:45:36 +04:00
|
|
|
/* Deletes the MPLS route */
|
|
|
|
if (l->binding >= min_label)
|
|
|
|
delete_route(make_mpls_union(l->binding), NULL, FREESO);
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2013-07-18 15:45:36 +04:00
|
|
|
l->binding = MPLS_LABEL_IMPLNULL;
|
|
|
|
l->p = NULL;
|
|
|
|
l->label = 0;
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get a label by dst and pref
|
|
|
|
*/
|
|
|
|
struct label*
|
2013-07-11 14:46:19 +04:00
|
|
|
label_get(const union sockunion *sodest, const union sockunion *sopref)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct label *l;
|
|
|
|
|
|
|
|
SLIST_FOREACH (l, &label_head, labels)
|
|
|
|
if (sodest->sin.sin_addr.s_addr ==
|
|
|
|
l->so_dest.sin.sin_addr.s_addr &&
|
|
|
|
sopref->sin.sin_addr.s_addr ==
|
|
|
|
l->so_pref.sin.sin_addr.s_addr)
|
|
|
|
return l;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all labels that points to a peer
|
|
|
|
* and reattach them to IPv4
|
|
|
|
*/
|
|
|
|
void
|
2013-07-11 09:45:23 +04:00
|
|
|
label_reattach_all_peer_labels(const struct ldp_peer *p, int readd)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
|
|
|
struct label *l;
|
|
|
|
|
|
|
|
SLIST_FOREACH(l, &label_head, labels)
|
|
|
|
if (l->p == p)
|
|
|
|
label_reattach_route(l, readd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find all labels that points to a peer
|
|
|
|
* and delete them
|
|
|
|
*/
|
|
|
|
void
|
2013-07-11 14:46:19 +04:00
|
|
|
del_all_peer_labels(const struct ldp_peer * p, int readd)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
2010-12-30 14:29:21 +03:00
|
|
|
struct label *l, *lnext;
|
|
|
|
|
|
|
|
SLIST_FOREACH_SAFE(l, &label_head, labels, lnext) {
|
|
|
|
if(l->p != p)
|
|
|
|
continue;
|
|
|
|
label_reattach_route(l, readd);
|
|
|
|
label_del(l);
|
|
|
|
SLIST_REMOVE(&label_head, l, label, labels);
|
|
|
|
}
|
2010-12-08 10:20:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds a label by its binding and deletes it
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
label_del_by_binding(uint32_t binding, int readd)
|
|
|
|
{
|
|
|
|
struct label *l;
|
|
|
|
|
|
|
|
SLIST_FOREACH(l, &label_head, labels)
|
|
|
|
if ((uint32_t)l->binding == binding) {
|
|
|
|
label_reattach_route(l, readd);
|
|
|
|
label_del(l);
|
|
|
|
SLIST_REMOVE(&label_head, l, label, labels);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For Compatibility with old bindinds code
|
|
|
|
*/
|
|
|
|
struct label*
|
2013-01-26 21:29:55 +04:00
|
|
|
label_get_by_prefix(const struct sockaddr *a, int prefixlen)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
2013-07-18 10:07:45 +04:00
|
|
|
const union sockunion *so_dest;
|
|
|
|
union sockunion *so_pref;
|
2010-12-08 10:20:14 +03:00
|
|
|
struct label *l;
|
|
|
|
|
2013-07-18 10:07:45 +04:00
|
|
|
so_dest = (const union sockunion *)a;
|
2010-12-08 10:20:14 +03:00
|
|
|
so_pref = from_cidr_to_union(prefixlen);
|
|
|
|
|
|
|
|
l = label_get(so_dest, so_pref);
|
|
|
|
|
|
|
|
free(so_pref);
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a free binding
|
|
|
|
*/
|
|
|
|
uint32_t
|
|
|
|
get_free_local_label()
|
|
|
|
{
|
|
|
|
struct label *l;
|
2010-12-30 14:29:21 +03:00
|
|
|
int lbl;
|
2010-12-08 10:20:14 +03:00
|
|
|
|
2010-12-30 14:29:21 +03:00
|
|
|
for (lbl = min_label; lbl <= max_label; lbl++) {
|
2010-12-08 10:20:14 +03:00
|
|
|
SLIST_FOREACH(l, &label_head, labels)
|
2010-12-30 14:29:21 +03:00
|
|
|
if (l->binding == lbl)
|
2010-12-08 10:20:14 +03:00
|
|
|
break;
|
|
|
|
if (l == NULL)
|
|
|
|
return lbl;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-07-18 15:45:36 +04:00
|
|
|
* Announce peers that a label has changed its binding
|
|
|
|
* by withdrawing it and reannouncing it
|
2010-12-08 10:20:14 +03:00
|
|
|
*/
|
|
|
|
void
|
2013-07-18 15:45:36 +04:00
|
|
|
announce_label_change(struct label *l)
|
2010-12-08 10:20:14 +03:00
|
|
|
{
|
2013-01-26 21:29:55 +04:00
|
|
|
send_withdraw_tlv_to_all(&(l->so_dest.sa),
|
2010-12-08 10:20:14 +03:00
|
|
|
from_union_to_cidr(&(l->so_pref)));
|
2013-01-26 21:29:55 +04:00
|
|
|
send_label_tlv_to_all(&(l->so_dest.sa),
|
2010-12-08 10:20:14 +03:00
|
|
|
from_union_to_cidr(&(l->so_pref)),
|
|
|
|
l->binding);
|
|
|
|
}
|
2013-07-18 10:07:45 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
label_check_assoc(struct ldp_peer *p)
|
|
|
|
{
|
|
|
|
struct label *l;
|
|
|
|
struct ldp_peer_address *wp;
|
|
|
|
|
|
|
|
SLIST_FOREACH (l, &label_head, labels)
|
|
|
|
if (l->p == NULL && l->so_gate.sa.sa_family != 0)
|
|
|
|
SLIST_FOREACH(wp, &p->ldp_peer_address_head, addresses)
|
|
|
|
if (sockaddr_cmp(&l->so_gate.sa,
|
|
|
|
&wp->address.sa) == 0){
|
|
|
|
l->p = p;
|
|
|
|
l->label = MPLS_LABEL_IMPLNULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|