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