]>
git.proxmox.com Git - mirror_frr.git/blob - ldpd/adjacency.c
4 * Copyright (c) 2013, 2015 Renato Westphal <renato@openbsd.org>
5 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
6 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
7 * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 static __inline
int adj_compare(struct adj
*, struct adj
*);
29 static int adj_itimer(struct thread
*);
30 static __inline
int tnbr_compare(struct tnbr
*, struct tnbr
*);
31 static void tnbr_del(struct ldpd_conf
*, struct tnbr
*);
32 static int tnbr_hello_timer(struct thread
*);
33 static void tnbr_start_hello_timer(struct tnbr
*);
34 static void tnbr_stop_hello_timer(struct tnbr
*);
36 RB_GENERATE(global_adj_head
, adj
, global_entry
, adj_compare
)
37 RB_GENERATE(nbr_adj_head
, adj
, nbr_entry
, adj_compare
)
38 RB_GENERATE(ia_adj_head
, adj
, ia_entry
, adj_compare
)
39 RB_GENERATE(tnbr_head
, tnbr
, entry
, tnbr_compare
)
42 adj_compare(struct adj
*a
, struct adj
*b
)
44 if (a
->source
.type
< b
->source
.type
)
46 if (a
->source
.type
> b
->source
.type
)
49 switch (a
->source
.type
) {
51 if (strcmp(a
->source
.link
.ia
->iface
->name
,
52 b
->source
.link
.ia
->iface
->name
) < 0)
54 if (strcmp(a
->source
.link
.ia
->iface
->name
,
55 b
->source
.link
.ia
->iface
->name
) > 0)
57 if (a
->source
.link
.ia
->af
< b
->source
.link
.ia
->af
)
59 if (a
->source
.link
.ia
->af
> b
->source
.link
.ia
->af
)
61 return (ldp_addrcmp(a
->source
.link
.ia
->af
,
62 &a
->source
.link
.src_addr
, &b
->source
.link
.src_addr
));
64 if (a
->source
.target
->af
< b
->source
.target
->af
)
66 if (a
->source
.target
->af
> b
->source
.target
->af
)
68 return (ldp_addrcmp(a
->source
.target
->af
,
69 &a
->source
.target
->addr
, &b
->source
.target
->addr
));
71 fatalx("adj_get_af: unknown hello type");
78 adj_new(struct in_addr lsr_id
, struct hello_source
*source
,
79 union ldpd_addr
*addr
)
83 log_debug("%s: lsr-id %s, %s", __func__
, inet_ntoa(lsr_id
),
84 log_hello_src(source
));
86 if ((adj
= calloc(1, sizeof(*adj
))) == NULL
)
91 adj
->source
= *source
;
92 adj
->trans_addr
= *addr
;
94 RB_INSERT(global_adj_head
, &global
.adj_tree
, adj
);
96 switch (source
->type
) {
98 RB_INSERT(ia_adj_head
, &source
->link
.ia
->adj_tree
, adj
);
101 source
->target
->adj
= adj
;
109 adj_del_single(struct adj
*adj
)
111 log_debug("%s: lsr-id %s, %s (%s)", __func__
, inet_ntoa(adj
->lsr_id
),
112 log_hello_src(&adj
->source
), af_name(adj_get_af(adj
)));
114 adj_stop_itimer(adj
);
116 RB_REMOVE(global_adj_head
, &global
.adj_tree
, adj
);
118 RB_REMOVE(nbr_adj_head
, &adj
->nbr
->adj_tree
, adj
);
119 switch (adj
->source
.type
) {
121 RB_REMOVE(ia_adj_head
, &adj
->source
.link
.ia
->adj_tree
, adj
);
124 adj
->source
.target
->adj
= NULL
;
132 adj_del(struct adj
*adj
, uint32_t notif_status
)
134 struct nbr
*nbr
= adj
->nbr
;
140 * If the neighbor still exists but none of its remaining
141 * adjacencies (if any) are from the preferred address-family,
144 if (nbr
&& nbr_adj_count(nbr
, nbr
->af
) == 0) {
145 RB_FOREACH_SAFE(adj
, nbr_adj_head
, &nbr
->adj_tree
, atmp
)
147 session_shutdown(nbr
, notif_status
, 0, 0);
153 adj_find(struct hello_source
*source
)
156 adj
.source
= *source
;
157 return (RB_FIND(global_adj_head
, &global
.adj_tree
, &adj
));
161 adj_get_af(struct adj
*adj
)
163 switch (adj
->source
.type
) {
165 return (adj
->source
.link
.ia
->af
);
167 return (adj
->source
.target
->af
);
169 fatalx("adj_get_af: unknown hello type");
173 /* adjacency timers */
177 adj_itimer(struct thread
*thread
)
179 struct adj
*adj
= THREAD_ARG(thread
);
181 adj
->inactivity_timer
= NULL
;
183 log_debug("%s: lsr-id %s", __func__
, inet_ntoa(adj
->lsr_id
));
185 if (adj
->source
.type
== HELLO_TARGETED
) {
186 if (!(adj
->source
.target
->flags
& F_TNBR_CONFIGURED
) &&
187 adj
->source
.target
->pw_count
== 0) {
188 /* remove dynamic targeted neighbor */
189 tnbr_del(leconf
, adj
->source
.target
);
192 adj
->source
.target
->adj
= NULL
;
195 adj_del(adj
, S_HOLDTIME_EXP
);
201 adj_start_itimer(struct adj
*adj
)
203 THREAD_TIMER_OFF(adj
->inactivity_timer
);
204 adj
->inactivity_timer
= thread_add_timer(master
, adj_itimer
, adj
,
209 adj_stop_itimer(struct adj
*adj
)
211 THREAD_TIMER_OFF(adj
->inactivity_timer
);
214 /* targeted neighbors */
217 tnbr_compare(struct tnbr
*a
, struct tnbr
*b
)
224 return (ldp_addrcmp(a
->af
, &a
->addr
, &b
->addr
));
228 tnbr_new(int af
, union ldpd_addr
*addr
)
232 if ((tnbr
= calloc(1, sizeof(*tnbr
))) == NULL
)
237 tnbr
->state
= TNBR_STA_DOWN
;
243 tnbr_del(struct ldpd_conf
*xconf
, struct tnbr
*tnbr
)
245 tnbr_stop_hello_timer(tnbr
);
247 adj_del(tnbr
->adj
, S_SHUTDOWN
);
248 RB_REMOVE(tnbr_head
, &xconf
->tnbr_tree
, tnbr
);
253 tnbr_find(struct ldpd_conf
*xconf
, int af
, union ldpd_addr
*addr
)
258 return (RB_FIND(tnbr_head
, &xconf
->tnbr_tree
, &tnbr
));
262 tnbr_check(struct ldpd_conf
*xconf
, struct tnbr
*tnbr
)
264 if (!(tnbr
->flags
& (F_TNBR_CONFIGURED
|F_TNBR_DYNAMIC
)) &&
265 tnbr
->pw_count
== 0) {
266 tnbr_del(xconf
, tnbr
);
274 tnbr_update(struct tnbr
*tnbr
)
276 int socket_ok
, rtr_id_ok
;
278 if ((ldp_af_global_get(&global
, tnbr
->af
))->ldp_edisc_socket
!= -1)
283 if (ldp_rtr_id_get(leconf
) != INADDR_ANY
)
288 if (tnbr
->state
== TNBR_STA_DOWN
) {
289 if (!socket_ok
|| !rtr_id_ok
)
292 tnbr
->state
= TNBR_STA_ACTIVE
;
293 send_hello(HELLO_TARGETED
, NULL
, tnbr
);
295 tnbr_start_hello_timer(tnbr
);
296 } else if (tnbr
->state
== TNBR_STA_ACTIVE
) {
297 if (socket_ok
&& rtr_id_ok
)
300 tnbr
->state
= TNBR_STA_DOWN
;
301 tnbr_stop_hello_timer(tnbr
);
306 tnbr_update_all(int af
)
310 /* update targeted neighbors */
311 RB_FOREACH(tnbr
, tnbr_head
, &leconf
->tnbr_tree
)
312 if (tnbr
->af
== af
|| af
== AF_UNSPEC
)
317 tnbr_get_hello_holdtime(struct tnbr
*tnbr
)
319 if ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_holdtime
!= 0)
320 return ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_holdtime
);
322 return (leconf
->thello_holdtime
);
326 tnbr_get_hello_interval(struct tnbr
*tnbr
)
328 if ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_interval
!= 0)
329 return ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_interval
);
331 return (leconf
->thello_interval
);
334 /* target neighbors timers */
338 tnbr_hello_timer(struct thread
*thread
)
340 struct tnbr
*tnbr
= THREAD_ARG(thread
);
342 tnbr
->hello_timer
= NULL
;
343 send_hello(HELLO_TARGETED
, NULL
, tnbr
);
344 tnbr_start_hello_timer(tnbr
);
350 tnbr_start_hello_timer(struct tnbr
*tnbr
)
352 THREAD_TIMER_OFF(tnbr
->hello_timer
);
353 tnbr
->hello_timer
= thread_add_timer(master
, tnbr_hello_timer
, tnbr
,
354 tnbr_get_hello_interval(tnbr
));
358 tnbr_stop_hello_timer(struct tnbr
*tnbr
)
360 THREAD_TIMER_OFF(tnbr
->hello_timer
);
364 adj_to_ctl(struct adj
*adj
)
366 static struct ctl_adj actl
;
368 actl
.af
= adj_get_af(adj
);
369 actl
.id
= adj
->lsr_id
;
370 actl
.type
= adj
->source
.type
;
371 switch (adj
->source
.type
) {
373 memcpy(actl
.ifname
, adj
->source
.link
.ia
->iface
->name
,
374 sizeof(actl
.ifname
));
377 actl
.src_addr
= adj
->source
.target
->addr
;
380 actl
.holdtime
= adj
->holdtime
;
381 actl
.trans_addr
= adj
->trans_addr
;