Minor fix up to label release and withdrawl processing. Main change
[mpls-ldp-portable.git] / ldp / ldp_if.c
blob755ab001a7c384cc3f9e07bb486df010f214584c
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 <netinet/in.h>
12 #include <sys/socket.h>
13 #include "ldp_struct.h"
14 #include "ldp_global.h"
15 #include "ldp_entity.h"
16 #include "ldp_nexthop.h"
17 #include "ldp_nortel.h"
18 #include "ldp_if.h"
19 #include "ldp_fec.h"
20 #include "ldp_mesg.h"
21 #include "ldp_buf.h"
22 #include "ldp_hello.h"
24 #include "mpls_assert.h"
25 #include "mpls_mm_impl.h"
26 #include "mpls_socket_impl.h"
27 #include "mpls_timer_impl.h"
28 #include "mpls_ifmgr_impl.h"
29 #include "mpls_trace_impl.h"
31 extern uint32_t _ldp_sub_entity_next_index;
33 ldp_if *ldp_if_create(ldp_global *g)
35 ldp_if *i = (ldp_if *) mpls_malloc(sizeof(ldp_if));
37 if (i) {
38 memset(i, 0, sizeof(ldp_if));
40 * note: this is init to 1 for a reason!
41 * We're placing it in the global list, so this is our refcnt
42 * when this refcnt gets to zero, it will be removed from the
43 * global list and deleted
45 MPLS_REFCNT_INIT(i, 1);
46 MPLS_LIST_ELEM_INIT(i, _global);
47 MPLS_LIST_INIT(&i->nh_root, ldp_nexthop);
48 i->label_space = -1;
49 i->dest.addr.type = MPLS_FAMILY_IPV4;
50 i->dest.addr.u.ipv4 = INADDR_ALLRTRS_GROUP;
51 i->tx_buffer = ldp_buf_create(MPLS_PDUMAXLEN);
52 i->tx_message = ldp_mesg_create();
53 i->index = _ldp_if_get_next_index();
54 i->oper_state = MPLS_OPER_DOWN;
55 i->used_by_ldp = MPLS_BOOL_FALSE;
56 i->used_by_fec = MPLS_BOOL_FALSE;
57 i->is_p2p = MPLS_BOOL_FALSE;
58 _ldp_global_add_if(g, i);
60 return i;
63 void ldp_if_delete(ldp_global *g, ldp_if * i)
65 LDP_PRINT(g->user_data,"if delete\n");
66 _ldp_global_del_if(g, i);
67 MPLS_REFCNT_ASSERT(i, 0);
68 mpls_free(i->tx_buffer);
69 mpls_free(i->tx_message);
70 mpls_free(i);
74 * ldp_if_insert and ldp_if_remove should ONLY be used in conjuction with
75 * adding and removing nexthops (or fecs).
78 ldp_if *ldp_if_insert(ldp_global *g, mpls_if_handle handle)
80 ldp_if *iff = NULL;
82 MPLS_ASSERT(g);
83 MPLS_ASSERT(mpls_if_handle_verify(g->ifmgr_handle, handle) == MPLS_BOOL_TRUE);
85 if ((iff = ldp_if_create(g)) == NULL) {
86 LDP_PRINT(g->user_data,"ldp_if_insert: unable to alloc ldp_if\n");
87 return NULL;
89 iff->used_by_fec = MPLS_BOOL_TRUE;
90 iff->handle = handle;
91 return iff;
94 void ldp_if_remove(ldp_global *g, ldp_if *iff)
96 MPLS_ASSERT(g && iff);
97 iff->used_by_fec = MPLS_BOOL_FALSE;
98 MPLS_REFCNT_RELEASE2(g, iff, ldp_if_delete);
101 void ldp_if_add_nexthop(ldp_if * i, ldp_nexthop * n)
103 ldp_nexthop *np = NULL;
105 MPLS_ASSERT(i && n);
106 MPLS_REFCNT_HOLD(n);
108 ldp_nexthop_add_if(n,i);
110 np = MPLS_LIST_HEAD(&i->nh_root);
111 while (np != NULL) {
112 if (np->index > n->index) {
113 MPLS_LIST_INSERT_BEFORE(&i->nh_root, np, n, _if);
114 return;
116 np = MPLS_LIST_NEXT(&i->nh_root, np, _if);
118 MPLS_LIST_ADD_TAIL(&i->nh_root, n, _if, ldp_nexthop);
121 void ldp_if_del_nexthop(ldp_global *g, ldp_if * i, ldp_nexthop * n)
123 MPLS_ASSERT(i && n);
124 MPLS_LIST_REMOVE(&i->nh_root, n, _if);
125 ldp_nexthop_del_if(g, n);
126 MPLS_REFCNT_RELEASE(n, ldp_nexthop_delete);
129 mpls_return_enum ldp_if_startup(ldp_global * g, ldp_if * i)
131 ldp_entity *e = NULL;
133 LDP_ENTER(g->user_data, "ldp_if_startup");
135 MPLS_ASSERT(i != NULL);
136 e = i->entity;
137 MPLS_ASSERT(e != NULL);
138 MPLS_ASSERT(e->p.iff != NULL);
140 if (mpls_ifmgr_get_address(g->ifmgr_handle, i->handle,
141 &i->local_source_address, NULL, NULL) == MPLS_FAILURE) {
142 goto ldp_if_startup_delay;
144 if (mpls_socket_multicast_if_join(g->socket_handle, g->hello_socket, i,
145 &i->dest.addr) == MPLS_FAILURE) {
146 goto ldp_if_startup_delay;
149 i->dest.port = e->remote_udp_port;
150 if (ldp_hello_send(g, e) == MPLS_FAILURE) {
151 ldp_if_shutdown(g, i);
152 return MPLS_FAILURE;
154 i->oper_state = MPLS_OPER_UP;
156 LDP_EXIT(g->user_data, "ldp_if_startup");
158 return MPLS_SUCCESS;
160 ldp_if_startup_delay:
163 * when a interface update comes in, it will search the global
164 * list of interfaces, and start up the interface then
166 i->oper_state = MPLS_OPER_DOWN;
168 LDP_EXIT(g->user_data, "ldp_if_startup-delayed");
170 return MPLS_SUCCESS;
173 mpls_return_enum ldp_if_shutdown(ldp_global * g, ldp_if * i)
175 ldp_entity *e = NULL;
177 MPLS_ASSERT(i != NULL && ((e = i->entity) != NULL));
179 LDP_ENTER(g->user_data, "ldp_if_shutdown");
181 i->oper_state = MPLS_OPER_DOWN;
183 mpls_socket_multicast_if_drop(g->socket_handle, g->hello_socket, i,
184 &i->dest.addr);
186 mpls_timer_stop(g->timer_handle, i->hellotime_send_timer);
187 mpls_timer_delete(g->timer_handle, i->hellotime_send_timer);
188 i->hellotime_send_timer_duration = 0;
189 i->hellotime_send_timer = 0;
192 * jleu: I'm not sure why these were here, I'm commenting them out,
193 * because I do not see a corresponding HOLD in ldp_if_startup
194 MPLS_REFCNT_RELEASE(e, ldp_entity_delete);
195 MPLS_ASSERT(e != NULL);
198 if (i->hello) {
199 ldp_mesg_delete(i->hello);
200 i->hello = NULL;
203 LDP_EXIT(g->user_data, "ldp_if_shutdown");
205 return MPLS_SUCCESS;
208 mpls_bool ldp_if_is_active(ldp_if * i)
210 if (i && i->entity && i->entity->admin_state == MPLS_ADMIN_ENABLE)
211 return MPLS_BOOL_TRUE;
213 return MPLS_BOOL_FALSE;
216 mpls_return_enum _ldp_if_add_entity(ldp_if * i, ldp_entity * e)
218 if (i && e) {
219 MPLS_REFCNT_HOLD(e);
220 i->entity = e;
221 return MPLS_SUCCESS;
223 return MPLS_FAILURE;
226 ldp_entity *ldp_if_get_entity(ldp_if * i)
228 return i->entity;
231 mpls_return_enum _ldp_if_del_entity(ldp_if * i)
233 if (i && i->entity) {
234 MPLS_REFCNT_RELEASE(i->entity, ldp_entity_delete);
235 i->entity = NULL;
236 return MPLS_SUCCESS;
238 return MPLS_FAILURE;
241 uint32_t _ldp_if_get_next_index()
243 uint32_t retval = _ldp_sub_entity_next_index;
245 _ldp_sub_entity_next_index++;
246 if (retval > _ldp_sub_entity_next_index) {
247 _ldp_sub_entity_next_index = 1;
249 return retval;