Make sure that all check for 'egress' utilize mpls_policy_egress_check
[mpls-ldp-portable.git] / ldp / ldp_inlabel.c
blobc5fa92d4ca60e4a0d0b6e194b4ffac92851567d6
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 <stdlib.h>
11 #include "ldp_struct.h"
12 #include "ldp_outlabel.h"
13 #include "ldp_inlabel.h"
14 #include "ldp_session.h"
15 #include "ldp_entity.h"
16 #include "ldp_attr.h"
17 #include "ldp_global.h"
19 #include "mpls_assert.h"
20 #include "mpls_mm_impl.h"
21 #include "mpls_trace_impl.h"
23 #if MPLS_USE_LSR
24 #include "lsr_cfg.h"
25 #else
26 #include "mpls_mpls_impl.h"
27 #endif
29 static uint32_t _ldp_inlabel_next_index = 1;
31 ldp_inlabel *ldp_inlabel_create()
33 ldp_inlabel *i = (ldp_inlabel *) mpls_malloc(sizeof(ldp_inlabel));
35 if (i) {
36 memset(i, 0, sizeof(ldp_inlabel));
37 MPLS_REFCNT_INIT(i, 0);
38 mpls_link_list_init(&i->session_root);
39 mpls_link_list_init(&i->attr_root);
40 MPLS_LIST_ELEM_INIT(i, _global);
41 MPLS_LIST_ELEM_INIT(i, _outlabel);
42 i->index = _ldp_inlabel_get_next_index();
43 i->info.label.type = MPLS_LABEL_TYPE_NONE;
45 return i;
48 void ldp_inlabel_delete_complete(ldp_global * g, ldp_inlabel * in,
49 ldp_session * s, ldp_attr * a)
51 ldp_attr_del_inlabel(a);
52 ldp_session_del_inlabel(s, in);
53 _ldp_global_del_inlabel(g, in);
54 ldp_inlabel_del_outlabel(g, in);
57 ldp_inlabel *ldp_inlabel_create_complete(ldp_global * g, ldp_session * s,
58 ldp_attr * a)
60 ldp_inlabel *in = ldp_inlabel_create();
61 mpls_return_enum result;
63 if (in != NULL) {
65 in->info.labelspace = s->cfg_label_space;
66 in->info.npop = 1;
67 in->info.family = MPLS_FAMILY_IPV4;
68 in->info.owner = MPLS_OWNER_LDP;
69 result = _ldp_global_add_inlabel(g, in);
71 if (result == MPLS_FAILURE) {
72 _ldp_global_del_inlabel(g, in);
73 return NULL;
76 if (ldp_session_add_inlabel(s, in) == MPLS_FAILURE) {
77 _ldp_global_del_inlabel(g, in);
78 return NULL;
81 mpls_label_struct2ldp_attr(&in->info.label, a);
82 ldp_attr_add_inlabel(a, in);
84 return in;
87 void ldp_inlabel_delete(ldp_inlabel * i)
89 LDP_PRINT(g->user_data,"inlabel delete\n");
90 MPLS_REFCNT_ASSERT(i, 0);
91 mpls_free(i);
94 mpls_return_enum ldp_inlabel_add_outlabel(ldp_global *g, ldp_inlabel *i,
95 ldp_outlabel *o) {
96 mpls_return_enum result;
98 MPLS_ASSERT(i && o);
99 MPLS_ASSERT(i->outlabel == NULL);
101 #if MPLS_USE_LSR
103 lsr_xconnect xcon;
104 xcon.insegment_index = i->info.handle;
105 xcon.outsegment_index = o->info.handle;
106 xcon.info.owner = MPLS_OWNER_LDP;
107 result = lsr_cfg_xconnect_set2(g->lsr_handle, &xcon, LSR_CFG_ADD|
108 LSR_XCONNECT_CFG_OUTSEGMENT|LSR_XCONNECT_CFG_INSEGMENT|
109 LSR_XCONNECT_CFG_LSPID|LSR_XCONNECT_CFG_OWNER);
111 #else
112 result = mpls_mpls_xconnect_add(g->mpls_handle, &i->info, &o->info);
113 #endif
114 if (result == MPLS_SUCCESS) {
115 MPLS_REFCNT_HOLD(o);
116 i->outlabel = o;
117 _ldp_outlabel_add_inlabel(o, i);
119 return result;
122 mpls_return_enum ldp_inlabel_del_outlabel(ldp_global *g, ldp_inlabel * i)
124 MPLS_ASSERT(i && i->outlabel);
126 #if MPLS_USE_LSR
127 lsr_xconnect xcon;
128 xcon.insegment_index = i->info.handle;
129 xcon.outsegment_index = i->outlabel->info.handle;
130 lsr_cfg_xconnect_set2(g->lsr_handle, &xcon, LSR_CFG_DEL);
131 #else
132 mpls_mpls_xconnect_del(g->mpls_handle, &i->info, &i->outlabel->info);
133 #endif
134 _ldp_outlabel_del_inlabel(i->outlabel, i);
135 MPLS_REFCNT_RELEASE(i->outlabel, ldp_outlabel_delete);
136 i->outlabel = NULL;
138 return MPLS_SUCCESS;
141 mpls_return_enum _ldp_inlabel_add_attr(ldp_inlabel * i, ldp_attr * a)
143 MPLS_ASSERT(i && a);
145 MPLS_REFCNT_HOLD(a);
146 if (mpls_link_list_add_tail(&i->attr_root, a) == MPLS_SUCCESS) {
147 i->reuse_count++;
148 return MPLS_SUCCESS;
150 MPLS_REFCNT_RELEASE(a, ldp_attr_delete);
151 return MPLS_FAILURE;
154 void _ldp_inlabel_del_attr(ldp_inlabel * i, ldp_attr * a)
156 MPLS_ASSERT(i && a);
157 mpls_link_list_remove_data(&i->attr_root, a);
158 MPLS_REFCNT_RELEASE(a, ldp_attr_delete);
159 i->reuse_count--;
162 mpls_return_enum _ldp_inlabel_add_session(ldp_inlabel * i, ldp_session * s)
164 MPLS_ASSERT(i && s);
166 MPLS_REFCNT_HOLD(s);
167 if (mpls_link_list_add_tail(&i->session_root, s) == MPLS_SUCCESS) {
168 return MPLS_SUCCESS;
170 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
171 return MPLS_FAILURE;
174 void _ldp_inlabel_del_session(ldp_inlabel * i, ldp_session * s)
176 MPLS_ASSERT(i && s);
177 mpls_link_list_remove_data(&i->session_root, s);
178 MPLS_REFCNT_RELEASE(s, ldp_session_delete);
181 uint32_t _ldp_inlabel_get_next_index()
183 uint32_t retval = _ldp_inlabel_next_index;
185 _ldp_inlabel_next_index++;
186 if (retval > _ldp_inlabel_next_index) {
187 _ldp_inlabel_next_index = 1;
189 return retval;