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