an address can only be advertised by one session. Thus
[mpls-ldp-portable.git] / ldp / ldp_label_mapping.c
blob7aab1e087ebdf051cbd678204b9cf23e3c3408c7
2 /*
3 * Copyright (C) James R. Leu 2000
4 * jleu@mindspring.com
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
8 */
10 #include "ldp_struct.h"
11 #include "ldp_session.h"
12 #include "ldp_attr.h"
13 #include "ldp_fec.h"
14 #include "ldp_mesg.h"
15 #include "ldp_notif.h"
16 #include "ldp_entity.h"
17 #include "ldp_inlabel.h"
18 #include "ldp_outlabel.h"
19 #include "ldp_nexthop.h"
20 #include "ldp_global.h"
21 #include "ldp_pdu_setup.h"
22 #include "ldp_label_rel_with.h"
23 #include "ldp_label_mapping.h"
24 #include "ldp_label_request.h"
26 #include "mpls_timer_impl.h"
27 #include "mpls_fib_impl.h"
28 #include "mpls_lock_impl.h"
29 #include "mpls_tree_impl.h"
30 #include "mpls_trace_impl.h"
31 #include "mpls_mm_impl.h"
32 #include "mpls_policy_impl.h"
34 #if MPLS_USE_LSR
35 #include "lsr_cfg.h"
36 #else
37 #include "mpls_mpls_impl.h"
38 #endif
40 mpls_return_enum ldp_label_mapping_with_xc(ldp_global * g, ldp_session * s,
41 ldp_fec * f, ldp_attr ** us_attr, ldp_attr * ds_attr)
43 mpls_return_enum result = MPLS_SUCCESS;
44 mpls_bool propogating = MPLS_BOOL_TRUE;
45 mpls_bool egress = MPLS_BOOL_FALSE;
46 mpls_bool egress_flag = MPLS_BOOL_FALSE;
47 mpls_bool created = MPLS_BOOL_FALSE;
48 ldp_nexthop *nh;
50 MPLS_ASSERT(us_attr);
52 nh = MPLS_LIST_HEAD(&f->nh_root);
53 while (nh) {
54 if (egress_flag == MPLS_BOOL_FALSE) {
55 egress_flag = mpls_policy_egress_check(g->user_data, &f->info, &nh->info);
57 nh = MPLS_LIST_NEXT(&f->nh_root, nh, _fec);
60 if (!(*us_attr)) {
61 if (!((*us_attr) = ldp_attr_create(&f->info))) {
62 return MPLS_FAILURE;
64 created = MPLS_BOOL_TRUE;
66 if ((!ds_attr) && (egress_flag == MPLS_BOOL_TRUE)) {
67 propogating = MPLS_BOOL_FALSE;
68 egress = MPLS_BOOL_TRUE;
71 Prepare_Label_Mapping_Attributes(g, s, &f->info, ds_attr, (*us_attr),
72 propogating, MPLS_BOOL_TRUE, egress);
74 result = ldp_label_mapping_send(g, s, f, (*us_attr), ds_attr);
75 if (result != MPLS_SUCCESS) {
76 if (created == MPLS_BOOL_TRUE) {
77 ldp_attr_delete(*us_attr);
79 return result;
82 if (created == MPLS_BOOL_TRUE) {
83 result = ldp_attr_insert_upstream2(g, s, (*us_attr), f);
84 if (result != MPLS_SUCCESS) {
85 ldp_attr_delete(*us_attr);
86 return result;
91 * If we have a downstream mapping (not neccessarily installed) and
92 * the downstream and upstream session are not the same....
94 if (ds_attr && ((*us_attr)->session->index != ds_attr->session->index)) {
95 /* then link the attra */
96 ldp_attr_add_us2ds((*us_attr), ds_attr);
98 /* if we just created the upstream, and we have install the
99 * downstream, then cross connect them */
100 if ((created == MPLS_BOOL_TRUE) && ds_attr->outlabel) {
102 if ((*us_attr)->inlabel->outlabel) {
104 * if we use an existing upstream mapping (in ldp_label_mapping_send())
105 * the inlabel will already be be connected to an outlabel;
107 MPLS_ASSERT((*us_attr)->inlabel->outlabel == ds_attr->outlabel);
108 } else {
109 LDP_TRACE_LOG(g->user_data,MPLS_TRACE_STATE_ALL,LDP_TRACE_FLAG_BINDING,
110 "Cross Connect Added for %08x/%d from %s -> %s\n",
111 f->info.u.prefix.network.u.ipv4, f->info.u.prefix.length,
112 (*us_attr)->session->session_name, ds_attr->session->session_name);
114 result = ldp_inlabel_add_outlabel(g,(*us_attr)->inlabel,
115 ds_attr->outlabel);
116 if (result != MPLS_SUCCESS) {
117 return result;
122 return MPLS_SUCCESS;
125 ldp_session *ldp_get_next_hop_session_for_fec2(ldp_fec * f, ldp_nexthop *nh) {
126 ldp_session *session = NULL;
128 * find the info about the next hop for this FEC
130 if (nh->addr && nh->addr->session) {
131 session = nh->addr->session;
132 } else if (nh->iff && nh->iff->is_p2p == MPLS_BOOL_TRUE &&
133 nh->iff->entity) {
134 ldp_adj *adj = MPLS_LIST_HEAD(&nh->iff->entity->adj_root);
135 session = adj ? adj->session : NULL;
137 return session;
140 mpls_return_enum ldp_get_next_hop_session_for_fec(ldp_global * g,
141 mpls_fec * fec, mpls_nexthop *nh, ldp_session ** next_hop_session)
143 ldp_fec *f = NULL;
144 ldp_nexthop *n = NULL;
146 MPLS_ASSERT(next_hop_session);
148 if (!(f = ldp_fec_find(g, fec))) {
149 return MPLS_NO_ROUTE;
152 if (!(n = ldp_fec_nexthop_find(f, nh))) {
153 return MPLS_NO_ROUTE;
156 *next_hop_session = ldp_get_next_hop_session_for_fec2(f,n);
157 return (*next_hop_session) ? MPLS_SUCCESS : MPLS_FAILURE;
160 mpls_return_enum Check_Received_Attributes(ldp_global * g, ldp_session * s,
161 ldp_attr * r_attr, uint16_t type)
163 int count = 0;
164 int i;
166 if (!r_attr->hopCountTlvExists) { /* CRa.1 */
167 goto Check_Received_Attributes_5;
170 if (r_attr->hopCountTlv.hcValue >= s->cfg_hop_count_limit) { /* CRa.2 */
171 LDP_PRINT(g->user_data, "CRa.2\n");
172 goto Check_Received_Attributes_6;
175 if (!r_attr->pathVecTlvExists) { /* CRa.3 */
176 goto Check_Received_Attributes_5;
179 for (i = 0; i < MPLS_MAXHOPSNUMBER; i++) { /* CRa.4 */
180 if (r_attr->pathVecTlv.lsrId[i]) {
181 count++;
182 if (r_attr->pathVecTlv.lsrId[i] == g->lsr_identifier.u.ipv4) {
183 goto Check_Received_Attributes_6;
184 LDP_PRINT(g->user_data, "CRa.4a\n");
186 if (count > s->oper_path_vector_limit) {
187 goto Check_Received_Attributes_6;
188 LDP_PRINT(g->user_data, "CRa.4b\n");
193 Check_Received_Attributes_5:
194 return MPLS_SUCCESS;
196 Check_Received_Attributes_6:
197 if (type != MPLS_LBLMAP_MSGTYPE) {
198 ldp_notif_send(g, s, r_attr, LDP_NOTIF_LOOP_DETECTED); /* CRa.7 */
200 return MPLS_FAILURE; /* CRa.8 */
203 void Prepare_Label_Mapping_Attributes(ldp_global * g, ldp_session * s,
204 mpls_fec * fec, ldp_attr * r_attr, ldp_attr * s_attr, mpls_bool propogating,
205 mpls_bool already, mpls_bool egress)
207 ldp_attr dummy;
208 int i;
210 /* NOTE: PMpA.21 is the end of the procedure (ie return) */
211 /* this function uses goto quite extensivly for a REASON!! */
212 /* Check Appedix A of the LDP draft */
214 LDP_ENTER(g->user_data, "Prepare_Label_Mapping_Attributes");
216 if (!r_attr) {
217 memset(&dummy, 0, sizeof(ldp_attr));
218 mpls_fec2fec_tlv(fec, &dummy.fecTlv, 0);
219 dummy.fecTlvExists = 1;
220 dummy.fecTlv.numberFecElements = 1;
221 r_attr = &dummy;
224 if (!(s->oper_loop_detection == LDP_LOOP_HOPCOUNT ||
225 s->oper_loop_detection == LDP_LOOP_HOPCOUNT_PATHVECTOR ||
226 r_attr->hopCountTlvExists)) { /* PMpA.1 */
227 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
228 return;
231 if (egress) {/* PMpA.2 */
232 /* I'm egress (for now) */
233 s_attr->hopCountTlvExists = 1;
234 s_attr->hopCountTlv.hcValue = 1; /* PMpA.3 */
235 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
236 return;
239 if (!(r_attr->hopCountTlvExists)) { /* PMpA.4 */
240 goto Prepare_Label_Mapping_Attributes_8;
243 if (!(g->ttl_less_domain == MPLS_BOOL_TRUE &&
244 s->cfg_remote_in_ttl_less_domain == MPLS_BOOL_TRUE)) { /* PMpA.5 */
245 goto Prepare_Label_Mapping_Attributes_7;
248 s_attr->hopCountTlvExists = 1;
249 s_attr->hopCountTlv.hcValue = 1; /* PMpA.6 */
250 goto Prepare_Label_Mapping_Attributes_9;
252 Prepare_Label_Mapping_Attributes_7:
253 s_attr->hopCountTlvExists = 1;
254 s_attr->hopCountTlv.hcValue = (r_attr->hopCountTlv.hcValue) ?
255 (r_attr->hopCountTlv.hcValue + 1) : 0;
256 goto Prepare_Label_Mapping_Attributes_9;
258 Prepare_Label_Mapping_Attributes_8:
259 s_attr->hopCountTlvExists = 1;
260 s_attr->hopCountTlv.hcValue = 0;
262 Prepare_Label_Mapping_Attributes_9:
263 if (s->oper_loop_detection == LDP_LOOP_NONE) {
264 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
265 return;
268 if (r_attr->pathVecTlvExists) { /* PMpA.10 */
269 goto Prepare_Label_Mapping_Attributes_19;
272 if (propogating == MPLS_BOOL_FALSE) { /* PMpA.11 */
273 goto Prepare_Label_Mapping_Attributes_20;
276 if (g->label_merge != MPLS_BOOL_TRUE) { /* PMpA.12 */
277 goto Prepare_Label_Mapping_Attributes_14;
280 if (already == MPLS_BOOL_FALSE) { /* PMpA.13 */
281 goto Prepare_Label_Mapping_Attributes_20;
284 Prepare_Label_Mapping_Attributes_14:
285 if (!r_attr->hopCountTlvExists) {
286 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
287 return;
290 if (r_attr->hopCountTlv.hcValue == 0) { /* PMpA.15 */
291 goto Prepare_Label_Mapping_Attributes_20;
294 if (already == MPLS_BOOL_FALSE) { /* PMpA.16 */
295 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
296 return;
299 /* r_attr contain PrevHopCount _IF_ we had one */
300 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
301 return; /* PMpA.17 */
303 if (r_attr->hopCountTlv.hcValue != 0) { /* PMpA.18 */
304 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
305 return;
308 Prepare_Label_Mapping_Attributes_19:
309 s_attr->pathVecTlvExists = 1;
310 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
311 for (i = 1; i < (MPLS_MAXHOPSNUMBER - 1); i++) {
312 if (r_attr->pathVecTlv.lsrId[i - 1]) {
313 s_attr->pathVecTlv.lsrId[0] = r_attr->pathVecTlv.lsrId[i - 1];
317 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
318 return;
320 Prepare_Label_Mapping_Attributes_20:
321 s_attr->pathVecTlvExists = 1;
322 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
324 LDP_EXIT(g->user_data, "Prepare_Label_Mapping_Attributes");
325 return;
328 void map2attr(mplsLdpLblMapMsg_t * map, ldp_attr * attr, uint32_t flag)
330 attr->msg_id = map->baseMsg.msgId;
332 if (map->fecTlvExists && flag & LDP_ATTR_FEC) {
333 memcpy(&attr->fecTlv, &map->fecTlv, sizeof(mplsLdpFecTlv_t));
334 attr->fecTlvExists = 1;
336 if (map->genLblTlvExists && flag & LDP_ATTR_LABEL) {
337 memcpy(&attr->genLblTlv, &map->genLblTlv, sizeof(mplsLdpGenLblTlv_t));
338 attr->genLblTlvExists = 1;
339 } else if (map->atmLblTlvExists && flag & LDP_ATTR_LABEL) {
340 memcpy(&attr->atmLblTlv, &map->atmLblTlv, sizeof(mplsLdpAtmLblTlv_t));
341 attr->atmLblTlvExists = 1;
342 } else if (map->frLblTlvExists && flag & LDP_ATTR_LABEL) {
343 memcpy(&attr->frLblTlv, &map->frLblTlv, sizeof(mplsLdpFrLblTlv_t));
344 attr->frLblTlvExists = 1;
346 if (map->hopCountTlvExists && flag & LDP_ATTR_HOPCOUNT) {
347 memcpy(&attr->hopCountTlv, &map->hopCountTlv, sizeof(mplsLdpHopTlv_t));
348 attr->hopCountTlvExists = 1;
350 if (map->pathVecTlvExists && flag & LDP_ATTR_PATH) {
351 memcpy(&attr->pathVecTlv, &map->pathVecTlv, sizeof(mplsLdpPathTlv_t));
352 attr->pathVecTlvExists = 1;
354 if (map->lblMsgIdTlvExists && flag & LDP_ATTR_MSGID) {
355 memcpy(&attr->lblMsgIdTlv, &map->lblMsgIdTlv, sizeof(mplsLdpLblMsgIdTlv_t));
356 attr->lblMsgIdTlvExists = 1;
358 if (map->lspidTlvExists && flag & LDP_ATTR_LSPID) {
359 memcpy(&attr->lspidTlv, &map->lspidTlv, sizeof(mplsLdpLspIdTlv_t));
360 attr->lspidTlvExists = 1;
362 if (map->trafficTlvExists && flag & LDP_ATTR_TRAFFIC) {
363 memcpy(&attr->trafficTlv, &map->trafficTlv, sizeof(mplsLdpTrafficTlv_t));
364 attr->trafficTlvExists = 1;
368 void attr2map(ldp_attr * attr, mplsLdpLblMapMsg_t * map)
370 if (attr->fecTlvExists) {
371 memcpy(&map->fecTlv, &attr->fecTlv, sizeof(mplsLdpFecTlv_t));
372 map->fecTlvExists = 1;
374 if (attr->genLblTlvExists) {
375 memcpy(&map->genLblTlv, &attr->genLblTlv, sizeof(mplsLdpGenLblTlv_t));
376 map->genLblTlvExists = 1;
378 if (attr->atmLblTlvExists) {
379 memcpy(&map->atmLblTlv, &attr->atmLblTlv, sizeof(mplsLdpAtmLblTlv_t));
380 map->atmLblTlvExists = 1;
382 if (attr->frLblTlvExists) {
383 memcpy(&map->frLblTlv, &attr->frLblTlv, sizeof(mplsLdpFrLblTlv_t));
384 map->frLblTlvExists = 1;
386 if (attr->hopCountTlvExists) {
387 memcpy(&map->hopCountTlv, &attr->hopCountTlv, sizeof(mplsLdpHopTlv_t));
388 map->hopCountTlvExists = 1;
390 if (attr->pathVecTlvExists) {
391 memcpy(&map->pathVecTlv, &attr->pathVecTlv, sizeof(mplsLdpPathTlv_t));
392 map->pathVecTlvExists = 1;
394 if (attr->lblMsgIdTlvExists) {
395 memcpy(&map->lblMsgIdTlv, &attr->lblMsgIdTlv, sizeof(mplsLdpLblMsgIdTlv_t));
396 map->lblMsgIdTlvExists = 1;
398 if (attr->lspidTlvExists) {
399 memcpy(&map->lspidTlv, &attr->lspidTlv, sizeof(mplsLdpLspIdTlv_t));
400 map->lspidTlvExists = 1;
402 if (attr->trafficTlvExists) {
403 memcpy(&map->trafficTlv, &attr->trafficTlv, sizeof(mplsLdpTrafficTlv_t));
404 map->trafficTlvExists = 1;
408 void ldp_label_mapping_initial_callback(mpls_timer_handle timer, void *extra,
409 mpls_cfg_handle handle)
411 ldp_session *s = (ldp_session *) extra;
412 ldp_global *g = (ldp_global*)handle;
413 ldp_attr *ds_attr = NULL;
414 ldp_attr *us_attr = NULL;
415 ldp_session *nh_session = NULL;
416 mpls_bool done = MPLS_BOOL_FALSE;
417 ldp_fec *f;
418 ldp_nexthop *nh;
420 LDP_ENTER(g->user_data, "ldp_label_mapping_initial_callback");
422 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_TIMER,
423 "Initial Label Mapping fired: session(%d)\n", s->index);
425 mpls_lock_get(g->global_lock);
427 mpls_timer_stop(g->timer_handle, timer);
429 f = MPLS_LIST_HEAD(&g->fec);
430 while (f) {
431 nh = MPLS_LIST_HEAD(&f->nh_root);
432 while (nh) {
433 switch (f->info.type) {
434 case MPLS_FEC_PREFIX:
435 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
436 LDP_TRACE_FLAG_ROUTE, "Processing prefix FEC: %08x/%d ",
437 f->info.u.prefix.network.u.ipv4, f->info.u.prefix.length);
438 break;
439 case MPLS_FEC_HOST:
440 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
441 LDP_TRACE_FLAG_ROUTE, "Processing host FEC: %08x ",
442 f->info.u.host.u.ipv4);
443 break;
444 case MPLS_FEC_L2CC:
445 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
446 LDP_TRACE_FLAG_ROUTE, "Processingu L2CC FEC: %d %d %d ",
447 f->info.u.l2cc.connection_id, f->info.u.l2cc.group_id,
448 f->info.u.l2cc.type);
449 break;
450 default:
451 MPLS_ASSERT(0);
454 if (nh->info.type & MPLS_NH_IP) {
455 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
456 LDP_TRACE_FLAG_ROUTE, "via %08x\n", nh->addr->address.u.ipv4);
458 if (nh->info.type & MPLS_NH_IF && nh->iff) {
459 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
460 LDP_TRACE_FLAG_ROUTE, "via %p\n", nh->iff->handle);
463 /* are we allowed to export this route from the rib */
464 if (mpls_policy_export_check(g->user_data, &f->info, &nh->info) ==
465 MPLS_BOOL_FALSE) {
466 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
467 LDP_TRACE_FLAG_POLICY, "Rejected by export policy\n");
468 goto ldp_label_mapping_initial_callback_end_nh;
471 /* have we already sent a mapping for this fec to the new session? */
472 if ((us_attr = ldp_attr_find_upstream_state2(g, s, f,
473 LDP_LSP_STATE_MAP_SENT))) {
474 /* no need to sent another mapping */
475 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
476 LDP_TRACE_FLAG_ROUTE, "Already sent this FEC to session %d\n",
477 s->index);
478 goto ldp_label_mapping_initial_callback_end_nh;
481 if (!(nh_session = ldp_get_next_hop_session_for_fec2(f,nh))) {
482 ds_attr = NULL;
483 } else {
484 if (nh_session->index == s->index) {
485 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
486 LDP_TRACE_FLAG_ROUTE, "Nexthop session(%d) == session(%d)\n",
487 nh_session->index, s->index);
488 goto ldp_label_mapping_initial_callback_end_nh;
490 ds_attr = ldp_attr_find_downstream_state2(g, nh_session, f,
491 LDP_LSP_STATE_MAP_RECV);
494 if ((g->label_merge != MPLS_BOOL_TRUE) &&
495 ldp_attr_num_us2ds(ds_attr)) {
496 /* we have a ds label, but can't use it */
497 ds_attr = NULL;
500 us_attr = NULL;
501 if (ds_attr) {
502 /* we can use it, merge on baby */
503 ldp_label_mapping_with_xc(g, s, f, &us_attr, ds_attr);
504 } else {
505 /* we don't have a ds label */
507 /* we will be egress? */
508 if (g->lsp_control_mode == LDP_CONTROL_ORDERED) {
509 if (mpls_policy_egress_check(g->user_data, &f->info,
510 &nh->info) == MPLS_BOOL_TRUE) {
511 ldp_label_mapping_with_xc(g, s, f, &us_attr, NULL);
513 } else {
514 ldp_label_mapping_with_xc(g, s, f, &us_attr, NULL);
517 ldp_label_mapping_initial_callback_end_nh:
518 nh = MPLS_LIST_NEXT(&f->nh_root, nh, _fec);
520 f = MPLS_LIST_NEXT(&g->fec, f, _global);
522 done = MPLS_BOOL_TRUE;
524 if (done == MPLS_BOOL_TRUE) {
525 mpls_timer_delete(g->timer_handle, timer);
526 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
527 s->initial_distribution_timer = (mpls_timer_handle) 0;
528 } else {
529 mpls_timer_start(g->timer_handle, timer, MPLS_TIMER_ONESHOT);
530 /* need to mark the session with where it left off */
533 mpls_lock_release(g->global_lock);
535 LDP_EXIT(g->user_data, "ldp_label_mapping_initial_callback");
538 mpls_return_enum ldp_label_mapping_send(ldp_global * g, ldp_session * s,
539 ldp_fec *f, ldp_attr * us_attr, ldp_attr * ds_attr)
541 ldp_inlabel *in = NULL;
542 ldp_attr *us_temp, *existing = NULL;
544 LDP_ENTER(g->user_data, "ldp_label_mapping_send");
545 MPLS_ASSERT(us_attr);
547 #if 0
549 * before we can enable this, inlabels need to keep track of all of
550 * the attr that link to it. Then when running in DU independent mode we
551 * can correctly attach the us and ds attrs involved when propogating a
552 * new mapping for a FEC we've already distributed labels for
554 existing = ldp_attr_find_upstream_map_in_labelspace(f, s->cfg_label_space);
555 #endif
557 if (existing) {
558 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_BINDING,
559 "Using an existing label\n");
560 in = existing->inlabel;
561 ldp_attr_add_inlabel(us_attr, in);
562 } else {
563 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_BINDING,
564 "Generating a label\n");
565 in = ldp_inlabel_create_complete(g, s, us_attr);
568 if (!in) { /* SL.1-3 */
569 goto Send_Label_9;
572 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_BINDING,
573 "In Label Added\n");
575 us_attr->state = LDP_LSP_STATE_MAP_SENT;
577 us_attr->msg_id = g->message_identifier;
578 ldp_label_mapping_prepare_msg(s->tx_message, g->message_identifier++,
579 us_attr);
581 if (ldp_mesg_send_tcp(g, s, s->tx_message) != MPLS_SUCCESS) { /* SL.4 */
582 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ERROR,
583 "Failed sending Label Mapping to %s\n",
584 s->session_name);
585 goto ldp_label_mapping_send_error;
588 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_LABEL,
589 "Label Mapping Sent to %s for %08x/%d\n",
590 s->session_name,
591 us_attr->fecTlv.fecElArray[0].addressEl.address,
592 us_attr->fecTlv.fecElArray[0].addressEl.preLen);
594 us_attr->state = LDP_LSP_STATE_MAP_SENT; /* SL.6,7 */
596 LDP_EXIT(g->user_data, "ldp_label_mapping_send");
597 return MPLS_SUCCESS; /* SL.8 */
599 Send_Label_9:
600 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_STATE,
601 "No Label Resources\n");
603 while ((us_temp = ldp_attr_find_upstream_state2(g, s, us_attr->fec,
604 LDP_LSP_STATE_REQ_RECV)) != NULL) { /* SL.9 */
605 ldp_notif_send(g, s, us_temp, LDP_NOTIF_NO_LABEL_RESOURCES_AVAILABLE);
606 /* SL.10 */
607 s->no_label_resource_sent = MPLS_BOOL_TRUE; /* SL.12 */
608 us_temp->state = LDP_LSP_STATE_NO_LABEL_RESOURCE_SENT; /* SL.13 */
611 LDP_EXIT(g->user_data, "ldp_label_mapping_send");
613 return MPLS_SUCCESS;
615 ldp_label_mapping_send_error:
617 LDP_EXIT(g->user_data, "ldp_label_mapping_send-error");
618 return MPLS_FAILURE;
621 void ldp_label_mapping_prepare_msg(ldp_mesg * msg, uint32_t msgid,
622 ldp_attr * s_attr)
624 mplsLdpLblMapMsg_t *map = NULL;
625 int i;
627 MPLS_ASSERT(msg);
629 ldp_mesg_prepare(msg, MPLS_LBLMAP_MSGTYPE, msgid);
630 map = &msg->u.map;
632 if (s_attr->fecTlvExists) {
633 /* JLEU: only 1 FEC is allowed!! */
634 map->fecTlvExists = 1;
635 map->baseMsg.msgLength += setupFecTlv(&map->fecTlv);
636 map->baseMsg.msgLength += addFecElem2FecTlv(&map->fecTlv,
637 &s_attr->fecTlv.fecElArray[0]);
639 if (s_attr->genLblTlvExists) {
640 map->genLblTlvExists = 1;
641 map->baseMsg.msgLength += setupGenLblTlv(&map->genLblTlv,
642 s_attr->genLblTlv.label);
644 if (s_attr->atmLblTlvExists) {
645 map->atmLblTlvExists = 1;
646 map->baseMsg.msgLength += setupAtmLblTlv(&map->atmLblTlv, 0, 0,
647 s_attr->atmLblTlv.flags.flags.vpi, s_attr->atmLblTlv.vci);
649 if (s_attr->frLblTlvExists) {
650 map->frLblTlvExists = 1;
651 map->baseMsg.msgLength += setupFrLblTlv(&map->frLblTlv, 0,
652 s_attr->frLblTlv.flags.flags.len, s_attr->frLblTlv.flags.flags.dlci);
654 if (s_attr->hopCountTlvExists) {
655 map->hopCountTlvExists = 1;
656 map->baseMsg.msgLength += setupHopCountTlv(&map->hopCountTlv,
657 s_attr->hopCountTlv.hcValue);
659 if (s_attr->pathVecTlvExists) {
660 map->pathVecTlvExists = 1;
661 map->baseMsg.msgLength += setupPathTlv(&map->pathVecTlv);
662 for (i = 0; i < MPLS_MAXHOPSNUMBER; i++) {
663 if (s_attr->pathVecTlv.lsrId[i]) {
664 map->baseMsg.msgLength += addLsrId2PathTlv(&map->pathVecTlv,
665 s_attr->pathVecTlv.lsrId[i]);
669 #if 0
670 if (s_attr->lblMsgIdTlvExists) {
672 if (s_attr->lspidTlvExists) {
674 if (s_attr->trafficTlvExists) {
676 #endif
679 mpls_return_enum ldp_label_mapping_process(ldp_global * g, ldp_session * s,
680 ldp_adj * a, ldp_entity * e, ldp_attr * r_attr, ldp_fec * f)
682 mpls_return_enum retval = MPLS_SUCCESS;
683 ldp_session *peer = NULL;
684 ldp_attr_list *us_list = NULL;
685 ldp_attr_list *ds_list = NULL;
686 ldp_attr *ds_attr = NULL;
687 ldp_attr *ds_temp = NULL;
688 ldp_attr *us_attr = NULL;
689 ldp_attr *us_temp = NULL;
690 ldp_attr dumb_attr;
691 ldp_nexthop *nh = NULL;
693 ldp_outlabel *out = NULL;
694 mpls_bool requested = MPLS_BOOL_FALSE;
695 ldp_attr *existing = NULL;
696 mpls_bool need_request = MPLS_BOOL_FALSE;
698 LDP_ENTER(g->user_data, "ldp_label_mapping_process");
700 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_LABEL,
701 "Label Mapping Recv from %s for %08x/%d\n",
702 s->session_name,
703 r_attr->fecTlv.fecElArray[0].addressEl.address,
704 r_attr->fecTlv.fecElArray[0].addressEl.preLen);
706 if ((ds_attr = ldp_attr_find_downstream_state2(g, s, f,
707 LDP_LSP_STATE_REQ_SENT)) != NULL) { /* LMp.1 */
708 /* just remove the req from the tree, we will use the r_attr sent to us */
709 ldp_attr_delete_downstream(g, s, ds_attr);
710 requested = MPLS_BOOL_TRUE;
711 } else {
712 requested = MPLS_BOOL_FALSE;
715 ds_attr = r_attr;
716 ds_attr->state = LDP_LSP_STATE_MAP_RECV; /* LMp.2 */
719 * ds_attr is the mapping we will keep and is NOT in the tree, unless
720 * it is an update mapping ...
722 if (Check_Received_Attributes(g, s, ds_attr, MPLS_LBLMAP_MSGTYPE) ==
723 MPLS_SUCCESS) { /* LMp.3 */
724 goto LMp_9;
728 * A loop was detected
730 if ((ds_list = ldp_attr_find_downstream_all2(g, s, f))) {
731 ds_temp = MPLS_LIST_HEAD(ds_list);
733 * check all the labels this session has received from "s" for "fec"
734 * do we have a duplicat?
736 while (ds_temp) {
737 if ((ds_temp->state == LDP_LSP_STATE_MAP_RECV) && /* LMp.4 */
738 ldp_attr_is_equal(ds_temp, ds_attr, LDP_ATTR_LABEL) == /* LMp.5 */
739 MPLS_BOOL_TRUE) {
740 /* remove record of the label and remove it switching */
741 ldp_attr_remove_complete(g, ds_temp, MPLS_BOOL_TRUE); /* LMp.6,7 */
743 * I think this is supposed to be 32 NOT 33, we need to release
744 * it don't we?
746 goto LMp_33;
748 ds_temp = MPLS_LIST_NEXT(ds_list, ds_temp, _fs);
752 LDP_PRINT(g->user_data, "Receive_Label_Map_8: send release");
753 if (ldp_label_release_send(g, s, ds_attr, LDP_NOTIF_LOOP_DETECTED) !=
754 MPLS_SUCCESS) { /* LMp.8 */
755 retval = MPLS_FAILURE;
757 goto LMp_33;
759 LMp_9:
761 * No Loop Detected
763 ds_temp = ldp_attr_find_downstream_state2(g, s, f, LDP_LSP_STATE_MAP_RECV);
764 if (requested == MPLS_BOOL_TRUE ||
765 g->label_merge == MPLS_BOOL_FALSE || !ds_temp) {
766 /* !merging then this is always a new LSP
767 * merging w/o a recv'd mapping is a new LSP
768 * this check comes from Note 6
770 goto LMp_11;
773 /* searching all recv'd attrs for matched mappings,
774 * stop after finding 1st match
776 if ((ds_list = ldp_attr_find_downstream_all2(g, s, f))) {
777 ds_temp = MPLS_LIST_HEAD(ds_list);
778 while (ds_temp) {
779 if (ds_temp->state == LDP_LSP_STATE_MAP_RECV) { /* LMp.9 */
780 if (ldp_attr_is_equal(ds_attr, ds_temp, LDP_ATTR_LABEL) ==
781 MPLS_BOOL_TRUE) { /* LMp.10 */
783 * this mapping matches an existing mapping, but it
784 * could contain updated attributes
786 existing = ds_temp;
787 break;
788 } else {
790 * we have been given another label for the same FEC and we
791 * didn't request it, release it
793 LDP_PRINT(g->user_data, "LMp.10 dup without req\n");
794 goto LMp_32;
797 ds_temp = MPLS_LIST_NEXT(ds_list, ds_temp, _fs);
800 if (existing) {
801 ldp_attr2ldp_attr(ds_attr, existing, LDP_ATTR_HOPCOUNT | LDP_ATTR_PATH |
802 LDP_ATTR_MSGID | LDP_ATTR_LSPID | LDP_ATTR_TRAFFIC);
803 ds_attr = existing;
805 * no need to free ds_attr, since it was not added to the tree it
806 * will be deleted when we exit ldp_label_mapping_process(), see
807 * ldp_state_process().
811 * from this point on.... if this is an updated mapping then ds_attr
812 * is the existing mapping which has now been update, else ds_attr
813 * is the new mapping
816 LMp_11:
818 * existing ONLY has a value for updated label mapping
820 nh = ldp_nexthop_for_fec_session(f,s); /* LMp.11 */
823 * the following departs from the procedure, it allows for filtering
824 * of label mappings
826 * Are we configured to accept and INSTALL this mapping?
828 if (mpls_policy_import_check(g->user_data, &f->info, &nh->info) ==
829 MPLS_BOOL_FALSE) {
831 * policy has rejected it, store it away
833 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_LABEL,
834 "Label Mapping for %08x/%d from %s filtered by import policy\n",
835 r_attr->fecTlv.fecElArray[0].addressEl.address,
836 r_attr->fecTlv.fecElArray[0].addressEl.preLen, s->session_name);
838 if (existing) {
839 ds_attr->filtered = MPLS_BOOL_TRUE;
840 if (ds_attr->outlabel && ds_attr->outlabel->switching == MPLS_BOOL_TRUE) {
841 /* the mapping has been filtered, but the original wasn't? */
842 MPLS_ASSERT(0);
844 } else {
845 ds_attr->filtered = MPLS_BOOL_TRUE;
846 if (ldp_attr_insert_downstream(g, s, ds_attr) != MPLS_SUCCESS) {
847 retval = MPLS_FAILURE;
850 goto LMp_33;
853 if (!nh) { /* LMp.12 */
855 * if we did not find a nh hop for this FEC that corresponded to the
856 * MsgSource then the MsgSource is not a nexthop for the FEC
858 if (g->label_retention_mode == LDP_RETENTION_CONSERVATIVE) { /* LMp.13C */
859 LDP_PRINT(g->user_data, "LMp.13C conservative\n");
860 goto LMp_32;
864 * store it away
866 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_LABEL,
867 "Session %s is not a valid nexthop for %08x/%d\n", s->session_name,
868 r_attr->fecTlv.fecElArray[0].addressEl.address,
869 r_attr->fecTlv.fecElArray[0].addressEl.preLen);
871 if (!existing) {
872 /* LMp.13L */
873 if (ldp_attr_insert_downstream(g, s, ds_attr) != MPLS_SUCCESS) {
874 retval = MPLS_FAILURE;
877 goto LMp_33;
881 * this is slightly different form the procedure, we can still be
882 * transit for a FEC we are not configured to be ingress for.
883 * Either way we only need to do the "install for fwd/switching"
884 * only once. We could arrive here multiple times due to updates,
885 * only install it the first time
887 if ((!existing) || (!existing->outlabel)) {
889 * we haven't installed it yet.
890 * Either new (!existing), or a result of a "Detect FEC Nexthop Change"
891 * and we had this mapping in our database (!existing->outlabel))
894 if (!(out = ldp_outlabel_create_complete(g, s, ds_attr, nh))) {
895 LDP_PRINT(g->user_data, "LMp.15 failure creating outlabel\n");
896 goto LMp_32;
899 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_BINDING,
900 "Out Label Added\n");
904 * are we configured to act as ingress for this FEC?
906 if (mpls_policy_ingress_check(g->user_data, &f->info, &nh->info) ==
907 MPLS_BOOL_TRUE) { /* LMp.14 */
909 * yep, bind the label to the FEC
911 if (ds_attr->ingress != MPLS_BOOL_TRUE) {
912 #if MPLS_USE_LSR
913 lsr_ftn ftn;
914 ftn.outsegment_index = ds_attr->outlabel->info.handle;
915 memcpy(&ftn.fec, &f->info, sizeof(mpls_fec));
916 lsr_cfg_ftn_set2(g->lsr_handle, &ftn, LSR_CFG_ADD|LSR_FTN_CFG_FEC|
917 LSR_FTN_CFG_OUTSEGMENT);
918 #else
919 mpls_mpls_fec2out_add(g->mpls_handle, &f->info, &ds_attr->outlabel->info);
920 #endif
921 ds_attr->ingress = MPLS_BOOL_TRUE;
922 ds_attr->outlabel->merge_count++;
923 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_BINDING,
924 "Acting as ingress for %08x/%d from %s\n",
925 r_attr->fecTlv.fecElArray[0].addressEl.address,
926 r_attr->fecTlv.fecElArray[0].addressEl.preLen, s->session_name);
930 /* create a set of attrs that we will fill and compare against
931 * if this mapping were to be propogate these are the attrs it would have
932 * by comparing what we did sent in the past to these, we con figure out
933 * if we need to send an updated mapping
935 memset(&dumb_attr, 0, sizeof(ldp_attr));
936 mpls_fec2fec_tlv(&f->info, &dumb_attr.fecTlv, 0);
937 dumb_attr.fecTlvExists = 1;
938 dumb_attr.fecTlv.numberFecElements = 1;
941 * by definition (we received a label mapping that will be used) this
942 * LSR is _not_ the egress, so calculate a hop and path based on the
943 * mapping we received. We will compare this with mapping that have
944 * already been sent. If they differ, we will send an updated mapping
946 Prepare_Label_Mapping_Attributes(g, s, &f->info, ds_attr, &dumb_attr,
947 MPLS_BOOL_TRUE, MPLS_BOOL_TRUE, MPLS_BOOL_FALSE);
949 if (!existing) {
951 * this is the first time we've seen this mapping, add it to the database.
952 * all future updates will modify this entry in place
954 /* LMp.16 */
955 if (ldp_attr_insert_downstream(g, s, ds_attr) != MPLS_SUCCESS) {
956 retval = MPLS_FAILURE;
957 goto LMp_33;
961 peer = MPLS_LIST_HEAD(&g->session);
962 while (peer) { /* LMp.17 */
964 if (peer->state != LDP_STATE_OPERATIONAL) {
965 goto next_peer;
969 * it is just as easy to walk the list of all upstream attr for this
970 * peer as it is to the individual check to see if we have sent a
971 * label mapping for this FEC LSP
974 /* LMp.22 - 27 */
975 if ((us_list = ldp_attr_find_upstream_all2(g, peer, f))) {
976 us_temp = MPLS_LIST_HEAD(us_list);
977 while (us_temp) {
979 * if we have sent a label mapping for the FEC and that label mapping
980 * was an done in independent mode or it is part of an LSP created
981 * due as part of an existing received label mapping
983 /* LMp.18 */
984 if (us_temp->state == LDP_LSP_STATE_MAP_SENT) {
986 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV,
987 LDP_TRACE_FLAG_BINDING, "Already sent mapping for %08x/%d to %s\n",
988 r_attr->fecTlv.fecElArray[0].addressEl.address,
989 r_attr->fecTlv.fecElArray[0].addressEl.preLen, peer->session_name);
991 if ((!existing) || (existing->index == us_temp->ds_attr->index)) {
993 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV,
994 LDP_TRACE_FLAG_BINDING, "Part of same LSP\n");
996 /* LMp.23 */
997 if (ldp_attr_is_equal(us_temp, &dumb_attr,
998 LDP_ATTR_HOPCOUNT | LDP_ATTR_PATH) != MPLS_BOOL_TRUE) {
1000 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV,
1001 LDP_TRACE_FLAG_BINDING, "Propogating updated attrs\n");
1003 /* send an updated label mapping */
1004 if (ldp_label_mapping_with_xc(g, us_temp->session, f, &us_temp,
1005 ds_attr) != MPLS_SUCCESS) { /* LMp.24-26 */
1006 retval = MPLS_FAILURE;
1007 goto LMp_33;
1012 us_temp = MPLS_LIST_NEXT(us_list, us_temp, _fs);
1016 if ((peer->oper_distribution_mode == LDP_DISTRIBUTION_UNSOLICITED) &&
1017 (g->lsp_control_mode == LDP_CONTROL_ORDERED)) { /* LMp.19 */
1020 * if we're not merging and we have multiple ORDERED DU sessions,
1021 * we will need to start requesting labels after we propogate the
1022 * mapping to the first peer
1024 if (need_request == MPLS_BOOL_TRUE) {
1025 if (ldp_attr_find_downstream_state2(g, peer, f,
1026 LDP_LSP_STATE_REQ_SENT) == NULL) {
1028 * we don't have a request for FEC to peer outstanding, make one
1030 ds_temp = NULL;
1031 if (ldp_label_request_for_xc(g, peer, &f->info, NULL, &ds_temp) !=
1032 MPLS_SUCCESS) {
1033 retval = MPLS_FAILURE;
1034 goto LMp_33;
1037 } else {
1039 * We're in DU more, either we're merging, or we're not merging and
1040 * this is the first peer we're propogating this mapping to
1042 /* LMp.20-21,30 */
1043 us_attr = NULL;
1044 if (ldp_label_mapping_with_xc(g, peer, f, &us_attr, ds_attr) !=
1045 MPLS_SUCCESS) {
1046 retval = MPLS_FAILURE;
1047 goto LMp_33;
1050 * if we're not merging, we will need to request a label for
1051 * the next DU peer
1053 if (g->label_merge == MPLS_BOOL_FALSE) {
1054 need_request = MPLS_BOOL_TRUE;
1059 /* LMp.28 */
1060 while ((us_temp = ldp_attr_find_upstream_state2(g, peer, f,
1061 LDP_LSP_STATE_REQ_RECV))) {
1063 if (peer->oper_distribution_mode == LDP_DISTRIBUTION_UNSOLICITED) {
1064 if (need_request == MPLS_BOOL_TRUE) {
1065 if (ldp_attr_find_downstream_state2(g, peer, f,
1066 LDP_LSP_STATE_REQ_SENT) == NULL) {
1068 * we don't have a request for FEC to peer outstanding
1070 ds_temp = NULL;
1071 if (ldp_label_request_for_xc(g, peer, &f->info, us_temp,
1072 &ds_temp) != MPLS_SUCCESS) {
1073 retval = MPLS_FAILURE;
1074 goto LMp_33;
1077 } else {
1078 if (ldp_label_mapping_with_xc(g, peer, f, &us_temp,
1079 ds_attr) != MPLS_SUCCESS) {
1080 retval = MPLS_FAILURE;
1081 goto LMp_33;
1084 } else {
1085 if ((us_list = ldp_attr_find_upstream_all2(g, peer, f))) {
1086 us_temp = MPLS_LIST_HEAD(ds_list);
1087 while (us_temp) {
1088 if (us_temp->state == LDP_LSP_STATE_REQ_RECV) {
1089 if (need_request == MPLS_BOOL_TRUE) {
1090 if (ldp_attr_find_downstream_state2(g, peer, f,
1091 LDP_LSP_STATE_REQ_SENT) == NULL) {
1093 * we don't have a request for FEC to peer outstanding
1095 ds_temp = NULL;
1096 if (ldp_label_request_for_xc(g, peer, &f->info, us_temp,
1097 &ds_temp) != MPLS_SUCCESS) {
1098 retval = MPLS_FAILURE;
1099 goto LMp_33;
1102 } else {
1103 if (ldp_label_mapping_with_xc(g, peer, f, &us_temp,
1104 ds_attr) != MPLS_SUCCESS) {
1105 retval = MPLS_FAILURE;
1106 goto LMp_33;
1109 * if we're not merging, we will need to request a label for
1110 * the next DU peer
1112 if (g->label_merge == MPLS_BOOL_FALSE) {
1113 need_request = MPLS_BOOL_TRUE;
1117 us_temp = MPLS_LIST_NEXT(us_list, us_temp, _fs);
1123 next_peer:
1124 peer = MPLS_LIST_NEXT(&g->session, peer, _global);
1127 LMp_33:
1128 LDP_EXIT(g->user_data, "ldp_label_mapping_process");
1129 return retval;
1131 LMp_32:
1132 LDP_PRINT(g->user_data, "Receive_Label_Map_32: send release");
1133 if (ldp_label_release_send(g, s, ds_attr, LDP_NOTIF_NONE) != MPLS_SUCCESS) {
1134 retval = MPLS_FAILURE;
1136 LDP_EXIT(g->user_data, "ldp_label_mapping_process");
1137 return retval;