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 (nh_session
&& peer
->index
== nh_session
->index
) {
84 /* have I already sent a mapping for FEC to peer */
85 if ((us_attr
= ldp_attr_find_upstream_state(g
, peer
, f
,
86 LDP_LSP_STATE_MAP_SENT
))) {
87 /* yep, don't send another */
90 if (peer
->oper_distribution_mode
== LDP_DISTRIBUTION_UNSOLICITED
) {
91 if (g
->lsp_control_mode
== LDP_CONTROL_INDEPENDENT
) {
93 ldp_attr_find_upstream_state(g
, peer
, f
, LDP_LSP_STATE_REQ_RECV
);
96 if (ldp_label_mapping_with_xc(g
, peer
, f
, &us_attr
, ds_attr
) !=
98 if (!us_attr
->in_tree
) {
99 ldp_attr_remove_complete(g
, us_attr
, MPLS_BOOL_FALSE
);
108 if (ds_attr
|| egress
== MPLS_BOOL_TRUE
) { /* FEC.1.DUO2 */
109 if (!(us_attr
= ldp_attr_create(f
))) {
113 if ((egress
== MPLS_BOOL_TRUE
) && (mpls_policy_egress_check(
114 g
->user_data
, f
, &f
->nh
) == MPLS_BOOL_TRUE
) &&
115 (egress
== MPLS_BOOL_TRUE
)) {
119 if (ldp_label_mapping_with_xc(g
, peer
, f
, &us_attr
, ds_attr
) !=
127 peer
= MPLS_LIST_NEXT(&g
->session
, peer
, _global
);
130 if (ds_attr
) { /* FEC.2 */
131 if (ldp_label_mapping_process(g
, nh_session
, NULL
, NULL
, ds_attr
, f
) ==
132 MPLS_FAILURE
) { /* FEC.5 */
139 * LDP_DISTRIBUTION_ONDEMAND
143 nh_session
->oper_distribution_mode
== LDP_DISTRIBUTION_ONDEMAND
) {
144 /* assume we're always "request when needed" */
146 if (ldp_label_request_for_xc(g
, nh_session
, f
, NULL
, &ds_attr
) ==
147 MPLS_FAILURE
) { /* FEC.4 */
152 LDP_EXIT(g
->user_data
, "Recognize_New_Fec");
154 return MPLS_SUCCESS
; /* FEC.6 */
157 mpls_return_enum
Detect_Change_Fec_Next_Hop(ldp_global
* g
, mpls_fec
* f
,
158 ldp_session
* nh_old
)
160 ldp_session
*peer
= NULL
;
161 ldp_attr
*us_attr
= NULL
;
162 ldp_attr
*ds_attr
= NULL
;
163 ldp_session
*nh_new
= NULL
;
164 mpls_return_enum result
;
166 LDP_ENTER(g
->user_data
, "Detect_Change_Fec_Next_Hop");
168 result
= ldp_get_next_hop_session_for_fec(g
, f
, NULL
, &nh_new
);
171 * NH 1-5 decide if we need to release an existing mapping
174 ldp_attr_find_downstream_state(g
, nh_old
, f
, LDP_LSP_STATE_MAP_RECV
);
175 if (!ds_attr
) { /* NH.1 */
176 goto Detect_Change_Fec_Next_Hop_6
;
179 if (ds_attr
->ingress
== MPLS_BOOL_TRUE
) {
183 ftn
.outsegment_index
= ds_attr
->outlabel
->info
.handle
;
184 memcpy(&ftn
.fec
, f
, sizeof(mpls_fec
));
185 lsr_cfg_ftn_set2(g
->lsr_handle
, &ftn
, LSR_CFG_DEL
);
187 mpls_mpls_fec2out_del(g
->mpls_handle
, f
, &ds_attr
->outlabel
->info
);
189 ds_attr
->ingress
= MPLS_BOOL_FALSE
;
190 ds_attr
->outlabel
->merge_count
--;
193 if (g
->label_retention_mode
== LDP_RETENTION_LIBERAL
) { /* NH.3 */
195 us_attr
= MPLS_LIST_HEAD(&ds_attr
->us_attr_root
);
197 /* need to walk the list in such a way as not to
198 * "pull the rug out from under me self"
200 us_temp
= MPLS_LIST_NEXT(&ds_attr
->us_attr_root
, us_attr
, _ds_attr
);
201 if (us_attr
->state
== LDP_LSP_STATE_MAP_SENT
) {
202 ldp_inlabel_del_outlabel(g
, us_attr
->inlabel
); /* NH.2 */
203 ldp_attr_del_us2ds(us_attr
, ds_attr
);
207 goto Detect_Change_Fec_Next_Hop_6
;
210 ldp_label_release_send(g
, nh_old
, ds_attr
, LDP_NOTIF_NONE
); /* NH.4 */
211 ldp_attr_remove_complete(g
, ds_attr
, MPLS_BOOL_FALSE
); /* NH.2,5 */
213 Detect_Change_Fec_Next_Hop_6
:
216 * NH 6-9 decides is we need to send a label request abort
219 ldp_attr_find_downstream_state(g
, nh_old
, f
, LDP_LSP_STATE_REQ_SENT
);
220 if (ds_attr
) { /* NH.6 */
221 if (g
->label_retention_mode
!= LDP_RETENTION_CONSERVATIVE
) { /* NH.7 */
223 if (ldp_label_abort_send(g
, nh_old
, ds_attr
) != MPLS_SUCCESS
) {
230 * NH 10-12 decides if we can use a mapping from our database
233 goto Detect_Change_Fec_Next_Hop_16
;
237 ldp_attr_find_downstream_state(g
, nh_new
, f
, LDP_LSP_STATE_MAP_RECV
);
238 if (!ds_attr
) { /* NH.11 */
239 goto Detect_Change_Fec_Next_Hop_13
;
242 if (ldp_label_mapping_process(g
, nh_new
, NULL
, NULL
, ds_attr
, f
) !=
243 MPLS_SUCCESS
) { /* NH.12 */
246 goto Detect_Change_Fec_Next_Hop_20
;
248 Detect_Change_Fec_Next_Hop_13
:
251 * NH 13-15 decides if we need to make a label request
253 if (nh_new
->oper_distribution_mode
== LDP_DISTRIBUTION_ONDEMAND
&&
254 g
->label_retention_mode
== LDP_RETENTION_CONSERVATIVE
) {
256 if (ldp_label_request_for_xc(g
, nh_new
, f
, NULL
, &ds_attr
) !=
261 goto Detect_Change_Fec_Next_Hop_20
;
263 Detect_Change_Fec_Next_Hop_16
:
265 peer
= MPLS_LIST_HEAD(&g
->session
);
267 us_attr
= ldp_attr_find_upstream_state(g
, peer
, f
, LDP_LSP_STATE_MAP_SENT
);
268 if (us_attr
) { /* NH.17 */
269 if (ldp_label_withdraw_send(g
, peer
, us_attr
, LDP_NOTIF_NONE
) !=
270 MPLS_SUCCESS
) { /* NH.18 */
271 ldp_attr_remove_complete(g
, us_attr
, MPLS_BOOL_FALSE
);
275 peer
= MPLS_LIST_NEXT(&g
->session
, peer
, _global
);
278 Detect_Change_Fec_Next_Hop_20
:
280 LDP_EXIT(g
->user_data
, "Detect_Change_Fec_Next_Hop");
285 ldp_fec
*ldp_fec_create()
287 ldp_fec
*fec
= (ldp_fec
*) mpls_malloc(sizeof(ldp_fec
));
290 memset(fec
, 0, sizeof(ldp_fec
));
291 MPLS_REFCNT_INIT(fec
, 0);
292 MPLS_LIST_ELEM_INIT(fec
, _global
);
293 MPLS_LIST_ELEM_INIT(fec
, _inlabel
);
294 MPLS_LIST_ELEM_INIT(fec
, _outlabel
);
295 MPLS_LIST_ELEM_INIT(fec
, _fec
);
296 MPLS_LIST_ELEM_INIT(fec
, _nh
);
297 MPLS_LIST_INIT(&fec
->fs_root_us
, ldp_fs
);
298 MPLS_LIST_INIT(&fec
->fs_root_ds
, ldp_fs
);
299 fec
->info
.type
= MPLS_FEC_NONE
;
300 fec
->info
.nh
.type
= MPLS_NH_NONE
;
301 fec
->info
.nh
.attached
= MPLS_BOOL_FALSE
;
306 void mpls_fec2ldp_fec(mpls_fec
* a
, ldp_fec
* b
)
308 memcpy(&b
->info
, a
, sizeof(mpls_fec
));
311 ldp_fec
*ldp_fec_create_host(mpls_inet_addr
* host
)
313 ldp_fec
*fec
= ldp_fec_create();
316 fec
->info
.type
= MPLS_FEC_HOST
;
317 memcpy(&fec
->info
.u
.host
, host
, sizeof(mpls_inet_addr
));
322 ldp_fec
*ldp_fec_create_prefix(mpls_inet_addr
* prefix
, int prefix_len
)
324 ldp_fec
*fec
= ldp_fec_create();
327 fec
->info
.type
= MPLS_FEC_PREFIX
;
328 memcpy(&fec
->info
.u
.prefix
.network
, prefix
, sizeof(mpls_inet_addr
));
329 fec
->info
.u
.prefix
.length
= prefix_len
;
334 void ldp_fec_delete(ldp_fec
* fec
)
339 void mpls_fec2fec_tlv(mpls_fec
* lf
, mplsLdpFecTlv_t
* tlv
, int i
)
341 tlv
->fecElArray
[i
].addressEl
.addressFam
= 1;
344 case MPLS_FEC_PREFIX
:
345 tlv
->fecElArray
[i
].addressEl
.type
= MPLS_PREFIX_FEC
;
346 tlv
->fecElArray
[i
].addressEl
.preLen
= lf
->u
.prefix
.length
;
347 tlv
->fecElArray
[i
].addressEl
.address
= lf
->u
.prefix
.network
.u
.ipv4
;
348 tlv
->fecElemTypes
[i
] = MPLS_PREFIX_FEC
;
351 tlv
->fecElArray
[i
].addressEl
.type
= MPLS_HOSTADR_FEC
;
352 tlv
->fecElArray
[i
].addressEl
.preLen
= MPLS_IPv4LEN
;
353 tlv
->fecElArray
[i
].addressEl
.address
= lf
->u
.host
.u
.ipv4
;
354 tlv
->fecElemTypes
[i
] = MPLS_HOSTADR_FEC
;
361 void fec_tlv2mpls_fec(mplsLdpFecTlv_t
* tlv
, int i
, mpls_fec
* lf
) {
362 switch (tlv
->fecElemTypes
[i
]) {
363 case MPLS_PREFIX_FEC
:
364 lf
->type
= MPLS_FEC_PREFIX
;
365 lf
->u
.prefix
.length
= tlv
->fecElArray
[i
].addressEl
.preLen
;
366 lf
->u
.prefix
.network
.u
.ipv4
= tlv
->fecElArray
[i
].addressEl
.address
;
367 lf
->u
.prefix
.network
.type
= MPLS_FAMILY_IPV4
;
369 case MPLS_HOSTADR_FEC
:
370 lf
->type
= MPLS_FEC_HOST
;
371 lf
->u
.host
.u
.ipv4
= tlv
->fecElArray
[i
].addressEl
.address
;
372 lf
->u
.host
.type
= MPLS_FAMILY_IPV4
;