an address can only be advertised by one session. Thus
[mpls-ldp-portable.git] / ldp / ldp_label_request.c
blob82c88bf43b3c7ccb5e2681ed486eb9193802b822
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_attr.h"
12 #include "ldp_fec.h"
13 #include "ldp_mesg.h"
14 #include "ldp_pdu_setup.h"
15 #include "ldp_notif.h"
16 #include "ldp_session.h"
17 #include "ldp_entity.h"
18 #include "ldp_label_mapping.h"
19 #include "ldp_label_request.h"
21 #include "mpls_timer_impl.h"
22 #include "mpls_policy_impl.h"
23 #include "mpls_tree_impl.h"
24 #include "mpls_trace_impl.h"
25 #include "mpls_fib_impl.h"
26 #include "mpls_lock_impl.h"
28 mpls_return_enum ldp_label_request_for_xc(ldp_global * g, ldp_session * s,
29 mpls_fec * fec, ldp_attr * us_attr, ldp_attr ** ds_attr)
32 LDP_ENTER(g->user_data, "ldp_label_request_for_xc");
34 if (!(*ds_attr)) {
35 if (!((*ds_attr) = ldp_attr_create(fec))) {
36 return MPLS_FATAL;
39 Prepare_Label_Request_Attributes(g, s, fec, (*ds_attr), us_attr);
40 (*ds_attr)->state = LDP_LSP_STATE_REQ_SENT;
41 if (ldp_label_request_send(g, s, us_attr, ds_attr) != MPLS_SUCCESS) {
42 return MPLS_FAILURE;
45 LDP_EXIT(g->user_data, "ldp_label_request_for_xc");
47 return MPLS_SUCCESS;
50 void ldp_label_request_prepare_msg(ldp_mesg * msg, uint32_t msgid,
51 ldp_attr * s_attr)
53 mplsLdpLblReqMsg_t *req = NULL;
54 int i;
56 ldp_mesg_prepare(msg, MPLS_LBLREQ_MSGTYPE, msgid);
57 req = &msg->u.request;
59 if (s_attr->fecTlvExists) {
60 req->fecTlvExists = 1;
61 req->baseMsg.msgLength += setupFecTlv(&req->fecTlv);
62 req->baseMsg.msgLength += addFecElem2FecTlv(&req->fecTlv,
63 &s_attr->fecTlv.fecElArray[0]);
65 if (s_attr->hopCountTlvExists) {
66 req->hopCountTlvExists = 1;
67 req->baseMsg.msgLength += setupHopCountTlv(&req->hopCountTlv,
68 s_attr->hopCountTlv.hcValue);
70 if (s_attr->pathVecTlvExists) {
71 req->pathVecTlvExists = 1;
72 req->baseMsg.msgLength += setupPathTlv(&req->pathVecTlv);
73 for (i = 0; i < MPLS_MAXHOPSNUMBER; i++) {
74 if (s_attr->pathVecTlv.lsrId[i]) {
75 req->baseMsg.msgLength += addLsrId2PathTlv(&req->pathVecTlv,
76 s_attr->pathVecTlv.lsrId[i]);
82 mpls_return_enum ldp_label_request_send(ldp_global * g, ldp_session * s,
83 ldp_attr * us_attr, ldp_attr ** ds_attr)
85 ldp_attr *ds_temp;
86 mpls_fec fec;
88 LDP_ENTER(g->user_data, "ldp_label_request_send");
89 MPLS_ASSERT(ds_attr && *ds_attr);
91 fec_tlv2mpls_fec(&((*ds_attr)->fecTlv), 0, &fec);
93 if ((ds_temp = ldp_attr_find_downstream_state(g, s, &fec,
94 LDP_LSP_STATE_REQ_SENT)) != NULL) { /* SLRq.1 */
96 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_LABEL,
97 "Label Request Send: request already pending(%d)\n", ds_temp->index);
99 ldp_attr_add_us2ds(us_attr, ds_temp);
101 /* we do not need the one passed in, but make sure that the caller
102 is using this one from here forth */
103 ldp_attr_remove_complete(g, *ds_attr, MPLS_BOOL_TRUE);
104 *ds_attr = ds_temp;
105 return MPLS_SUCCESS;
108 if (s->no_label_resource_recv == MPLS_BOOL_TRUE) { /* SLRq.2 */
109 goto ldp_label_request_send_error;
112 (*ds_attr)->msg_id = g->message_identifier++;
113 ldp_label_request_prepare_msg(s->tx_message, (*ds_attr)->msg_id, *ds_attr);
115 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_LABEL,
116 "Label Request Sent: session(%d)\n", s->index);
118 if (ldp_mesg_send_tcp(g, s, s->tx_message) == MPLS_FAILURE) { /* SLRq.3 */
119 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ERROR,
120 "Label Request send failed\n");
121 goto ldp_label_request_send_error;
124 (*ds_attr)->state = LDP_LSP_STATE_REQ_SENT;
125 if (ldp_attr_insert_downstream(g, s, (*ds_attr)) == MPLS_FAILURE) { /* SLRq.4 */
126 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_SEND, LDP_TRACE_FLAG_ERROR,
127 "Couldn't insert sent attributes in tree\n");
128 goto ldp_label_request_send_error;
130 if (us_attr) {
131 ldp_attr_add_us2ds(us_attr, *ds_attr);
134 LDP_EXIT(g->user_data, "ldp_label_request_send");
136 return MPLS_SUCCESS; /* SLRq.5 */
138 ldp_label_request_send_error:
140 LDP_PRINT(g->user_data, "SLRq.6\n");
141 (*ds_attr)->state = LDP_LSP_STATE_NO_LABEL_RESOURCE_SENT;
142 ldp_attr_insert_downstream(g, s, (*ds_attr)); /* SLRq.6 */
144 LDP_EXIT(g->user_data, "ldp_label_request_send-error");
146 return MPLS_FAILURE; /* SLRq.7 */
149 void req2attr(mplsLdpLblReqMsg_t * req, ldp_attr * attr, uint32_t flag)
151 attr->msg_id = req->baseMsg.msgId;
153 if (req->fecTlvExists && flag & LDP_ATTR_FEC) {
154 memcpy(&attr->fecTlv, &req->fecTlv, sizeof(mplsLdpFecTlv_t));
155 attr->fecTlvExists = 1;
157 if (req->hopCountTlvExists && flag & LDP_ATTR_HOPCOUNT) {
158 memcpy(&attr->hopCountTlv, &req->hopCountTlv, sizeof(mplsLdpHopTlv_t));
159 attr->hopCountTlvExists = 1;
161 if (req->pathVecTlvExists && flag & LDP_ATTR_PATH) {
162 memcpy(&attr->pathVecTlv, &req->pathVecTlv, sizeof(mplsLdpPathTlv_t));
163 attr->pathVecTlvExists = 1;
165 if (req->lblMsgIdTlvExists && flag & LDP_ATTR_MSGID) {
166 memcpy(&attr->lblMsgIdTlv, &req->lblMsgIdTlv, sizeof(mplsLdpLblMsgIdTlv_t));
167 attr->lblMsgIdTlvExists = 1;
169 if (req->lspidTlvExists && flag & LDP_ATTR_LSPID) {
170 memcpy(&attr->lspidTlv, &req->lspidTlv, sizeof(mplsLdpLspIdTlv_t));
171 attr->lspidTlvExists = 1;
173 if (req->trafficTlvExists && flag & LDP_ATTR_TRAFFIC) {
174 memcpy(&attr->trafficTlv, &req->trafficTlv, sizeof(mplsLdpTrafficTlv_t));
175 attr->trafficTlvExists = 1;
179 void ldp_label_request_initial_callback(mpls_timer_handle timer, void *extra,
180 mpls_cfg_handle handle)
182 ldp_session *s = (ldp_session *)extra;
183 ldp_global *g = (ldp_global*)handle;
184 ldp_nexthop *nh = NULL;
185 ldp_fec *f = NULL;
187 ldp_session *nh_session = NULL;
188 mpls_bool done = MPLS_BOOL_FALSE;
190 ldp_attr *attr = NULL;
191 ldp_fs *fs = NULL;
192 ldp_attr *ds_attr = NULL;
194 LDP_ENTER(g->user_data, "ldp_label_request_initial_callback");
196 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_TIMER,
197 "Initial Label Request Callback fired: session(%d)\n", s->index);
199 mpls_lock_get(g->global_lock);
201 mpls_timer_stop(g->timer_handle, timer);
203 if ((f = MPLS_LIST_HEAD(&g->fec))) {
204 do {
205 if ((nh = MPLS_LIST_HEAD(&f->nh_root))) {
206 do {
207 switch (f->info.type) {
208 case MPLS_FEC_PREFIX:
209 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
210 LDP_TRACE_FLAG_ROUTE, "Processing prefix FEC: %08x/%d ",
211 f->info.u.prefix.network.u.ipv4, f->info.u.prefix.length);
212 break;
213 case MPLS_FEC_HOST:
214 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
215 LDP_TRACE_FLAG_ROUTE, "Processing host FEC: %08x ",
216 f->info.u.host.u.ipv4);
217 break;
218 case MPLS_FEC_L2CC:
219 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
220 LDP_TRACE_FLAG_ROUTE, "Processing L2CC FEC: %d %d %d ",
221 f->info.u.l2cc.connection_id, f->info.u.l2cc.group_id,
222 f->info.u.l2cc.type);
223 break;
224 default:
225 MPLS_ASSERT(0);
228 if (nh->info.type & MPLS_NH_IP) {
229 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
230 LDP_TRACE_FLAG_ROUTE, "via %08x\n", nh->addr->address.u.ipv4);
232 if (nh->info.type & MPLS_NH_IF) {
233 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
234 LDP_TRACE_FLAG_ROUTE, "via %p\n", nh->iff->handle);
237 /* check to see if export policy allows us to 'see' this route */
238 if (mpls_policy_export_check(g->user_data, &f->info, &nh->info)
239 == MPLS_BOOL_FALSE) {
240 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL,
241 LDP_TRACE_FLAG_DEBUG, "Rejected by export policy\n");
242 continue;
245 /* find the next hop session corresponding to this FEC */
246 nh_session = ldp_session_for_nexthop(nh);
248 /* do we have a valid next hop session, and is the nexp hop session
249 * this session? */
250 if ((!nh_session) || (nh_session->index != s->index)) {
251 continue;
254 /* have we already sent a label request to this peer for this FEC? */
255 if (ldp_attr_find_downstream_state(g, s, &f->info,
256 LDP_LSP_STATE_REQ_SENT)) {
257 continue;
260 /* clear out info from the last FEC */
261 ds_attr = NULL;
263 /* jleu: duplicate code from ldp_attr_find_upstream_state_any */
264 fs = MPLS_LIST_HEAD(&f->fs_root_us);
265 while (fs) {
266 attr = MPLS_LIST_HEAD(&fs->attr_root);
267 while (attr) {
268 if (attr->state == LDP_LSP_STATE_REQ_RECV ||
269 attr->state == LDP_LSP_STATE_MAP_SENT) {
270 if (!ds_attr) {
271 /* this is not neccessarily going to be XC'd to something */
272 ldp_label_request_for_xc(g, s, &f->info, attr, &ds_attr);
275 attr = MPLS_LIST_NEXT(&fs->attr_root, attr, _fs);
277 fs = MPLS_LIST_NEXT(&f->fs_root_us, fs, _fec);
280 if (!ds_attr) {
282 * we did not find any received requests or sent mappings so
283 * send a request and xc it to nothing
285 ldp_label_request_for_xc(g, s, &f->info, NULL, &ds_attr);
287 } while ((nh = MPLS_LIST_NEXT(&f->nh_root, nh, _fec)));
289 } while ((f = MPLS_LIST_NEXT(&g->fec, f, _global)));
290 done = MPLS_BOOL_TRUE;
293 if (done == MPLS_BOOL_TRUE) {
294 mpls_timer_delete(g->timer_handle, timer);
295 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
296 s->initial_distribution_timer = (mpls_timer_handle) 0;
297 } else {
298 mpls_timer_start(g->timer_handle, timer, MPLS_TIMER_ONESHOT);
299 /* need to mark the session with where it left off */
302 mpls_lock_release(g->global_lock);
304 LDP_EXIT(g->user_data, "ldp_label_request_initial_callback");
307 void Prepare_Label_Request_Attributes(ldp_global * g, ldp_session * s,
308 mpls_fec * fec, ldp_attr * r_attr, ldp_attr * s_attr)
310 int i;
312 MPLS_ASSERT(s && r_attr);
314 if (!(s->oper_loop_detection == LDP_LOOP_HOPCOUNT ||
315 s->oper_loop_detection == LDP_LOOP_HOPCOUNT_PATHVECTOR ||
316 r_attr->hopCountTlvExists)) { /* PRqA.1 */
317 return;
320 /* is this LSR allowed to be an LER for FEC? *//* PRqA.2 */
321 /* some policy gunk needs to be checked here */
322 /* if not goto PRqA.6 */
324 s_attr->hopCountTlvExists = 1; /* PRqA.3 */
325 s_attr->hopCountTlv.hcValue = 1;
327 if (s->oper_loop_detection == LDP_LOOP_NONE) { /* PRqA.4 */
328 return;
331 if (g->label_merge == MPLS_BOOL_TRUE) { /* PRqA.5 */
332 return;
334 goto Prepare_Label_Request_Attributes_13;
336 if (r_attr && r_attr->hopCountTlvExists) { /* PRqA.6 */
337 s_attr->hopCountTlvExists = 1; /* PRqA.7 */
338 s_attr->hopCountTlv.hcValue = (r_attr->hopCountTlv.hcValue) ?
339 (r_attr->hopCountTlv.hcValue + 1) : 0;
340 } else {
341 s_attr->hopCountTlvExists = 1; /* PRqA.8 */
342 s_attr->hopCountTlv.hcValue = 0;
345 if (s->oper_loop_detection == LDP_LOOP_NONE) { /* PRqA.9 */
346 return;
349 if (r_attr && r_attr->pathVecTlvExists) { /* PRqA.10 */
350 goto Prepare_Label_Request_Attributes_12;
353 if (g->label_merge == MPLS_BOOL_TRUE) { /* PRqA.11 */
354 return;
356 goto Prepare_Label_Request_Attributes_13;
358 Prepare_Label_Request_Attributes_12:
359 /* we only get to PRqA.12 if we have verified we have a r_attr */
360 s_attr->pathVecTlvExists = 1;
361 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
362 for (i = 1; i < (MPLS_MAXHOPSNUMBER - 1); i++) {
363 if (r_attr->pathVecTlv.lsrId[i - 1]) {
364 s_attr->pathVecTlv.lsrId[i] = r_attr->pathVecTlv.lsrId[i - 1];
367 return;
369 Prepare_Label_Request_Attributes_13:
370 s_attr->pathVecTlvExists = 1;
371 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
374 mpls_return_enum ldp_label_request_process(ldp_global * g, ldp_session * s,
375 ldp_adj * a, ldp_entity * e, ldp_attr * us_attr, ldp_fec * f)
377 ldp_session *nh_session = NULL;
378 ldp_nexthop *nh = NULL;
379 ldp_attr_list *us_list = NULL;
380 mpls_bool egress = MPLS_BOOL_FALSE;
381 mpls_bool egress_flag = MPLS_BOOL_FALSE;
382 ldp_attr *ds_attr = NULL;
383 ldp_attr *us_temp = NULL;
385 if (Check_Received_Attributes(g, s, us_attr, MPLS_LBLREQ_MSGTYPE) !=
386 MPLS_SUCCESS) { /* LRp.1 */
387 goto LRq_13;
390 if (f == NULL) {
391 ldp_notif_send(g, s, us_attr, LDP_NOTIF_NO_ROUTE); /* LRq.5 */
392 goto LRq_13;
395 /* just find one valid nexthop session for now */
396 nh = MPLS_LIST_HEAD(&f->nh_root);
397 while (nh) {
398 if (!nh_session) {
399 nh_session = ldp_session_for_nexthop(nh);
401 if (egress_flag == MPLS_BOOL_FALSE) {
402 egress_flag = mpls_policy_egress_check(g->user_data, &f->info, &nh->info);
404 nh = MPLS_LIST_NEXT(&f->nh_root, nh, _fec);
407 if ((!nh_session) && (egress_flag == MPLS_BOOL_TRUE)) {
408 egress = MPLS_BOOL_TRUE;
410 if (nh_session != NULL && s->index == nh_session->index) { /* LRq.3 */
411 ldp_notif_send(g, s, us_attr, LDP_NOTIF_LOOP_DETECTED); /* LRq.4 */
412 goto LRq_13;
415 if ((us_list = ldp_attr_find_upstream_all2(g, s, f)) != NULL) {
416 us_temp = MPLS_LIST_HEAD(us_list);
417 while (us_temp != NULL) {
418 if (us_temp->state == LDP_LSP_STATE_REQ_RECV && /* LRq.6 */
419 us_temp->msg_id == us_attr->msg_id) { /* LRq.7 */
420 goto LRq_13;
422 us_temp = MPLS_LIST_NEXT(us_list, us_temp, _fs);
426 us_attr->state = LDP_LSP_STATE_REQ_RECV; /* LRq.8 */
428 if (ldp_attr_insert_upstream2(g, s, us_attr, f) != MPLS_SUCCESS) {
429 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_ERROR,
430 "Couldn't insert recv attributes in tree\n");
431 goto ldp_label_request_process_error;
434 if (nh_session) {
435 ds_attr = ldp_attr_find_downstream_state2(g, nh_session, f,
436 LDP_LSP_STATE_MAP_RECV);
437 } else {
438 ds_attr = NULL;
441 if (g->lsp_control_mode == LDP_CONTROL_INDEPENDENT) { /* LRq.9 */
442 if (ldp_label_mapping_with_xc(g, s, f, &us_attr, ds_attr) != MPLS_SUCCESS) {
443 goto ldp_label_request_process_error;
446 if (egress == MPLS_BOOL_TRUE || ds_attr) {
447 goto LRq_11;
449 } else {
450 if ((!(egress == MPLS_BOOL_TRUE || ds_attr)) ||
451 (g->label_merge == MPLS_BOOL_FALSE)) {
452 goto LRq_10;
455 if (ldp_label_mapping_with_xc(g, s, f, &us_attr, ds_attr) != MPLS_SUCCESS) {
456 goto ldp_label_request_process_error;
458 goto LRq_11;
461 LRq_10:
462 ds_attr = NULL;
463 if (ldp_label_request_for_xc(g, nh_session, &f->info, us_attr, &ds_attr) !=
464 MPLS_SUCCESS) {
465 goto ldp_label_request_process_error;
468 LRq_11:
469 /* the work done by LRq_11 is handled in ldp_label_mapping_with_xc() */
470 LRq_13:
471 if (ds_attr != NULL && ds_attr->in_tree == MPLS_BOOL_FALSE) {
472 ldp_attr_remove_complete(g, ds_attr, MPLS_BOOL_FALSE);
474 return MPLS_SUCCESS;
476 ldp_label_request_process_error:
477 return MPLS_FAILURE;