3 * Copyright (C) James R. Leu 2000
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
10 #include "ldp_struct.h"
14 #include "ldp_session.h"
15 #include "ldp_inlabel.h"
16 #include "ldp_outlabel.h"
17 #include "ldp_global.h"
18 #include "ldp_label_mapping.h"
19 #include "ldp_label_request.h"
20 #include "ldp_label_abort.h"
21 #include "ldp_label_rel_with.h"
22 #include "mpls_mm_impl.h"
23 #include "mpls_policy_impl.h"
24 #include "mpls_trace_impl.h"
29 #include "mpls_mpls_impl.h"
32 mpls_return_enum
Recognize_New_Fec(ldp_global
* g
, mpls_fec
* f
)
34 ldp_session
*peer
= NULL
;
35 ldp_session
*nh_session
= NULL
;
36 ldp_attr
*ds_attr
= NULL
;
37 ldp_attr
*us_attr
= NULL
;
38 mpls_bool egress
= MPLS_BOOL_FALSE
;
42 LDP_ENTER(g
->user_data
, "Recognize_New_Fec");
45 * find the info about the next hop for this FEC
47 switch (ldp_get_next_hop_session_for_fec(g
, f
, &nh_addr
, &nh_session
)) {
49 if (nh_addr
== NULL
) {
51 * we found the route, but no next hop
53 egress
= MPLS_BOOL_TRUE
;
64 ds_attr
= ldp_attr_find_downstream_state(g
, nh_session
, f
,
65 LDP_LSP_STATE_MAP_RECV
);
66 if (ds_attr
&& !ds_attr
->outlabel
) {
67 out
= ldp_outlabel_create_complete(g
, nh_session
, nh_addr
, ds_attr
);
71 ds_attr
->outlabel
= out
;
76 * for every peer except the nh hop peer, check to see if we need to
79 peer
= MPLS_LIST_HEAD(&g
->session
);
80 while (peer
!= NULL
) { /* FEC.1 */
81 if ((peer
->state
!= LDP_STATE_OPERATIONAL
) ||
82 (nh_session
&& peer
->index
== nh_session
->index
)) {
85 /* have I already sent a mapping for FEC to peer */
86 if ((us_attr
= ldp_attr_find_upstream_state(g
, peer
, f
,
87 LDP_LSP_STATE_MAP_SENT
))) {
88 /* yep, don't send another */
91 if (peer
->oper_distribution_mode
== LDP_DISTRIBUTION_UNSOLICITED
) {
92 if (g
->lsp_control_mode
== LDP_CONTROL_INDEPENDENT
) {
94 ldp_attr_find_upstream_state(g
, peer
, f
, LDP_LSP_STATE_REQ_RECV
);
97 if (ldp_label_mapping_with_xc(g
, peer
, f
, &us_attr
, ds_attr
) !=
99 if (!us_attr
->in_tree
) {
100 ldp_attr_remove_complete(g
, us_attr
, MPLS_BOOL_FALSE
);
109 if (ds_attr
|| egress
== MPLS_BOOL_TRUE
) { /* FEC.1.DUO2 */
110 if (!(us_attr
= ldp_attr_create(f
))) {
114 if ((egress
== MPLS_BOOL_TRUE
) && (mpls_policy_egress_check(
115 g
->user_data
, f
, &f
->nh
) == MPLS_BOOL_TRUE
) &&
116 (egress
== MPLS_BOOL_TRUE
)) {
120 if (ldp_label_mapping_with_xc(g
, peer
, f
, &us_attr
, ds_attr
) !=
128 peer
= MPLS_LIST_NEXT(&g
->session
, peer
, _global
);
131 if (ds_attr
) { /* FEC.2 */
132 if (ldp_label_mapping_process(g
, nh_session
, NULL
, NULL
, ds_attr
, f
) ==
133 MPLS_FAILURE
) { /* FEC.5 */
140 * LDP_DISTRIBUTION_ONDEMAND
144 nh_session
->oper_distribution_mode
== LDP_DISTRIBUTION_ONDEMAND
) {
145 /* assume we're always "request when needed" */
147 if (ldp_label_request_for_xc(g
, nh_session
, f
, NULL
, &ds_attr
) ==
148 MPLS_FAILURE
) { /* FEC.4 */
153 LDP_EXIT(g
->user_data
, "Recognize_New_Fec");
155 return MPLS_SUCCESS
; /* FEC.6 */
158 mpls_return_enum
Detect_Change_Fec_Next_Hop(ldp_global
* g
, mpls_fec
* f
,
159 ldp_session
* nh_old
)
161 ldp_session
*peer
= NULL
;
162 ldp_attr
*us_attr
= NULL
;
163 ldp_attr
*ds_attr
= NULL
;
164 ldp_session
*nh_new
= NULL
;
165 mpls_return_enum result
;
167 LDP_ENTER(g
->user_data
, "Detect_Change_Fec_Next_Hop");
169 result
= ldp_get_next_hop_session_for_fec(g
, f
, NULL
, &nh_new
);
172 * NH 1-5 decide if we need to release an existing mapping
175 ldp_attr_find_downstream_state(g
, nh_old
, f
, LDP_LSP_STATE_MAP_RECV
);
176 if (!ds_attr
) { /* NH.1 */
177 goto Detect_Change_Fec_Next_Hop_6
;
180 if (ds_attr
->ingress
== MPLS_BOOL_TRUE
) {
184 ftn
.outsegment_index
= ds_attr
->outlabel
->info
.handle
;
185 memcpy(&ftn
.fec
, f
, sizeof(mpls_fec
));
186 lsr_cfg_ftn_set2(g
->lsr_handle
, &ftn
, LSR_CFG_DEL
);
188 mpls_mpls_fec2out_del(g
->mpls_handle
, f
, &ds_attr
->outlabel
->info
);
190 ds_attr
->ingress
= MPLS_BOOL_FALSE
;
191 ds_attr
->outlabel
->merge_count
--;
194 if (g
->label_retention_mode
== LDP_RETENTION_LIBERAL
) { /* NH.3 */
196 us_attr
= MPLS_LIST_HEAD(&ds_attr
->us_attr_root
);
198 /* need to walk the list in such a way as not to
199 * "pull the rug out from under me self"
201 us_temp
= MPLS_LIST_NEXT(&ds_attr
->us_attr_root
, us_attr
, _ds_attr
);
202 if (us_attr
->state
== LDP_LSP_STATE_MAP_SENT
) {
203 ldp_inlabel_del_outlabel(g
, us_attr
->inlabel
); /* NH.2 */
204 ldp_attr_del_us2ds(us_attr
, ds_attr
);
208 goto Detect_Change_Fec_Next_Hop_6
;
211 ldp_label_release_send(g
, nh_old
, ds_attr
, LDP_NOTIF_NONE
); /* NH.4 */
212 ldp_attr_remove_complete(g
, ds_attr
, MPLS_BOOL_FALSE
); /* NH.2,5 */
214 Detect_Change_Fec_Next_Hop_6
:
217 * NH 6-9 decides is we need to send a label request abort
220 ldp_attr_find_downstream_state(g
, nh_old
, f
, LDP_LSP_STATE_REQ_SENT
);
221 if (ds_attr
) { /* NH.6 */
222 if (g
->label_retention_mode
!= LDP_RETENTION_CONSERVATIVE
) { /* NH.7 */
224 if (ldp_label_abort_send(g
, nh_old
, ds_attr
) != MPLS_SUCCESS
) {
231 * NH 10-12 decides if we can use a mapping from our database
234 goto Detect_Change_Fec_Next_Hop_16
;
238 ldp_attr_find_downstream_state(g
, nh_new
, f
, LDP_LSP_STATE_MAP_RECV
);
239 if (!ds_attr
) { /* NH.11 */
240 goto Detect_Change_Fec_Next_Hop_13
;
243 if (ldp_label_mapping_process(g
, nh_new
, NULL
, NULL
, ds_attr
, f
) !=
244 MPLS_SUCCESS
) { /* NH.12 */
247 goto Detect_Change_Fec_Next_Hop_20
;
249 Detect_Change_Fec_Next_Hop_13
:
252 * NH 13-15 decides if we need to make a label request
254 if (nh_new
->oper_distribution_mode
== LDP_DISTRIBUTION_ONDEMAND
&&
255 g
->label_retention_mode
== LDP_RETENTION_CONSERVATIVE
) {
257 if (ldp_label_request_for_xc(g
, nh_new
, f
, NULL
, &ds_attr
) !=
262 goto Detect_Change_Fec_Next_Hop_20
;
264 Detect_Change_Fec_Next_Hop_16
:
266 peer
= MPLS_LIST_HEAD(&g
->session
);
268 if (peer
->state
== LDP_STATE_OPERATIONAL
) {
269 us_attr
= ldp_attr_find_upstream_state(g
, peer
, f
,
270 LDP_LSP_STATE_MAP_SENT
);
271 if (us_attr
) { /* NH.17 */
272 if (ldp_label_withdraw_send(g
, peer
, us_attr
, LDP_NOTIF_NONE
) !=
273 MPLS_SUCCESS
) { /* NH.18 */
274 ldp_attr_remove_complete(g
, us_attr
, MPLS_BOOL_FALSE
);
279 peer
= MPLS_LIST_NEXT(&g
->session
, peer
, _global
);
282 Detect_Change_Fec_Next_Hop_20
:
284 LDP_EXIT(g
->user_data
, "Detect_Change_Fec_Next_Hop");
289 ldp_fec
*ldp_fec_create()
291 ldp_fec
*fec
= (ldp_fec
*) mpls_malloc(sizeof(ldp_fec
));
294 memset(fec
, 0, sizeof(ldp_fec
));
295 MPLS_REFCNT_INIT(fec
, 0);
296 MPLS_LIST_ELEM_INIT(fec
, _global
);
297 MPLS_LIST_ELEM_INIT(fec
, _inlabel
);
298 MPLS_LIST_ELEM_INIT(fec
, _outlabel
);
299 MPLS_LIST_ELEM_INIT(fec
, _fec
);
300 MPLS_LIST_ELEM_INIT(fec
, _nh
);
301 MPLS_LIST_INIT(&fec
->fs_root_us
, ldp_fs
);
302 MPLS_LIST_INIT(&fec
->fs_root_ds
, ldp_fs
);
303 fec
->info
.type
= MPLS_FEC_NONE
;
304 fec
->info
.nh
.type
= MPLS_NH_NONE
;
305 fec
->info
.nh
.attached
= MPLS_BOOL_FALSE
;
310 void mpls_fec2ldp_fec(mpls_fec
* a
, ldp_fec
* b
)
312 memcpy(&b
->info
, a
, sizeof(mpls_fec
));
315 ldp_fec
*ldp_fec_create_host(mpls_inet_addr
* host
)
317 ldp_fec
*fec
= ldp_fec_create();
320 fec
->info
.type
= MPLS_FEC_HOST
;
321 memcpy(&fec
->info
.u
.host
, host
, sizeof(mpls_inet_addr
));
326 ldp_fec
*ldp_fec_create_prefix(mpls_inet_addr
* prefix
, int prefix_len
)
328 ldp_fec
*fec
= ldp_fec_create();
331 fec
->info
.type
= MPLS_FEC_PREFIX
;
332 memcpy(&fec
->info
.u
.prefix
.network
, prefix
, sizeof(mpls_inet_addr
));
333 fec
->info
.u
.prefix
.length
= prefix_len
;
338 void ldp_fec_delete(ldp_fec
* fec
)
343 void mpls_fec2fec_tlv(mpls_fec
* lf
, mplsLdpFecTlv_t
* tlv
, int i
)
345 tlv
->fecElArray
[i
].addressEl
.addressFam
= 1;
348 case MPLS_FEC_PREFIX
:
349 tlv
->fecElArray
[i
].addressEl
.type
= MPLS_PREFIX_FEC
;
350 tlv
->fecElArray
[i
].addressEl
.preLen
= lf
->u
.prefix
.length
;
351 tlv
->fecElArray
[i
].addressEl
.address
= lf
->u
.prefix
.network
.u
.ipv4
;
352 tlv
->fecElemTypes
[i
] = MPLS_PREFIX_FEC
;
355 tlv
->fecElArray
[i
].addressEl
.type
= MPLS_HOSTADR_FEC
;
356 tlv
->fecElArray
[i
].addressEl
.preLen
= MPLS_IPv4LEN
;
357 tlv
->fecElArray
[i
].addressEl
.address
= lf
->u
.host
.u
.ipv4
;
358 tlv
->fecElemTypes
[i
] = MPLS_HOSTADR_FEC
;
365 void fec_tlv2mpls_fec(mplsLdpFecTlv_t
* tlv
, int i
, mpls_fec
* lf
) {
366 switch (tlv
->fecElemTypes
[i
]) {
367 case MPLS_PREFIX_FEC
:
368 lf
->type
= MPLS_FEC_PREFIX
;
369 lf
->u
.prefix
.length
= tlv
->fecElArray
[i
].addressEl
.preLen
;
370 lf
->u
.prefix
.network
.u
.ipv4
= tlv
->fecElArray
[i
].addressEl
.address
;
371 lf
->u
.prefix
.network
.type
= MPLS_FAMILY_IPV4
;
373 case MPLS_HOSTADR_FEC
:
374 lf
->type
= MPLS_FEC_HOST
;
375 lf
->u
.host
.u
.ipv4
= tlv
->fecElArray
[i
].addressEl
.address
;
376 lf
->u
.host
.type
= MPLS_FAMILY_IPV4
;