]>
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(const struct adj
*, const struct adj
*);
29 static int adj_itimer(struct thread
*);
30 static __inline
int tnbr_compare(const struct tnbr
*, const struct tnbr
*);
31 static void tnbr_del(struct ldpd_conf
*, struct tnbr
*);
32 static void tnbr_start(struct tnbr
*);
33 static void tnbr_stop(struct tnbr
*);
34 static int tnbr_hello_timer(struct thread
*);
35 static void tnbr_start_hello_timer(struct tnbr
*);
36 static void tnbr_stop_hello_timer(struct tnbr
*);
38 RB_GENERATE(global_adj_head
, adj
, global_entry
, adj_compare
)
39 RB_GENERATE(nbr_adj_head
, adj
, nbr_entry
, adj_compare
)
40 RB_GENERATE(ia_adj_head
, adj
, ia_entry
, adj_compare
)
41 RB_GENERATE(tnbr_head
, tnbr
, entry
, tnbr_compare
)
44 adj_compare(const struct adj
*a
, const struct adj
*b
)
46 if (adj_get_af(a
) < adj_get_af(b
))
48 if (adj_get_af(a
) > adj_get_af(b
))
51 if (ntohl(a
->lsr_id
.s_addr
) < ntohl(b
->lsr_id
.s_addr
))
53 if (ntohl(a
->lsr_id
.s_addr
) > ntohl(b
->lsr_id
.s_addr
))
56 if (a
->source
.type
< b
->source
.type
)
58 if (a
->source
.type
> b
->source
.type
)
61 switch (a
->source
.type
) {
63 if (if_cmp_name_func((char *)a
->source
.link
.ia
->iface
->name
,
64 (char *)b
->source
.link
.ia
->iface
->name
) < 0)
66 if (if_cmp_name_func((char *)a
->source
.link
.ia
->iface
->name
,
67 (char *)b
->source
.link
.ia
->iface
->name
) > 0)
69 return (ldp_addrcmp(a
->source
.link
.ia
->af
,
70 &a
->source
.link
.src_addr
, &b
->source
.link
.src_addr
));
72 return (ldp_addrcmp(a
->source
.target
->af
,
73 &a
->source
.target
->addr
, &b
->source
.target
->addr
));
75 fatalx("adj_compare: unknown hello type");
82 adj_new(struct in_addr lsr_id
, struct hello_source
*source
,
83 union ldpd_addr
*addr
)
87 log_debug("%s: lsr-id %s, %s", __func__
, inet_ntoa(lsr_id
),
88 log_hello_src(source
));
90 if ((adj
= calloc(1, sizeof(*adj
))) == NULL
)
95 adj
->source
= *source
;
96 adj
->trans_addr
= *addr
;
98 RB_INSERT(global_adj_head
, &global
.adj_tree
, adj
);
100 switch (source
->type
) {
102 RB_INSERT(ia_adj_head
, &source
->link
.ia
->adj_tree
, adj
);
105 source
->target
->adj
= adj
;
113 adj_del(struct adj
*adj
, uint32_t notif_status
)
115 struct nbr
*nbr
= adj
->nbr
;
117 log_debug("%s: lsr-id %s, %s (%s)", __func__
, inet_ntoa(adj
->lsr_id
),
118 log_hello_src(&adj
->source
), af_name(adj_get_af(adj
)));
120 adj_stop_itimer(adj
);
122 RB_REMOVE(global_adj_head
, &global
.adj_tree
, adj
);
124 RB_REMOVE(nbr_adj_head
, &nbr
->adj_tree
, adj
);
125 switch (adj
->source
.type
) {
127 RB_REMOVE(ia_adj_head
, &adj
->source
.link
.ia
->adj_tree
, adj
);
130 adj
->source
.target
->adj
= NULL
;
137 * If the neighbor still exists but none of its remaining
138 * adjacencies (if any) are from the preferred address-family,
141 if (nbr
&& nbr_adj_count(nbr
, nbr
->af
) == 0) {
142 session_shutdown(nbr
, notif_status
, 0, 0);
148 adj_find(struct in_addr lsr_id
, struct hello_source
*source
)
152 adj
.source
= *source
;
153 return (RB_FIND(global_adj_head
, &global
.adj_tree
, &adj
));
157 adj_get_af(const struct adj
*adj
)
159 switch (adj
->source
.type
) {
161 return (adj
->source
.link
.ia
->af
);
163 return (adj
->source
.target
->af
);
165 fatalx("adj_get_af: unknown hello type");
169 /* adjacency timers */
173 adj_itimer(struct thread
*thread
)
175 struct adj
*adj
= THREAD_ARG(thread
);
177 adj
->inactivity_timer
= NULL
;
179 log_debug("%s: lsr-id %s", __func__
, inet_ntoa(adj
->lsr_id
));
181 if (adj
->source
.type
== HELLO_TARGETED
) {
182 if (!(adj
->source
.target
->flags
& F_TNBR_CONFIGURED
) &&
183 adj
->source
.target
->pw_count
== 0) {
184 /* remove dynamic targeted neighbor */
185 tnbr_del(leconf
, adj
->source
.target
);
190 adj_del(adj
, S_HOLDTIME_EXP
);
196 adj_start_itimer(struct adj
*adj
)
198 THREAD_TIMER_OFF(adj
->inactivity_timer
);
199 adj
->inactivity_timer
= NULL
;
200 thread_add_timer(master
, adj_itimer
, adj
, adj
->holdtime
,
201 &adj
->inactivity_timer
);
205 adj_stop_itimer(struct adj
*adj
)
207 THREAD_TIMER_OFF(adj
->inactivity_timer
);
210 /* targeted neighbors */
213 tnbr_compare(const struct tnbr
*a
, const struct tnbr
*b
)
220 return (ldp_addrcmp(a
->af
, &a
->addr
, &b
->addr
));
224 tnbr_new(int af
, union ldpd_addr
*addr
)
228 if ((tnbr
= calloc(1, sizeof(*tnbr
))) == NULL
)
233 tnbr
->state
= TNBR_STA_DOWN
;
239 tnbr_del(struct ldpd_conf
*xconf
, struct tnbr
*tnbr
)
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_start(struct tnbr
*tnbr
)
270 send_hello(HELLO_TARGETED
, NULL
, tnbr
);
271 tnbr_start_hello_timer(tnbr
);
272 tnbr
->state
= TNBR_STA_ACTIVE
;
276 tnbr_stop(struct tnbr
*tnbr
)
278 tnbr_stop_hello_timer(tnbr
);
280 adj_del(tnbr
->adj
, S_SHUTDOWN
);
281 tnbr
->state
= TNBR_STA_DOWN
;
285 tnbr_update(struct tnbr
*tnbr
)
287 int socket_ok
, rtr_id_ok
;
289 if ((ldp_af_global_get(&global
, tnbr
->af
))->ldp_edisc_socket
!= -1)
294 if (ldp_rtr_id_get(leconf
) != INADDR_ANY
)
299 if (tnbr
->state
== TNBR_STA_DOWN
) {
300 if (!socket_ok
|| !rtr_id_ok
)
304 } else if (tnbr
->state
== TNBR_STA_ACTIVE
) {
305 if (socket_ok
&& rtr_id_ok
)
313 tnbr_update_all(int af
)
317 /* update targeted neighbors */
318 RB_FOREACH(tnbr
, tnbr_head
, &leconf
->tnbr_tree
)
319 if (tnbr
->af
== af
|| af
== AF_UNSPEC
)
324 tnbr_get_hello_holdtime(struct tnbr
*tnbr
)
326 if ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_holdtime
!= 0)
327 return ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_holdtime
);
329 return (leconf
->thello_holdtime
);
333 tnbr_get_hello_interval(struct tnbr
*tnbr
)
335 if ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_interval
!= 0)
336 return ((ldp_af_conf_get(leconf
, tnbr
->af
))->thello_interval
);
338 return (leconf
->thello_interval
);
341 /* target neighbors timers */
345 tnbr_hello_timer(struct thread
*thread
)
347 struct tnbr
*tnbr
= THREAD_ARG(thread
);
349 tnbr
->hello_timer
= NULL
;
350 send_hello(HELLO_TARGETED
, NULL
, tnbr
);
351 tnbr_start_hello_timer(tnbr
);
357 tnbr_start_hello_timer(struct tnbr
*tnbr
)
359 THREAD_TIMER_OFF(tnbr
->hello_timer
);
360 tnbr
->hello_timer
= NULL
;
361 thread_add_timer(master
, tnbr_hello_timer
, tnbr
, tnbr_get_hello_interval(tnbr
),
366 tnbr_stop_hello_timer(struct tnbr
*tnbr
)
368 THREAD_TIMER_OFF(tnbr
->hello_timer
);
372 adj_to_ctl(struct adj
*adj
)
374 static struct ctl_adj actl
;
376 actl
.af
= adj_get_af(adj
);
377 actl
.id
= adj
->lsr_id
;
378 actl
.type
= adj
->source
.type
;
379 switch (adj
->source
.type
) {
381 memcpy(actl
.ifname
, adj
->source
.link
.ia
->iface
->name
,
382 sizeof(actl
.ifname
));
383 actl
.src_addr
= adj
->source
.link
.src_addr
;
386 actl
.src_addr
= adj
->source
.target
->addr
;
389 actl
.holdtime
= adj
->holdtime
;
390 actl
.holdtime_remaining
=
391 thread_timer_remain_second(adj
->inactivity_timer
);
392 actl
.trans_addr
= adj
->trans_addr
;
393 actl
.ds_tlv
= adj
->ds_tlv
;