1 /* $NetBSD: tlv_stack.c,v 1.3 2011/02/10 12:44:41 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@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 34 #include <netmpls/mpls.h> 35 36 #include <stdio.h> 37 #include <string.h> 38 #include <stdlib.h> 39 40 #include "ldp.h" 41 #include "ldp_errors.h" 42 #include "ldp_peer.h" 43 #include "tlv.h" 44 #include "socketops.h" 45 #include "pdu.h" 46 #include "label.h" 47 #include "mpls_interface.h" 48 #include "tlv_stack.h" 49 50 uint8_t ldp_ceil8(int); 51 52 uint8_t 53 ldp_ceil8(int x) 54 { 55 if (x % 8 == 0) 56 return x / 8; 57 return x / 8 + 1; 58 } 59 60 int 61 map_label(struct ldp_peer * p, struct fec_tlv * f, struct label_tlv * l) 62 { 63 int n; 64 struct prefix_tlv *pref; 65 struct in_addr inatmp; 66 67 if (ntohs(f->type) != TLV_FEC) { 68 debugp("Invalid FEC TLV !\n"); 69 return LDP_E_BAD_FEC; 70 } 71 if (ntohs(l->type) != TLV_GENERIC_LABEL) { 72 debugp("Invalid LABEL TLV! (0x%.4X)\n", ntohs(l->type)); 73 return LDP_E_BAD_LABEL; 74 } 75 /* 76 * Actually address field length is given only in length field in 77 * bits ! 78 */ 79 80 n = ntohs(f->length); 81 if (!n) 82 return LDP_E_BAD_FEC; 83 84 debugp("Label %u for:\n", ntohl(l->label)); 85 86 pref = (struct prefix_tlv *) & f[1]; 87 88 /* 89 * Section 3.4.1 90 * Note that this version of LDP supports the use of multiple FEC 91 * Elements per FEC for the Label Mapping message only. The use of 92 * multiple FEC Elements in other messages is not permitted in this 93 * version, and is a subject for future study. 94 */ 95 96 for (; n > 0; pref = (struct prefix_tlv *) ((unsigned char *) pref + 97 ldp_ceil8(pref->prelen) + TLV_TYPE_LENGTH)) { 98 n -= ldp_ceil8(pref->prelen) + TLV_TYPE_LENGTH; 99 if (ntohs(pref->af) != LDP_AF_INET) { 100 debugp("BAD ADDRESS FAMILY (%d) ! (prefix type %d, " 101 "length %d)\n", ntohs(pref->af), pref->type, 102 pref->prelen); 103 return LDP_E_BAD_AF; 104 } 105 switch(pref->type) { 106 case FEC_PREFIX: 107 case FEC_HOST: 108 memset(&inatmp, 0, sizeof(struct in_addr)); 109 memcpy(&inatmp, &pref->prefix, ldp_ceil8(pref->prelen)); 110 debugp("Prefix/Host add: %s/%d\n", inet_ntoa(inatmp), 111 pref->prelen); 112 113 /* don't bother if we don't have a label/route */ 114 if (label_get_by_prefix(&inatmp, pref->prelen) == NULL) 115 break; 116 117 ldp_peer_add_mapping(p, &inatmp, pref->prelen, 118 ntohl(l->label)); 119 mpls_add_label(p, NULL, &inatmp, pref->prelen, 120 ntohl(l->label), 1); 121 break; 122 case FEC_WILDCARD: 123 fatalp("LDP: Wildcard add from peer %s\n", 124 inet_ntoa(p->address)); 125 return LDP_E_BAD_FEC; 126 default: 127 fatalp("Unknown FEC type %d\n", pref->type); 128 return LDP_E_BAD_FEC; 129 } 130 } 131 132 return LDP_E_OK; 133 } 134 135 int 136 withdraw_label(struct ldp_peer * p, struct fec_tlv * f) 137 { 138 int n; 139 struct prefix_tlv *pref; 140 struct in_addr inatmp; 141 struct label *lab; 142 143 if (ntohs(f->type) != TLV_FEC) { 144 debugp("Invalid FEC TLV !\n"); 145 return LDP_E_BAD_FEC; 146 } 147 n = ntohs(f->length); 148 if (!n) 149 return LDP_E_BAD_FEC; 150 151 pref = (struct prefix_tlv *) & f[1]; 152 if (ntohs(pref->af) != LDP_AF_INET) { 153 debugp("BAD ADDRESS FAMILY (%d)! (prefix type %d, length %d)\n", 154 ntohs(pref->af), pref->type, pref->prelen); 155 return LDP_E_BAD_AF; 156 } 157 switch(pref->type) { 158 case FEC_PREFIX: 159 case FEC_HOST: 160 memset(&inatmp, 0, sizeof(struct in_addr)); 161 memcpy(&inatmp, &pref->prefix, ldp_ceil8(pref->prelen)); 162 debugp("Prefix/Host withdraw: %s/%d\n", inet_ntoa(inatmp), 163 pref->prelen); 164 165 /* Delete mapping */ 166 ldp_peer_delete_mapping(p, &inatmp, pref->prelen); 167 168 /* Get label, see if we're pointing to this peer 169 * if so, send withdraw, reattach IP route and announce 170 * POP Label 171 */ 172 lab = label_get_by_prefix(&inatmp, pref->prelen); 173 if ((lab) && (lab->p == p)) { 174 change_local_label(lab, MPLS_LABEL_IMPLNULL); 175 label_reattach_route(lab, LDP_READD_CHANGE); 176 } 177 break; 178 case FEC_WILDCARD: 179 fatalp("LDP neighbour %s: Wildcard withdraw !!!\n", 180 inet_ntoa(p->address)); 181 ldp_peer_delete_mapping(p, NULL, 0); 182 label_reattach_all_peer_labels(p, LDP_READD_CHANGE); 183 break; 184 default: 185 fatalp("Unknown FEC type %d\n", pref->type); 186 return LDP_E_BAD_FEC; 187 } 188 189 return LDP_E_OK; 190 } 191 192 193 /* 194 * In case of label redraw, reuse the same buffer to send label release 195 * Simply replace type and message id 196 */ 197 void 198 prepare_release(struct tlv * v) 199 { 200 struct label_map_tlv *t; 201 202 t = (struct label_map_tlv *) v; 203 204 t->type = htons(LDP_LABEL_RELEASE); 205 t->messageid = htonl(get_message_id()); 206 } 207 208 /* Sends a label mapping */ 209 void 210 send_label_tlv(struct ldp_peer * peer, struct in_addr * addr, 211 uint8_t prefixlen, uint32_t label, struct label_request_tlv *lrt) 212 { 213 struct label_map_tlv *lmt; 214 struct fec_tlv *fec; 215 struct prefix_tlv *p; 216 struct label_tlv *l; 217 218 /* 219 * Ok, so we have label map tlv that contains fec tlvs and label tlv 220 * but fec tlv contains prefix or host tlvs and prefix or host tlvs 221 * contains the network. After that we may have optional parameters 222 * Got it ? 223 */ 224 225 lmt = malloc( 226 sizeof(struct label_map_tlv) + 227 sizeof(struct fec_tlv) + 228 sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 229 ldp_ceil8(prefixlen) + 230 sizeof(struct label_tlv) + 231 /* Label request optional parameter */ 232 (lrt != NULL ? sizeof(struct label_request_tlv) : 0) ); 233 234 if (!lmt) { 235 fatalp("send_label_tlv: malloc problem\n"); 236 return; 237 } 238 239 lmt->type = htons(LDP_LABEL_MAPPING); 240 lmt->length = htons(sizeof(struct label_map_tlv) - TLV_TYPE_LENGTH 241 + sizeof(struct fec_tlv) 242 + sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 243 ldp_ceil8(prefixlen) 244 + sizeof(struct label_tlv) + 245 + (lrt != NULL ? sizeof(struct label_request_tlv) : 0)); 246 lmt->messageid = htonl(get_message_id()); 247 248 /* FEC TLV */ 249 fec = (struct fec_tlv *) & lmt[1]; 250 fec->type = htons(TLV_FEC); 251 fec->length = htons(sizeof(struct fec_tlv) - TLV_TYPE_LENGTH 252 + sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 253 ldp_ceil8(prefixlen)); 254 255 /* Now let's do the even a dirtier job: PREFIX TLV */ 256 p = (struct prefix_tlv *) & fec[1]; 257 /* Cisco and Juniper don't support FEC type HOST 258 * so everything is FEC_PREFIX.. 259 * 260 * if (prefixlen == 32) p->type = FEC_HOST; else 261 */ 262 p->type = FEC_PREFIX; 263 p->af = htons(LDP_AF_INET); 264 p->prelen = prefixlen; 265 memcpy(&p->prefix, addr, ldp_ceil8(prefixlen)); 266 267 /* LABEL TLV */ 268 l = (struct label_tlv *) ((unsigned char *) p + 269 sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 270 ldp_ceil8(prefixlen)); 271 l->type = htons(TLV_GENERIC_LABEL); 272 l->length = htons(sizeof(l->label)); 273 l->label = htonl(label); 274 275 /* Label request optional parameter */ 276 if (lrt) 277 memcpy(((char*)l) + TLV_TYPE_LENGTH + ntohs(l->length), 278 lrt, htons(lrt->length) + TLV_TYPE_LENGTH); 279 280 /* Wow, seems we're ready */ 281 send_tlv(peer, (struct tlv *) lmt); 282 free(lmt); 283 } 284 285 void 286 send_label_tlv_to_all(struct in_addr * addr, uint8_t prefixlen, uint32_t label) 287 { 288 struct ldp_peer *p; 289 SLIST_FOREACH(p, &ldp_peer_head, peers) 290 send_label_tlv(p, addr, prefixlen, label, NULL); 291 } 292 293 /* 294 * Send all local labels to a peer 295 */ 296 void 297 send_all_bindings(struct ldp_peer * peer) 298 { 299 struct label *l; 300 301 SLIST_FOREACH(l, &label_head, labels) 302 send_label_tlv(peer, &((struct sockaddr_in*)(&l->so_dest))->sin_addr, 303 from_union_to_cidr(&l->so_pref), l->binding, NULL); 304 305 } 306 307 /* Sends a label WITHDRAW */ 308 void 309 send_withdraw_tlv(struct ldp_peer * peer, struct in_addr * addr, 310 uint8_t prefixlen) 311 { 312 struct label_map_tlv *lmt; 313 struct fec_tlv *fec; 314 struct prefix_tlv *p; 315 316 /* 317 * Ok, so we have label map tlv that contains fec tlvs but fec tlv 318 * contains prefix or host tlvs and prefix or host tlvs contains the 319 * network. Yes, we don't have to announce label here 320 */ 321 322 lmt = malloc(sizeof(struct label_map_tlv) 323 + sizeof(struct fec_tlv) 324 + sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 325 ldp_ceil8(prefixlen)); 326 327 if (!lmt) { 328 fatalp("send_withdraw_tlv: malloc problem\n"); 329 return; 330 } 331 332 lmt->type = htons(LDP_LABEL_WITHDRAW); 333 lmt->length = htons(sizeof(struct label_map_tlv) - TLV_TYPE_LENGTH 334 + sizeof(struct fec_tlv) 335 + sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 336 ldp_ceil8(prefixlen)); 337 lmt->messageid = htonl(get_message_id()); 338 339 /* FEC TLV */ 340 fec = (struct fec_tlv *) & lmt[1]; 341 fec->type = htons(TLV_FEC); 342 fec->length = htons(sizeof(struct fec_tlv) - TLV_TYPE_LENGTH 343 + sizeof(struct prefix_tlv) - sizeof(struct in_addr) + 344 ldp_ceil8(prefixlen)); 345 346 /* Now the even dirtier job: PREFIX TLV */ 347 p = (struct prefix_tlv *) & fec[1]; 348 /* See above comment 349 * 350 * if (prefixlen == 32) p->type = FEC_HOST; else 351 */ 352 p->type = FEC_PREFIX; 353 p->af = htons(LDP_AF_INET); 354 p->prelen = prefixlen; 355 memcpy(&p->prefix, addr, ldp_ceil8(prefixlen)); 356 357 /* Wow, seems we're ready */ 358 send_tlv(peer, (struct tlv *) lmt); 359 free(lmt); 360 } 361 362 void 363 send_withdraw_tlv_to_all(struct in_addr * addr, uint8_t prefixlen) 364 { 365 struct ldp_peer *p; 366 SLIST_FOREACH(p, &ldp_peer_head, peers) 367 send_withdraw_tlv(p, addr, prefixlen); 368 } 369 370 int 371 request_respond(struct ldp_peer *p, struct label_map_tlv *lmt, 372 struct fec_tlv *fec) 373 { 374 struct prefix_tlv *pref; 375 struct in_addr inatmp; 376 struct label *lab; 377 struct label_request_tlv lrm; 378 379 if (ntohs(fec->type) != TLV_FEC || fec->length == 0) { 380 debugp("Invalid FEC TLV !\n"); 381 return LDP_E_BAD_FEC; 382 } 383 pref = (struct prefix_tlv *) (fec + 1); 384 385 if (ntohs(pref->af) != LDP_AF_INET) { 386 debugp("request_respond: Bad address family\n"); 387 return LDP_E_BAD_AF; 388 } 389 390 switch (pref->type) { 391 case FEC_PREFIX: 392 case FEC_HOST: 393 394 memset(&inatmp, 0, sizeof(struct in_addr)); 395 memcpy(&inatmp, &pref->prefix, ldp_ceil8(pref->prelen)); 396 debugp("Prefix/Host request: %s/%d\n", inet_ntoa(inatmp), 397 pref->prelen); 398 399 lab = label_get_by_prefix(&inatmp, pref->prelen); 400 if (!lab) 401 return LDP_E_NO_SUCH_ROUTE; 402 lrm.type = htons(TLV_LABEL_REQUEST); 403 lrm.length = htons(sizeof(uint32_t)); 404 lrm.messageid = lmt->messageid; 405 send_label_tlv(p, &inatmp, pref->prelen, lab->binding, &lrm); 406 break; 407 408 case FEC_WILDCARD: 409 /* 410 * Section 3.4.1 411 * To be used only in the Label Withdraw and Label Release 412 */ 413 /* Fallthrough */ 414 default: 415 416 fatalp("Invalid FEC type\n"); 417 return LDP_E_BAD_FEC; 418 } 419 return LDP_E_OK; 420 } 421