]>
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 (a
->source
.link
.ia
->af
< b
->source
.link
.ia
->af
)
53 if (a
->source
.link
.ia
->af
> b
->source
.link
.ia
->af
)
55 return (ldp_addrcmp(a
->source
.link
.ia
->af
,
56 &a
->source
.link
.src_addr
, &b
->source
.link
.src_addr
));
58 if (a
->source
.target
->af
< b
->source
.target
->af
)
60 if (a
->source
.target
->af
> b
->source
.target
->af
)
62 return (ldp_addrcmp(a
->source
.target
->af
,
63 &a
->source
.target
->addr
, &b
->source
.target
->addr
));
65 fatalx("adj_get_af: unknown hello type");
72 adj_new(struct in_addr lsr_id
, struct hello_source
*source
,
73 union ldpd_addr
*addr
)
77 log_debug("%s: lsr-id %s, %s", __func__
, inet_ntoa(lsr_id
),
78 log_hello_src(source
));
80 if ((adj
= calloc(1, sizeof(*adj
))) == NULL
)
85 adj
->source
= *source
;
86 adj
->trans_addr
= *addr
;
88 RB_INSERT(global_adj_head
, &global
.adj_tree
, adj
);
90 switch (source
->type
) {
92 RB_INSERT(ia_adj_head
, &source
->link
.ia
->adj_tree
, adj
);
95 source
->target
->adj
= adj
;
103 adj_del_single(struct adj
*adj
)
105 log_debug("%s: lsr-id %s, %s (%s)", __func__
, inet_ntoa(adj
->lsr_id
),
106 log_hello_src(&adj
->source
), af_name(adj_get_af(adj
)));
108 adj_stop_itimer(adj
);
110 RB_REMOVE(global_adj_head
, &global
.adj_tree
, adj
);
112 RB_REMOVE(nbr_adj_head
, &adj
->nbr
->adj_tree
, adj
);
113 switch (adj
->source
.type
) {
115 RB_REMOVE(ia_adj_head
, &adj
->source
.link
.ia
->adj_tree
, adj
);
118 adj
->source
.target
->adj
= NULL
;
126 adj_del(struct adj
*adj
, uint32_t notif_status
)
128 struct nbr
*nbr
= adj
->nbr
;
134 * If the neighbor still exists but none of its remaining
135 * adjacencies (if any) are from the preferred address-family,
138 if (nbr
&& nbr_adj_count(nbr
, nbr
->af
) == 0) {
139 RB_FOREACH_SAFE(adj
, nbr_adj_head
, &nbr
->adj_tree
, atmp
)
141 session_shutdown(nbr
, notif_status
, 0, 0);
147 adj_find(struct hello_source
*source
)
150 adj
.source
= *source
;
151 return (RB_FIND(global_adj_head
, &global
.adj_tree
, &adj
));
155 adj_get_af(struct adj
*adj
)
157 switch (adj
->source
.type
) {
159 return (adj
->source
.link
.ia
->af
);
161 return (adj
->source
.target
->af
);
163 fatalx("adj_get_af: unknown hello type");
167 /* adjacency timers */
171 adj_itimer(struct thread
*thread
)
173 struct adj
*adj
= THREAD_ARG(thread
);
175 adj
->inactivity_timer
= NULL
;
177 log_debug("%s: lsr-id %s", __func__
, inet_ntoa(adj
->lsr_id
));
179 if (adj
->source
.type
== HELLO_TARGETED
) {
180 if (!(adj
->source
.target
->flags
& F_TNBR_CONFIGURED
) &&
181 adj
->source
.target
->pw_count
== 0) {
182 /* remove dynamic targeted neighbor */
183 tnbr_del(leconf
, adj
->source
.target
);
186 adj
->source
.target
->adj
= NULL
;
189 adj_del(adj
, S_HOLDTIME_EXP
);
195 adj_start_itimer(struct adj
*adj
)
197 THREAD_TIMER_OFF(adj
->inactivity_timer
);
198 adj
->inactivity_timer
= thread_add_timer(master
, adj_itimer
, adj
,
203 adj_stop_itimer(struct adj
*adj
)
205 THREAD_TIMER_OFF(adj
->inactivity_timer
);
208 /* targeted neighbors */
211 tnbr_compare(struct tnbr
*a
, struct tnbr
*b
)
218 return (ldp_addrcmp(a
->af
, &a
->addr
, &b
->addr
));
222 tnbr_new(int af
, union ldpd_addr
*addr
)
226 if ((tnbr
= calloc(1, sizeof(*tnbr
))) == NULL
)
231 tnbr
->state
= TNBR_STA_DOWN
;
237 tnbr_del(struct ldpd_conf
*xconf
, struct tnbr
*tnbr
)
239 tnbr_stop_hello_timer(tnbr
);
241 adj_del(tnbr
->adj
, S_SHUTDOWN
);
242 RB_REMOVE(tnbr_head
, &xconf
->tnbr_tree
, tnbr
);
247 tnbr_find(struct ldpd_conf
*xconf
, int af
, union ldpd_addr
*addr
)
252 return (RB_FIND(tnbr_head
, &xconf
->tnbr_tree
, &tnbr
));
256 tnbr_check(struct ldpd_conf
*xconf
, struct tnbr
*tnbr
)
258 if (!(tnbr
->flags
& (F_TNBR_CONFIGURED
|F_TNBR_DYNAMIC
)) &&
259 tnbr
->pw_count
== 0) {
260 tnbr_del(xconf
, tnbr
);
268 tnbr_update(struct tnbr
*tnbr
)
270 int socket_ok
, rtr_id_ok
;
272 if ((ldp_af_global_get(&global
, tnbr
->af
))->ldp_edisc_socket
!= -1)
277 if (ldp_rtr_id_get(leconf
) != INADDR_ANY
)
282 if (tnbr
->state
== TNBR_STA_DOWN
) {
283 if (!socket_ok
|| !rtr_id_ok
)
286 tnbr
->state
= TNBR_STA_ACTIVE
;
287 send_hello(HELLO_TARGETED
, NULL
, tnbr
);
289 tnbr_start_hello_timer(tnbr
);
290 } else if (tnbr
->state
== TNBR_STA_ACTIVE
) {
291 if (socket_ok
&& rtr_id_ok
)
294 tnbr
->state
= TNBR_STA_DOWN
;
295 tnbr_stop_hello_timer(tnbr
);
300 tnbr_update_all(int af
)
304 /* update targeted neighbors */
305 RB_FOREACH(tnbr
, tnbr_head
, &leconf
->tnbr_tree
)
306 if (tnbr
->af
== af
|| af
== AF_UNSPEC
)
311 tnbr_get_hello_holdtime(struct tnbr
*tnbr
)
313 if ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_holdtime
!= 0)
314 return ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_holdtime
);
316 return (leconf
->thello_holdtime
);
320 tnbr_get_hello_interval(struct tnbr
*tnbr
)
322 if ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_interval
!= 0)
323 return ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_interval
);
325 return (leconf
->thello_interval
);
328 /* target neighbors timers */
332 tnbr_hello_timer(struct thread
*thread
)
334 struct tnbr
*tnbr
= THREAD_ARG(thread
);
336 tnbr
->hello_timer
= NULL
;
337 send_hello(HELLO_TARGETED
, NULL
, tnbr
);
338 tnbr_start_hello_timer(tnbr
);
344 tnbr_start_hello_timer(struct tnbr
*tnbr
)
346 THREAD_TIMER_OFF(tnbr
->hello_timer
);
347 tnbr
->hello_timer
= thread_add_timer(master
, tnbr_hello_timer
, tnbr
,
348 tnbr_get_hello_interval(tnbr
));
352 tnbr_stop_hello_timer(struct tnbr
*tnbr
)
354 THREAD_TIMER_OFF(tnbr
->hello_timer
);
358 adj_to_ctl(struct adj
*adj
)
360 static struct ctl_adj actl
;
362 actl
.af
= adj_get_af(adj
);
363 actl
.id
= adj
->lsr_id
;
364 actl
.type
= adj
->source
.type
;
365 switch (adj
->source
.type
) {
367 memcpy(actl
.ifname
, adj
->source
.link
.ia
->iface
->name
,
368 sizeof(actl
.ifname
));
371 actl
.src_addr
= adj
->source
.target
->addr
;
374 actl
.holdtime
= adj
->holdtime
;
375 actl
.trans_addr
= adj
->trans_addr
;