There was a possibility for a memory leak, if the adding of the FTN
[mpls-ldp-portable.git] / ldp / ldp_label_request.c
blob4f9b79452250fafbd4575f928703e2d8e16e316d
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_attr *ds_attr = NULL;
185 ldp_attr *us_attr = NULL;
186 ldp_addr *nh_addr = NULL;
187 ldp_session *nh_session = NULL;
188 mpls_bool done = MPLS_BOOL_FALSE;
189 mpls_fec dest;
191 LDP_ENTER(g->user_data, "ldp_label_request_initial_callback");
193 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_TIMER,
194 "Initial Label Request Callback fired: session(%d)\n", s->index);
196 mpls_lock_get(g->global_lock);
198 mpls_timer_stop(g->timer_handle, timer);
200 if (mpls_fib_getfirst_route(g->fib_handle, &dest) == MPLS_SUCCESS) {
201 do {
203 /* check to see if export policy allows us to 'see' this route */
204 if (mpls_policy_export_check(g->user_data, &dest, &dest.nh)
205 == MPLS_BOOL_FALSE) {
206 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_ALL, LDP_TRACE_FLAG_DEBUG,
207 "Rejected by export policy\n");
208 continue;
211 /* find the next hop session corresponding to this FEC */
212 if (ldp_get_next_hop_session_for_fec(g, &dest, &nh_addr, &nh_session) !=
213 MPLS_SUCCESS || !nh_session) {
214 us_attr = NULL;
215 } else {
216 us_attr =
217 ldp_attr_find_upstream_state(g, nh_session, &dest,
218 LDP_LSP_STATE_REQ_RECV);
221 /* do we have a valid next hop session? */
222 if (!nh_session || nh_session->index != s->index) {
223 continue;
226 ds_attr = NULL;
228 /* this is not neccessarily going to be XC'd to something */
229 ldp_label_request_for_xc(g, s, &dest, us_attr, &ds_attr);
231 } while (mpls_fib_getnext_route(g->fib_handle, &dest) == MPLS_SUCCESS);
232 done = MPLS_BOOL_TRUE;
234 #if 0
235 // --- If we want to make it work, I think we have to modify the inlabel structure
236 // in ldp_struct.h, but dod can still be working without this ---
237 in = MPLS_LIST_HEAD(&g->inlabel);
238 while (in != NULL) {
239 fec_tlv2mpls_fec(&in->attr->fecTlv, 0, &dest);
240 if (ldp_attr_find_downstream_state(g, s, &dest, LDP_LSP_STATE_REQ_SENT) ==
241 NULL) {
243 * we haven't sent a request yet. if merge count is 0,
244 * and we don't merge sent a label request to the next hop
246 if (out->merge_count > 0 && g->label_merge == MPLS_BOOL_FALSE) {
247 if (ldp_label_request_for_xc(g, in->session, &dest, in->attr,
248 &ds_attr) == MPLS_FAILURE) {
249 goto ldp_label_request_initial_end;
253 in = MPLS_LIST_NEXT(&g->inlabel, in, _global);
255 done = MPLS_BOOL_TRUE;
257 ldp_label_request_initial_end:
258 #endif
260 if (done == MPLS_BOOL_TRUE) {
261 mpls_timer_delete(g->timer_handle, timer);
262 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
263 s->initial_distribution_timer = (mpls_timer_handle) 0;
264 } else {
265 mpls_timer_start(g->timer_handle, timer, MPLS_TIMER_ONESHOT);
266 /* need to mark the session with where it left off */
269 mpls_lock_release(g->global_lock);
271 LDP_EXIT(g->user_data, "ldp_label_request_initial_callback");
274 void Prepare_Label_Request_Attributes(ldp_global * g, ldp_session * s,
275 mpls_fec * fec, ldp_attr * r_attr, ldp_attr * s_attr)
277 int i;
279 MPLS_ASSERT(s && r_attr);
281 if (!(s->oper_loop_detection == LDP_LOOP_HOPCOUNT ||
282 s->oper_loop_detection == LDP_LOOP_HOPCOUNT_PATHVECTOR ||
283 r_attr->hopCountTlvExists)) { /* PRqA.1 */
284 return;
287 /* is this LSR allowed to be an LER for FEC? *//* PRqA.2 */
288 /* some policy gunk needs to be checked here */
289 /* if not goto PRqA.6 */
291 s_attr->hopCountTlvExists = 1; /* PRqA.3 */
292 s_attr->hopCountTlv.hcValue = 1;
294 if (s->oper_loop_detection == LDP_LOOP_NONE) { /* PRqA.4 */
295 return;
298 if (g->label_merge == MPLS_BOOL_TRUE) { /* PRqA.5 */
299 return;
301 goto Prepare_Label_Request_Attributes_13;
303 if (r_attr && r_attr->hopCountTlvExists) { /* PRqA.6 */
304 s_attr->hopCountTlvExists = 1; /* PRqA.7 */
305 s_attr->hopCountTlv.hcValue = (r_attr->hopCountTlv.hcValue) ?
306 (r_attr->hopCountTlv.hcValue + 1) : 0;
307 } else {
308 s_attr->hopCountTlvExists = 1; /* PRqA.8 */
309 s_attr->hopCountTlv.hcValue = 0;
312 if (s->oper_loop_detection == LDP_LOOP_NONE) { /* PRqA.9 */
313 return;
316 if (r_attr && r_attr->pathVecTlvExists) { /* PRqA.10 */
317 goto Prepare_Label_Request_Attributes_12;
320 if (g->label_merge == MPLS_BOOL_TRUE) { /* PRqA.11 */
321 return;
323 goto Prepare_Label_Request_Attributes_13;
325 Prepare_Label_Request_Attributes_12:
326 /* we only get to PRqA.12 if we have verified we have a r_attr */
327 s_attr->pathVecTlvExists = 1;
328 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
329 for (i = 1; i < (MPLS_MAXHOPSNUMBER - 1); i++) {
330 if (r_attr->pathVecTlv.lsrId[i - 1]) {
331 s_attr->pathVecTlv.lsrId[i] = r_attr->pathVecTlv.lsrId[i - 1];
334 return;
336 Prepare_Label_Request_Attributes_13:
337 s_attr->pathVecTlvExists = 1;
338 s_attr->pathVecTlv.lsrId[0] = g->lsr_identifier.u.ipv4;
341 mpls_return_enum ldp_label_request_process(ldp_global * g, ldp_session * s,
342 ldp_adj * a, ldp_entity * e, ldp_attr * us_attr, mpls_fec * fec)
344 ldp_session *nh_session = NULL;
345 ldp_addr *nh_addr = NULL;
346 ldp_attr_list *us_list = NULL;
347 mpls_bool egress = MPLS_BOOL_FALSE;
348 ldp_attr *ds_attr = NULL;
349 ldp_attr *us_temp = NULL;
351 if (Check_Received_Attributes(g, s, us_attr, MPLS_LBLREQ_MSGTYPE) != MPLS_SUCCESS) { /* LRp.1 */
352 goto LRq_13;
355 switch (ldp_get_next_hop_session_for_fec(g, fec, &nh_addr, &nh_session)) {
356 case MPLS_SUCCESS: /* LRq.2 */
358 if (nh_addr == NULL) {
359 egress = MPLS_BOOL_TRUE;
361 if (nh_session != NULL && s->index == nh_session->index) { /* LRq.3 */
362 ldp_notif_send(g, s, us_attr, LDP_NOTIF_LOOP_DETECTED); /* LRq.4 */
363 goto LRq_13;
365 break;
367 case MPLS_FAILURE:
368 case MPLS_NO_ROUTE:
370 ldp_notif_send(g, s, us_attr, LDP_NOTIF_NO_ROUTE); /* LRq.5 */
371 goto LRq_13;
373 default:
375 MPLS_ASSERT(0);
379 if ((us_list = ldp_attr_find_upstream_all(g, s, fec)) != NULL) {
380 us_temp = MPLS_LIST_HEAD(us_list);
381 while (us_temp != NULL) {
382 if (us_temp->state == LDP_LSP_STATE_REQ_RECV && /* LRq.6 */
383 us_temp->msg_id == us_attr->msg_id) { /* LRq.7 */
384 goto LRq_13;
386 us_temp = MPLS_LIST_NEXT(us_list, us_temp, _fs);
390 us_attr->state = LDP_LSP_STATE_REQ_RECV; /* LRq.8 */
392 if (ldp_attr_insert_upstream(g, s, us_attr) != MPLS_SUCCESS) {
393 LDP_TRACE_LOG(g->user_data, MPLS_TRACE_STATE_RECV, LDP_TRACE_FLAG_ERROR,
394 "Couldn't insert recv attributes in tree\n");
395 goto ldp_label_request_process_error;
398 if (nh_session) {
399 ds_attr = ldp_attr_find_downstream_state(g, nh_session, fec,
400 LDP_LSP_STATE_MAP_RECV);
401 } else {
402 ds_attr = NULL;
405 if (g->lsp_control_mode == LDP_CONTROL_INDEPENDENT) { /* LRq.9 */
406 if (ldp_label_mapping_with_xc(g, s, fec, &us_attr, ds_attr) != MPLS_SUCCESS) {
407 goto ldp_label_request_process_error;
410 if (egress == MPLS_BOOL_TRUE || ds_attr) {
411 goto LRq_11;
413 } else {
414 if ((!(egress == MPLS_BOOL_TRUE || ds_attr)) || (g->label_merge == MPLS_BOOL_FALSE)) {
415 goto LRq_10;
418 if (ldp_label_mapping_with_xc(g, s, fec, &us_attr, ds_attr) != MPLS_SUCCESS) {
419 goto ldp_label_request_process_error;
421 goto LRq_11;
424 LRq_10:
425 ds_attr = NULL;
426 if (ldp_label_request_for_xc(g, nh_session, fec, us_attr, &ds_attr) !=
427 MPLS_SUCCESS) {
428 goto ldp_label_request_process_error;
431 LRq_11:
432 /* the work done by LRq_11 is handled in ldp_label_mapping_with_xc() */
433 LRq_13:
434 if (ds_attr != NULL && ds_attr->in_tree == MPLS_BOOL_FALSE) {
435 ldp_attr_remove_complete(g, ds_attr, MPLS_BOOL_FALSE);
437 return MPLS_SUCCESS;
439 ldp_label_request_process_error:
440 return MPLS_FAILURE;