Remove address callback. Locally attached address are now
[mpls-ldp-portable.git] / ldp / ldp_if.c
blob859d3cea5b0ae8e03d9de038071a87995c0c7293
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_addr.h"
19 #include "ldp_if.h"
20 #include "ldp_fec.h"
21 #include "ldp_mesg.h"
22 #include "ldp_buf.h"
23 #include "ldp_hello.h"
25 #include "mpls_assert.h"
26 #include "mpls_mm_impl.h"
27 #include "mpls_compare.h"
28 #include "mpls_socket_impl.h"
29 #include "mpls_timer_impl.h"
30 #include "mpls_ifmgr_impl.h"
31 #include "mpls_trace_impl.h"
33 extern uint32_t _ldp_sub_entity_next_index;
35 ldp_if *ldp_if_create(ldp_global *g)
37 ldp_if *i = (ldp_if *) mpls_malloc(sizeof(ldp_if));
39 if (i) {
40 memset(i, 0, sizeof(ldp_if));
42 * note: this is init to 1 for a reason!
43 * We're placing it in the global list, so this is our refcnt
44 * when this refcnt gets to zero, it will be removed from the
45 * global list and deleted
47 MPLS_REFCNT_INIT(i, 1);
48 MPLS_LIST_ELEM_INIT(i, _global);
49 MPLS_LIST_INIT(&i->nh_root, ldp_nexthop);
50 MPLS_LIST_INIT(&i->addr_root, ldp_addr);
51 i->label_space = -1;
52 i->dest.addr.type = MPLS_FAMILY_IPV4;
53 i->dest.addr.u.ipv4 = INADDR_ALLRTRS_GROUP;
54 i->tx_buffer = ldp_buf_create(MPLS_PDUMAXLEN);
55 i->tx_message = ldp_mesg_create();
56 i->index = _ldp_if_get_next_index();
57 i->oper_state = MPLS_OPER_DOWN;
58 i->used_by_ldp = MPLS_BOOL_FALSE;
59 i->used_by_fec = MPLS_BOOL_FALSE;
60 i->is_p2p = MPLS_BOOL_FALSE;
61 _ldp_global_add_if(g, i);
63 return i;
66 void ldp_if_delete(ldp_global *g, ldp_if * i)
68 LDP_PRINT(g->user_data,"if delete\n");
69 _ldp_global_del_if(g, i);
70 MPLS_REFCNT_ASSERT(i, 0);
71 mpls_free(i->tx_buffer);
72 mpls_free(i->tx_message);
73 mpls_free(i);
77 * ldp_if_insert and ldp_if_remove should ONLY be used in conjuction with
78 * adding and removing nexthops (or fecs).
81 ldp_if *ldp_if_insert(ldp_global *g, mpls_if_handle handle)
83 ldp_if *iff = NULL;
85 MPLS_ASSERT(g);
86 MPLS_ASSERT(mpls_if_handle_verify(g->ifmgr_handle, handle) == MPLS_BOOL_TRUE);
88 if ((iff = ldp_if_create(g)) == NULL) {
89 LDP_PRINT(g->user_data,"ldp_if_insert: unable to alloc ldp_if\n");
90 return NULL;
92 iff->used_by_fec = MPLS_BOOL_TRUE;
93 iff->handle = handle;
94 return iff;
97 void ldp_if_remove(ldp_global *g, ldp_if *iff)
99 MPLS_ASSERT(g && iff);
100 iff->used_by_fec = MPLS_BOOL_FALSE;
101 MPLS_REFCNT_RELEASE2(g, iff, ldp_if_delete);
104 void ldp_if_add_nexthop(ldp_if * i, ldp_nexthop * n)
106 ldp_nexthop *np = NULL;
108 MPLS_ASSERT(i && n);
109 MPLS_REFCNT_HOLD(n);
111 ldp_nexthop_add_if(n,i);
113 np = MPLS_LIST_HEAD(&i->nh_root);
114 while (np != NULL) {
115 if (np->index > n->index) {
116 MPLS_LIST_INSERT_BEFORE(&i->nh_root, np, n, _if);
117 return;
119 np = MPLS_LIST_NEXT(&i->nh_root, np, _if);
121 MPLS_LIST_ADD_TAIL(&i->nh_root, n, _if, ldp_nexthop);
124 void ldp_if_del_nexthop(ldp_global *g, ldp_if * i, ldp_nexthop * n)
126 MPLS_ASSERT(i && n);
127 MPLS_LIST_REMOVE(&i->nh_root, n, _if);
128 ldp_nexthop_del_if(g, n);
129 MPLS_REFCNT_RELEASE(n, ldp_nexthop_delete);
132 void ldp_if_add_addr(ldp_if * i, ldp_addr * a)
134 ldp_addr *ap = NULL;
136 MPLS_ASSERT(i && a);
137 MPLS_REFCNT_HOLD(a);
139 ldp_addr_add_if(a,i);
141 ap = MPLS_LIST_HEAD(&i->addr_root);
142 while (ap != NULL) {
143 if (ap->index > a->index) {
144 MPLS_LIST_INSERT_BEFORE(&i->addr_root, ap, a, _if);
145 return;
147 ap = MPLS_LIST_NEXT(&i->addr_root, ap, _if);
149 MPLS_LIST_ADD_TAIL(&i->addr_root, a, _if, ldp_addr);
152 void ldp_if_del_addr(ldp_global *g, ldp_if * i, ldp_addr * a)
154 MPLS_ASSERT(i && a);
155 MPLS_LIST_REMOVE(&i->addr_root, a, _if);
156 ldp_addr_del_if(g, a);
157 MPLS_REFCNT_RELEASE2(g, a, ldp_addr_delete);
160 ldp_addr *ldp_if_addr_find(ldp_if *i, mpls_inet_addr *a)
162 ldp_addr *ap = NULL;
164 MPLS_ASSERT(i && a);
166 ap = MPLS_LIST_HEAD(&i->addr_root);
167 do {
168 if (!mpls_inet_addr_compare(&ap->address, a)) {
169 return ap;
171 ap = MPLS_LIST_NEXT(&i->addr_root, ap, _if);
172 } while((ap = MPLS_LIST_NEXT(&i->addr_root, ap, _if)));
173 return NULL;
176 mpls_return_enum ldp_if_startup(ldp_global * g, ldp_if * i)
178 ldp_entity *e = NULL;
180 LDP_ENTER(g->user_data, "ldp_if_startup");
182 MPLS_ASSERT(i != NULL);
183 e = i->entity;
184 MPLS_ASSERT(e != NULL);
185 MPLS_ASSERT(e->p.iff != NULL);
187 if (mpls_ifmgr_get_address(g->ifmgr_handle, i->handle,
188 &i->local_source_address, NULL, NULL) == MPLS_FAILURE) {
189 goto ldp_if_startup_delay;
191 if (mpls_socket_multicast_if_join(g->socket_handle, g->hello_socket, i,
192 &i->dest.addr) == MPLS_FAILURE) {
193 goto ldp_if_startup_delay;
196 i->dest.port = e->remote_udp_port;
197 if (ldp_hello_send(g, e) == MPLS_FAILURE) {
198 ldp_if_shutdown(g, i);
199 return MPLS_FAILURE;
201 i->oper_state = MPLS_OPER_UP;
203 LDP_EXIT(g->user_data, "ldp_if_startup");
205 return MPLS_SUCCESS;
207 ldp_if_startup_delay:
210 * when a interface update comes in, it will search the global
211 * list of interfaces, and start up the interface then
213 i->oper_state = MPLS_OPER_DOWN;
215 LDP_EXIT(g->user_data, "ldp_if_startup-delayed");
217 return MPLS_SUCCESS;
220 mpls_return_enum ldp_if_shutdown(ldp_global * g, ldp_if * i)
222 ldp_entity *e = NULL;
224 MPLS_ASSERT(i != NULL && ((e = i->entity) != NULL));
226 LDP_ENTER(g->user_data, "ldp_if_shutdown");
228 i->oper_state = MPLS_OPER_DOWN;
230 mpls_socket_multicast_if_drop(g->socket_handle, g->hello_socket, i,
231 &i->dest.addr);
233 mpls_timer_stop(g->timer_handle, i->hellotime_send_timer);
234 mpls_timer_delete(g->timer_handle, i->hellotime_send_timer);
235 i->hellotime_send_timer_duration = 0;
236 i->hellotime_send_timer = 0;
239 * jleu: I'm not sure why these were here, I'm commenting them out,
240 * because I do not see a corresponding HOLD in ldp_if_startup
241 MPLS_REFCNT_RELEASE(e, ldp_entity_delete);
242 MPLS_ASSERT(e != NULL);
245 if (i->hello) {
246 ldp_mesg_delete(i->hello);
247 i->hello = NULL;
250 LDP_EXIT(g->user_data, "ldp_if_shutdown");
252 return MPLS_SUCCESS;
255 mpls_bool ldp_if_is_active(ldp_if * i)
257 if (i && i->entity && i->entity->admin_state == MPLS_ADMIN_ENABLE)
258 return MPLS_BOOL_TRUE;
260 return MPLS_BOOL_FALSE;
263 mpls_return_enum _ldp_if_add_entity(ldp_if * i, ldp_entity * e)
265 if (i && e) {
266 MPLS_REFCNT_HOLD(e);
267 i->entity = e;
268 return MPLS_SUCCESS;
270 return MPLS_FAILURE;
273 ldp_entity *ldp_if_get_entity(ldp_if * i)
275 return i->entity;
278 mpls_return_enum _ldp_if_del_entity(ldp_if * i)
280 if (i && i->entity) {
281 MPLS_REFCNT_RELEASE(i->entity, ldp_entity_delete);
282 i->entity = NULL;
283 return MPLS_SUCCESS;
285 return MPLS_FAILURE;
288 uint32_t _ldp_if_get_next_index()
290 uint32_t retval = _ldp_sub_entity_next_index;
292 _ldp_sub_entity_next_index++;
293 if (retval > _ldp_sub_entity_next_index) {
294 _ldp_sub_entity_next_index = 1;
296 return retval;