mpls_interface.c revision 1.9 1 /* $NetBSD: mpls_interface.c,v 1.9 2013/07/11 05:45:23 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(const struct ldp_peer * p, struct rt_msg * inh_rg,
57 struct sockaddr * addr, int len, int label, int rlookup)
58 {
59 char padd[20];
60 int kount = 0, rv;
61 union sockunion *so_dest, *so_pref = NULL, *so_gate, *so_nexthop,
62 *so_tag, *so_oldifa = NULL, *so_ifa;
63 struct rt_msg rg;
64 struct rt_msg *rgp = &rg;
65 struct label *lab;
66
67 strlcpy(padd, satos(p->address), 20);
68 debugp("Trying to add %s/%d as label %d to peer %s\n", satos(addr),
69 len, label, padd);
70
71 /* Check if we should accept default route */
72 if (!len && no_default_route != 0)
73 return LDP_E_BAD_AF;
74
75 /* Is there a label mapping for this ? */
76 if (ldp_peer_get_lm(p, addr, len) == NULL)
77 return LDP_E_NOENT;
78
79 if (!inh_rg || (inh_rg->m_rtm.rtm_addrs & RTA_IFA) == 0) {
80 /*
81 * XXX: Check if we have a route for that.
82 * Why the hell isn't kernel inserting the route immediatly ?
83 * let's loop until we have it..
84 */
85
86 if ((so_dest = make_inet_union(satos(addr))) == NULL) // XXX
87 return LDP_E_MEMORY;
88 if (len != 32 && (so_pref = from_cidr_to_union(len)) == NULL) {
89 free(so_dest);
90 return LDP_E_MEMORY;
91 }
92 do {
93 if (kount == rlookup) {
94 debugp("No route for this prefix\n");
95 return LDP_E_NO_SUCH_ROUTE;
96 }
97 if (kount > 0)
98 debugp("Route test hit: %d\n", kount);
99 kount++;
100 /* Last time give it a higher chance */
101 if (kount == rlookup)
102 usleep(5000);
103
104 rv = get_route(rgp, so_dest, so_pref, 1);
105 if (rv != LDP_E_OK && len == 32)
106 /* Host maybe ? */
107 rv = get_route(rgp, so_dest, NULL, 1);
108 } while (rv != LDP_E_OK);
109
110 free(so_dest);
111 if (so_pref)
112 free(so_pref);
113
114 } else
115 rgp = inh_rg;
116
117 /* Check if it's an IPv4 route */
118
119 so_gate = (union sockunion *) rgp->m_space;
120 so_gate = (union sockunion *)((char*)so_gate +
121 RT_ROUNDUP(so_gate->sa.sa_len));
122 if (rgp->m_rtm.rtm_addrs & RTA_IFA) {
123 int li = 1;
124 so_oldifa = so_gate;
125 if (rgp->m_rtm.rtm_addrs & RTA_NETMASK)
126 li++;
127 if (rgp->m_rtm.rtm_addrs & RTA_GENMASK)
128 li++;
129 if (rgp->m_rtm.rtm_addrs & RTA_IFP)
130 li++;
131 for (int i = 0; i < li; i++)
132 so_oldifa = (union sockunion *)((char*)so_oldifa +
133 RT_ROUNDUP(so_oldifa->sa.sa_len));
134 }
135
136 if (so_gate->sa.sa_family != AF_INET &&
137 so_gate->sa.sa_family != AF_INET6) {
138 debugp("mpls_add_label: so_gate is not IP or IPv6\n");
139 return LDP_E_BAD_AF;
140 }
141
142 /* Check if the address is bounded to the peer */
143 if (check_ifaddr(p, &so_gate->sa) == NULL) {
144 debugp("Failed at next-hop check\n");
145 return LDP_E_ROUTE_ERROR;
146 }
147
148 /* Verify if we have a binding for this prefix */
149 lab = label_get_by_prefix(addr, len);
150
151 /* And we should have one because we have a route for it */
152 assert (lab);
153
154 if (lab->binding == MPLS_LABEL_IMPLNULL) {
155 change_local_label(lab, get_free_local_label());
156 if (!lab->binding) {
157 fatalp("Label pool depleted\n");
158 return LDP_E_TOO_MANY_LABELS;
159 }
160 }
161
162 warnp("[mpls_add_label] Adding %s/%d as local binding %d, label %d"
163 " to peer %s\n",
164 satos(addr), len, lab->binding, label, padd);
165
166 /* Modify existing label */
167 lab->label = label;
168 lab->p = p;
169
170 /* Add switching route */
171 so_dest = make_mpls_union(lab->binding);
172 so_nexthop = malloc(sizeof(*so_nexthop));
173 if (!so_nexthop) {
174 free(so_dest);
175 fatalp("Out of memory\n");
176 return LDP_E_MEMORY;
177 }
178 memcpy(so_nexthop, so_gate, so_gate->sa.sa_len);
179 if ((so_tag = make_mpls_union(label)) == NULL) {
180 free(so_dest);
181 free(so_nexthop);
182 fatalp("Out of memory\n");
183 return LDP_E_MEMORY;
184 }
185 if (add_route(so_dest, NULL, so_nexthop, NULL, so_tag,
186 FREESO, RTM_ADD) != LDP_E_OK)
187 return LDP_E_ROUTE_ERROR;
188
189 /* Now, let's add tag to IP route and point it to mpls interface */
190 if ((so_dest = make_inet_union(satos(addr))) == NULL) { // XXX: grobian
191 fatalp("Out of memory\n");
192 return LDP_E_MEMORY;
193 }
194
195 /*
196 * if prefixlen == 32 check if it's inserted as host
197 * and treat it as host. It may also be set as /32 prefix
198 * (thanks mlelstv for heads-up about this)
199 */
200 if ((len == 32) && (rgp->m_rtm.rtm_flags & RTF_HOST))
201 so_pref = NULL;
202 else if ((so_pref = from_cidr_to_union(len)) == NULL) {
203 free(so_dest);
204 fatalp("Out of memory\n");
205 return LDP_E_MEMORY;
206 }
207
208 /* Add tag to route */
209 so_nexthop = malloc(sizeof(*so_nexthop));
210 if (!so_nexthop) {
211 free(so_dest);
212 if (so_pref != NULL)
213 free(so_pref);
214 fatalp("Out of memory\n");
215 return LDP_E_MEMORY;
216 }
217 memcpy(so_nexthop, so_gate, so_gate->sa.sa_len);
218 so_tag = make_mpls_union(label);
219 if (so_oldifa != NULL) {
220 so_ifa = malloc(sizeof(*so_ifa));
221 if (so_ifa == NULL) {
222 free(so_dest);
223 if (so_pref != NULL)
224 free(so_pref);
225 free(so_tag);
226 free(so_nexthop);
227 fatalp("Out of memory\n");
228 return LDP_E_MEMORY;
229 }
230 memcpy(so_ifa, so_oldifa, so_oldifa->sa.sa_len);
231 } else
232 so_ifa = NULL;
233 if (add_route(so_dest, so_pref, so_nexthop, so_ifa, so_tag,
234 FREESO, RTM_CHANGE) != LDP_E_OK)
235 return LDP_E_ROUTE_ERROR;
236
237 debugp("Added %s/%d as label %d to peer %s\n", satos(addr), len,
238 label, padd);
239
240 return LDP_E_OK;
241 }
242
243 int
244 mpls_add_ldp_peer(const struct ldp_peer * p)
245 {
246 return LDP_E_OK;
247 }
248
249 int
250 mpls_delete_ldp_peer(const struct ldp_peer * p)
251 {
252
253 /* Reput all the routes also to IPv4 */
254 label_reattach_all_peer_labels(p, LDP_READD_CHANGE);
255
256 return LDP_E_OK;
257 }
258
259 int
260 mpls_start_ldp()
261 {
262 ldp_peer_init();
263 label_init();
264
265 return LDP_E_OK;
266 }
267