]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/adjacency.c
release: FRR 3.0-rc1
[mirror_frr.git] / ldpd / adjacency.c
1 /* $OpenBSD$ */
2
3 /*
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>
8 *
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.
12 *
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.
20 */
21
22 #include <zebra.h>
23
24 #include "ldpd.h"
25 #include "ldpe.h"
26 #include "log.h"
27
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 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 *);
37
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)
42
43 static __inline int
44 adj_compare(struct adj *a, struct adj *b)
45 {
46 if (adj_get_af(a) < adj_get_af(b))
47 return (-1);
48 if (adj_get_af(a) > adj_get_af(b))
49 return (1);
50
51 if (ntohl(a->lsr_id.s_addr) < ntohl(b->lsr_id.s_addr))
52 return (-1);
53 if (ntohl(a->lsr_id.s_addr) > ntohl(b->lsr_id.s_addr))
54 return (1);
55
56 if (a->source.type < b->source.type)
57 return (-1);
58 if (a->source.type > b->source.type)
59 return (1);
60
61 switch (a->source.type) {
62 case HELLO_LINK:
63 if (strcmp(a->source.link.ia->iface->name,
64 b->source.link.ia->iface->name) < 0)
65 return (-1);
66 if (strcmp(a->source.link.ia->iface->name,
67 b->source.link.ia->iface->name) > 0)
68 return (1);
69 return (ldp_addrcmp(a->source.link.ia->af,
70 &a->source.link.src_addr, &b->source.link.src_addr));
71 case HELLO_TARGETED:
72 return (ldp_addrcmp(a->source.target->af,
73 &a->source.target->addr, &b->source.target->addr));
74 default:
75 fatalx("adj_compare: unknown hello type");
76 }
77
78 return (0);
79 }
80
81 struct adj *
82 adj_new(struct in_addr lsr_id, struct hello_source *source,
83 union ldpd_addr *addr)
84 {
85 struct adj *adj;
86
87 log_debug("%s: lsr-id %s, %s", __func__, inet_ntoa(lsr_id),
88 log_hello_src(source));
89
90 if ((adj = calloc(1, sizeof(*adj))) == NULL)
91 fatal(__func__);
92
93 adj->lsr_id = lsr_id;
94 adj->nbr = NULL;
95 adj->source = *source;
96 adj->trans_addr = *addr;
97
98 RB_INSERT(global_adj_head, &global.adj_tree, adj);
99
100 switch (source->type) {
101 case HELLO_LINK:
102 RB_INSERT(ia_adj_head, &source->link.ia->adj_tree, adj);
103 break;
104 case HELLO_TARGETED:
105 source->target->adj = adj;
106 break;
107 }
108
109 return (adj);
110 }
111
112 void
113 adj_del(struct adj *adj, uint32_t notif_status)
114 {
115 struct nbr *nbr = adj->nbr;
116
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)));
119
120 adj_stop_itimer(adj);
121
122 RB_REMOVE(global_adj_head, &global.adj_tree, adj);
123 if (nbr)
124 RB_REMOVE(nbr_adj_head, &nbr->adj_tree, adj);
125 switch (adj->source.type) {
126 case HELLO_LINK:
127 RB_REMOVE(ia_adj_head, &adj->source.link.ia->adj_tree, adj);
128 break;
129 case HELLO_TARGETED:
130 adj->source.target->adj = NULL;
131 break;
132 }
133
134 free(adj);
135
136 /*
137 * If the neighbor still exists but none of its remaining
138 * adjacencies (if any) are from the preferred address-family,
139 * then delete it.
140 */
141 if (nbr && nbr_adj_count(nbr, nbr->af) == 0) {
142 session_shutdown(nbr, notif_status, 0, 0);
143 nbr_del(nbr);
144 }
145 }
146
147 struct adj *
148 adj_find(struct in_addr lsr_id, struct hello_source *source)
149 {
150 struct adj adj;
151 adj.lsr_id = lsr_id;
152 adj.source = *source;
153 return (RB_FIND(global_adj_head, &global.adj_tree, &adj));
154 }
155
156 int
157 adj_get_af(struct adj *adj)
158 {
159 switch (adj->source.type) {
160 case HELLO_LINK:
161 return (adj->source.link.ia->af);
162 case HELLO_TARGETED:
163 return (adj->source.target->af);
164 default:
165 fatalx("adj_get_af: unknown hello type");
166 }
167 }
168
169 /* adjacency timers */
170
171 /* ARGSUSED */
172 static int
173 adj_itimer(struct thread *thread)
174 {
175 struct adj *adj = THREAD_ARG(thread);
176
177 adj->inactivity_timer = NULL;
178
179 log_debug("%s: lsr-id %s", __func__, inet_ntoa(adj->lsr_id));
180
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);
186 return (0);
187 }
188 }
189
190 adj_del(adj, S_HOLDTIME_EXP);
191
192 return (0);
193 }
194
195 void
196 adj_start_itimer(struct adj *adj)
197 {
198 THREAD_TIMER_OFF(adj->inactivity_timer);
199 adj->inactivity_timer = thread_add_timer(master, adj_itimer, adj,
200 adj->holdtime);
201 }
202
203 void
204 adj_stop_itimer(struct adj *adj)
205 {
206 THREAD_TIMER_OFF(adj->inactivity_timer);
207 }
208
209 /* targeted neighbors */
210
211 static __inline int
212 tnbr_compare(struct tnbr *a, struct tnbr *b)
213 {
214 if (a->af < b->af)
215 return (-1);
216 if (a->af > b->af)
217 return (1);
218
219 return (ldp_addrcmp(a->af, &a->addr, &b->addr));
220 }
221
222 struct tnbr *
223 tnbr_new(int af, union ldpd_addr *addr)
224 {
225 struct tnbr *tnbr;
226
227 if ((tnbr = calloc(1, sizeof(*tnbr))) == NULL)
228 fatal(__func__);
229
230 tnbr->af = af;
231 tnbr->addr = *addr;
232 tnbr->state = TNBR_STA_DOWN;
233
234 return (tnbr);
235 }
236
237 static void
238 tnbr_del(struct ldpd_conf *xconf, struct tnbr *tnbr)
239 {
240 tnbr_stop(tnbr);
241 RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr);
242 free(tnbr);
243 }
244
245 struct tnbr *
246 tnbr_find(struct ldpd_conf *xconf, int af, union ldpd_addr *addr)
247 {
248 struct tnbr tnbr;
249 tnbr.af = af;
250 tnbr.addr = *addr;
251 return (RB_FIND(tnbr_head, &xconf->tnbr_tree, &tnbr));
252 }
253
254 struct tnbr *
255 tnbr_check(struct ldpd_conf *xconf, struct tnbr *tnbr)
256 {
257 if (!(tnbr->flags & (F_TNBR_CONFIGURED|F_TNBR_DYNAMIC)) &&
258 tnbr->pw_count == 0) {
259 tnbr_del(xconf, tnbr);
260 return (NULL);
261 }
262
263 return (tnbr);
264 }
265
266 static void
267 tnbr_start(struct tnbr *tnbr)
268 {
269 send_hello(HELLO_TARGETED, NULL, tnbr);
270 tnbr_start_hello_timer(tnbr);
271 tnbr->state = TNBR_STA_ACTIVE;
272 }
273
274 static void
275 tnbr_stop(struct tnbr *tnbr)
276 {
277 tnbr_stop_hello_timer(tnbr);
278 if (tnbr->adj)
279 adj_del(tnbr->adj, S_SHUTDOWN);
280 tnbr->state = TNBR_STA_DOWN;
281 }
282
283 void
284 tnbr_update(struct tnbr *tnbr)
285 {
286 int socket_ok, rtr_id_ok;
287
288 if ((ldp_af_global_get(&global, tnbr->af))->ldp_edisc_socket != -1)
289 socket_ok = 1;
290 else
291 socket_ok = 0;
292
293 if (ldp_rtr_id_get(leconf) != INADDR_ANY)
294 rtr_id_ok = 1;
295 else
296 rtr_id_ok = 0;
297
298 if (tnbr->state == TNBR_STA_DOWN) {
299 if (!socket_ok || !rtr_id_ok)
300 return;
301
302 tnbr_start(tnbr);
303 } else if (tnbr->state == TNBR_STA_ACTIVE) {
304 if (socket_ok && rtr_id_ok)
305 return;
306
307 tnbr_stop(tnbr);
308 }
309 }
310
311 void
312 tnbr_update_all(int af)
313 {
314 struct tnbr *tnbr;
315
316 /* update targeted neighbors */
317 RB_FOREACH(tnbr, tnbr_head, &leconf->tnbr_tree)
318 if (tnbr->af == af || af == AF_UNSPEC)
319 tnbr_update(tnbr);
320 }
321
322 uint16_t
323 tnbr_get_hello_holdtime(struct tnbr *tnbr)
324 {
325 if ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime != 0)
326 return ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime);
327
328 return (leconf->thello_holdtime);
329 }
330
331 uint16_t
332 tnbr_get_hello_interval(struct tnbr *tnbr)
333 {
334 if ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval != 0)
335 return ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval);
336
337 return (leconf->thello_interval);
338 }
339
340 /* target neighbors timers */
341
342 /* ARGSUSED */
343 static int
344 tnbr_hello_timer(struct thread *thread)
345 {
346 struct tnbr *tnbr = THREAD_ARG(thread);
347
348 tnbr->hello_timer = NULL;
349 send_hello(HELLO_TARGETED, NULL, tnbr);
350 tnbr_start_hello_timer(tnbr);
351
352 return (0);
353 }
354
355 static void
356 tnbr_start_hello_timer(struct tnbr *tnbr)
357 {
358 THREAD_TIMER_OFF(tnbr->hello_timer);
359 tnbr->hello_timer = thread_add_timer(master, tnbr_hello_timer, tnbr,
360 tnbr_get_hello_interval(tnbr));
361 }
362
363 static void
364 tnbr_stop_hello_timer(struct tnbr *tnbr)
365 {
366 THREAD_TIMER_OFF(tnbr->hello_timer);
367 }
368
369 struct ctl_adj *
370 adj_to_ctl(struct adj *adj)
371 {
372 static struct ctl_adj actl;
373
374 actl.af = adj_get_af(adj);
375 actl.id = adj->lsr_id;
376 actl.type = adj->source.type;
377 switch (adj->source.type) {
378 case HELLO_LINK:
379 memcpy(actl.ifname, adj->source.link.ia->iface->name,
380 sizeof(actl.ifname));
381 actl.src_addr = adj->source.link.src_addr;
382 break;
383 case HELLO_TARGETED:
384 actl.src_addr = adj->source.target->addr;
385 break;
386 }
387 actl.holdtime = adj->holdtime;
388 actl.holdtime_remaining =
389 thread_timer_remain_second(adj->inactivity_timer);
390 actl.trans_addr = adj->trans_addr;
391 actl.ds_tlv = adj->ds_tlv;
392
393 return (&actl);
394 }