Home | History | Annotate | Line # | Download | only in ldpd
mpls_interface.c revision 1.11
      1 /* $NetBSD: mpls_interface.c,v 1.11 2013/07/18 11:45:36 kefren Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <arpa/inet.h>
     33 #include <netinet/in.h>
     34 #include <netmpls/mpls.h>
     35 #include <net/route.h>
     36 
     37 #include <sys/param.h>
     38 
     39 #include <assert.h>
     40 #include <stdio.h>
     41 #include <string.h>
     42 #include <stdlib.h>
     43 #include <unistd.h>
     44 
     45 #include "ldp.h"
     46 #include "ldp_peer.h"
     47 #include "ldp_errors.h"
     48 #include "tlv_stack.h"
     49 #include "label.h"
     50 #include "mpls_interface.h"
     51 #include "mpls_routes.h"
     52 
     53 extern int no_default_route;
     54 
     55 int
     56 mpls_add_label(struct label *lab)
     57 {
     58 	char            p_str[20];
     59 	union sockunion *so_dest, *so_nexthop, *so_tag, so_ifa;
     60 	uint8_t prefixlen;
     61 	uint32_t oldbinding;
     62 
     63 	assert(lab != NULL);
     64 
     65 	oldbinding = lab->binding;
     66 	prefixlen = from_union_to_cidr(&lab->so_pref);
     67 
     68 	strlcpy(p_str, satos(lab->p->address), sizeof(p_str));
     69 	warnp("Trying to add %s/%d as label %d (oldlocal %d) to peer %s\n",
     70 	    satos(&lab->so_dest.sa), prefixlen, lab->label, lab->binding,p_str);
     71 
     72 	/* Check if we should accept default route */
     73 	if (prefixlen == 0 && no_default_route != 0)
     74 		return LDP_E_BAD_AF;
     75 
     76 	/* double check if there is a label mapping for this */
     77 	if (ldp_peer_get_lm(lab->p, &lab->so_dest.sa, prefixlen) == NULL)
     78 		return LDP_E_NOENT;
     79 
     80 	if (lab->so_gate.sa.sa_family != AF_INET &&
     81 	    lab->so_gate.sa.sa_family != AF_INET6) {
     82 		warnp("mpls_add_label: so_gate is not IP or IPv6\n");
     83 		return LDP_E_BAD_AF;
     84 	}
     85 
     86 	/* Check if the address is bounded to the peer */
     87 	if (check_ifaddr(lab->p, &lab->so_gate.sa) == NULL) {
     88 		warnp("Failed at next-hop check\n");
     89 		return LDP_E_ROUTE_ERROR;
     90 	}
     91 
     92 	/* if binding is implicit null we need to generate a new one */
     93 	if (lab->binding == MPLS_LABEL_IMPLNULL) {
     94 		lab->binding = get_free_local_label();
     95 		if (!lab->binding) {
     96 			fatalp("Label pool depleted\n");
     97 			return LDP_E_TOO_MANY_LABELS;
     98 		}
     99 		announce_label_change(lab);
    100 	}
    101 
    102 	warnp("[mpls_add_label] Adding %s/%d as local binding %d (%d), label %d"
    103 	    " to peer %s\n", satos(&lab->so_dest.sa), prefixlen, lab->binding,
    104 	    oldbinding, lab->label, p_str);
    105 
    106 	/* Add switching route */
    107 	if ((so_dest = make_mpls_union(lab->binding)) == NULL)
    108 		return LDP_E_MEMORY;
    109 	if ((so_tag = make_mpls_union(lab->label)) == NULL) {
    110 		free(so_dest);
    111 		fatalp("Out of memory\n");
    112 		return LDP_E_MEMORY;
    113 	}
    114 	if ((so_nexthop = malloc(lab->so_gate.sa.sa_len)) == NULL) {
    115 		free(so_dest);
    116 		free(so_tag);
    117 		fatalp("Out of memory\n");
    118 		return LDP_E_MEMORY;
    119 	}
    120 	memcpy(so_nexthop, &lab->so_gate, lab->so_gate.sa.sa_len);
    121 
    122 	if (add_route(so_dest, NULL, so_nexthop, NULL, so_tag, FREESO,
    123 	    oldbinding == MPLS_LABEL_IMPLNULL ? RTM_ADD : RTM_CHANGE)
    124 	    != LDP_E_OK) {
    125 		fatalp("[mpls_add_label] MPLS route error\n");
    126 		return LDP_E_ROUTE_ERROR;
    127 	}
    128 
    129 	/* Now, let's add tag to IP route and point it to mpls interface */
    130 
    131 	if (getsockname(lab->p->socket, &so_ifa.sa,
    132 	    & (socklen_t) { sizeof(union sockunion) } )) {
    133 		fatalp("[mpls_add_label]: getsockname\n");
    134                 return LDP_E_ROUTE_ERROR;
    135         }
    136 
    137 	if ((so_tag = make_mpls_union(lab->label)) == NULL) {
    138 		fatalp("Out of memory\n");
    139 		return LDP_E_MEMORY;
    140 	}
    141 /*
    142 	if (so_oldifa != NULL) {
    143 		so_ifa = malloc(sizeof(*so_ifa));
    144 		if (so_ifa == NULL) {
    145 			free(so_dest);
    146 			if (so_pref != NULL)
    147 				free(so_pref);
    148 			free(so_tag);
    149 			free(so_nexthop);
    150 			fatalp("Out of memory\n");
    151 			return LDP_E_MEMORY;
    152 		}
    153 		memcpy(so_ifa, so_oldifa, so_oldifa->sa.sa_len);
    154 	} else
    155 		so_ifa = NULL;
    156 */
    157 	if (add_route(&lab->so_dest, &lab->so_pref, &lab->so_gate, &so_ifa,
    158 	    so_tag, NO_FREESO, RTM_CHANGE) != LDP_E_OK) {
    159 		free(so_tag);
    160 		fatalp("[mpls_add_label]: INET route failure\n");
    161 		return LDP_E_ROUTE_ERROR;
    162 	}
    163 	free(so_tag);
    164 
    165 	warnp("[mpls_add_label]: SUCCESS\n");
    166 
    167 	return LDP_E_OK;
    168 }
    169 
    170 int
    171 mpls_add_ldp_peer(const struct ldp_peer * p)
    172 {
    173 	return LDP_E_OK;
    174 }
    175 
    176 int
    177 mpls_delete_ldp_peer(const struct ldp_peer * p)
    178 {
    179 
    180 	/* Reput all the routes also to IPv4 */
    181 	label_reattach_all_peer_labels(p, REATT_INET_CHANGE);
    182 
    183 	return LDP_E_OK;
    184 }
    185 
    186 int
    187 mpls_start_ldp()
    188 {
    189 	ldp_peer_init();
    190 	label_init();
    191 
    192 	return LDP_E_OK;
    193 }
    194