Home | History | Annotate | Line # | Download | only in ldpd
mpls_interface.c revision 1.10
      1 /* $NetBSD: mpls_interface.c,v 1.10 2013/07/18 06:07:45 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 		change_local_label(lab, get_free_local_label());
     95 		if (!lab->binding) {
     96 			fatalp("Label pool depleted\n");
     97 			return LDP_E_TOO_MANY_LABELS;
     98 		}
     99 	}
    100 
    101 	warnp("[mpls_add_label] Adding %s/%d as local binding %d (%d), label %d"
    102 	    " to peer %s\n", satos(&lab->so_dest.sa), prefixlen, lab->binding,
    103 	    oldbinding, lab->label, p_str);
    104 
    105 	/* Add switching route */
    106 	if ((so_dest = make_mpls_union(lab->binding)) == NULL)
    107 		return LDP_E_MEMORY;
    108 	if ((so_tag = make_mpls_union(lab->label)) == NULL) {
    109 		free(so_dest);
    110 		fatalp("Out of memory\n");
    111 		return LDP_E_MEMORY;
    112 	}
    113 	if ((so_nexthop = malloc(lab->so_gate.sa.sa_len)) == NULL) {
    114 		free(so_dest);
    115 		free(so_tag);
    116 		fatalp("Out of memory\n");
    117 		return LDP_E_MEMORY;
    118 	}
    119 	memcpy(so_nexthop, &lab->so_gate, lab->so_gate.sa.sa_len);
    120 
    121 	if (add_route(so_dest, NULL, so_nexthop, NULL, so_tag, FREESO,
    122 	    oldbinding == MPLS_LABEL_IMPLNULL ? RTM_ADD : RTM_CHANGE)
    123 	    != LDP_E_OK) {
    124 		fatalp("[mpls_add_label] MPLS route error\n");
    125 		return LDP_E_ROUTE_ERROR;
    126 	}
    127 
    128 	/* Now, let's add tag to IP route and point it to mpls interface */
    129 
    130 	if (getsockname(lab->p->socket, &so_ifa.sa,
    131 	    & (socklen_t) { sizeof(union sockunion) } )) {
    132 		fatalp("[mpls_add_label]: getsockname\n");
    133                 return LDP_E_ROUTE_ERROR;
    134         }
    135 
    136 	if ((so_tag = make_mpls_union(lab->label)) == NULL) {
    137 		fatalp("Out of memory\n");
    138 		return LDP_E_MEMORY;
    139 	}
    140 /*
    141 	if (so_oldifa != NULL) {
    142 		so_ifa = malloc(sizeof(*so_ifa));
    143 		if (so_ifa == NULL) {
    144 			free(so_dest);
    145 			if (so_pref != NULL)
    146 				free(so_pref);
    147 			free(so_tag);
    148 			free(so_nexthop);
    149 			fatalp("Out of memory\n");
    150 			return LDP_E_MEMORY;
    151 		}
    152 		memcpy(so_ifa, so_oldifa, so_oldifa->sa.sa_len);
    153 	} else
    154 		so_ifa = NULL;
    155 */
    156 	if (add_route(&lab->so_dest, &lab->so_pref, &lab->so_gate, &so_ifa,
    157 	    so_tag, NO_FREESO, RTM_CHANGE) != LDP_E_OK) {
    158 		free(so_tag);
    159 		fatalp("[mpls_add_label]: INET route failure\n");
    160 		return LDP_E_ROUTE_ERROR;
    161 	}
    162 	free(so_tag);
    163 
    164 	warnp("[mpls_add_label]: SUCCESS\n");
    165 
    166 	return LDP_E_OK;
    167 }
    168 
    169 int
    170 mpls_add_ldp_peer(const struct ldp_peer * p)
    171 {
    172 	return LDP_E_OK;
    173 }
    174 
    175 int
    176 mpls_delete_ldp_peer(const struct ldp_peer * p)
    177 {
    178 
    179 	/* Reput all the routes also to IPv4 */
    180 	label_reattach_all_peer_labels(p, LDP_READD_CHANGE);
    181 
    182 	return LDP_E_OK;
    183 }
    184 
    185 int
    186 mpls_start_ldp()
    187 {
    188 	ldp_peer_init();
    189 	label_init();
    190 
    191 	return LDP_E_OK;
    192 }
    193