]> git.proxmox.com Git - mirror_frr.git/blob - ldpd/adjacency.c
Merge pull request #7261 from Niral-Networks/niral_dev_vrf_ospf6
[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(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 *);
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(const struct adj *a, const 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 (if_cmp_name_func(a->source.link.ia->iface->name,
64 b->source.link.ia->iface->name) < 0)
65 return (-1);
66 if (if_cmp_name_func(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 %pI4, %s", __func__, &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 %pI4, %s (%s)", __func__, &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
129 if (nbr)
130 ldp_sync_fsm_adj_event(adj, LDP_SYNC_EVT_ADJ_DEL);
131 break;
132 case HELLO_TARGETED:
133 adj->source.target->adj = NULL;
134 break;
135 }
136
137 free(adj);
138
139 /*
140 * If the neighbor still exists but none of its remaining
141 * adjacencies (if any) are from the preferred address-family,
142 * then delete it.
143 */
144 if (nbr && nbr_adj_count(nbr, nbr->af) == 0) {
145 session_shutdown(nbr, notif_status, 0, 0);
146 nbr_del(nbr);
147 }
148 }
149
150 struct adj *
151 adj_find(struct in_addr lsr_id, struct hello_source *source)
152 {
153 struct adj adj;
154 adj.lsr_id = lsr_id;
155 adj.source = *source;
156 return (RB_FIND(global_adj_head, &global.adj_tree, &adj));
157 }
158
159 int
160 adj_get_af(const struct adj *adj)
161 {
162 switch (adj->source.type) {
163 case HELLO_LINK:
164 return (adj->source.link.ia->af);
165 case HELLO_TARGETED:
166 return (adj->source.target->af);
167 default:
168 fatalx("adj_get_af: unknown hello type");
169 }
170 }
171
172 /* adjacency timers */
173
174 /* ARGSUSED */
175 static int
176 adj_itimer(struct thread *thread)
177 {
178 struct adj *adj = THREAD_ARG(thread);
179
180 adj->inactivity_timer = NULL;
181
182 log_debug("%s: lsr-id %pI4", __func__, &adj->lsr_id);
183
184 if (adj->source.type == HELLO_TARGETED) {
185 if (!(adj->source.target->flags & F_TNBR_CONFIGURED) &&
186 adj->source.target->pw_count == 0) {
187 /* remove dynamic targeted neighbor */
188 tnbr_del(leconf, adj->source.target);
189 return (0);
190 }
191 }
192
193 adj_del(adj, S_HOLDTIME_EXP);
194
195 return (0);
196 }
197
198 void
199 adj_start_itimer(struct adj *adj)
200 {
201 thread_cancel(&adj->inactivity_timer);
202 adj->inactivity_timer = NULL;
203 thread_add_timer(master, adj_itimer, adj, adj->holdtime,
204 &adj->inactivity_timer);
205 }
206
207 void
208 adj_stop_itimer(struct adj *adj)
209 {
210 thread_cancel(&adj->inactivity_timer);
211 }
212
213 /* targeted neighbors */
214
215 static __inline int
216 tnbr_compare(const struct tnbr *a, const struct tnbr *b)
217 {
218 if (a->af < b->af)
219 return (-1);
220 if (a->af > b->af)
221 return (1);
222
223 return (ldp_addrcmp(a->af, &a->addr, &b->addr));
224 }
225
226 struct tnbr *
227 tnbr_new(int af, union ldpd_addr *addr)
228 {
229 struct tnbr *tnbr;
230
231 if ((tnbr = calloc(1, sizeof(*tnbr))) == NULL)
232 fatal(__func__);
233
234 tnbr->af = af;
235 tnbr->addr = *addr;
236 tnbr->state = TNBR_STA_DOWN;
237
238 return (tnbr);
239 }
240
241 static void
242 tnbr_del(struct ldpd_conf *xconf, struct tnbr *tnbr)
243 {
244 tnbr_stop(tnbr);
245 RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr);
246 free(tnbr);
247 }
248
249 struct tnbr *
250 tnbr_find(struct ldpd_conf *xconf, int af, union ldpd_addr *addr)
251 {
252 struct tnbr tnbr;
253 tnbr.af = af;
254 tnbr.addr = *addr;
255 return (RB_FIND(tnbr_head, &xconf->tnbr_tree, &tnbr));
256 }
257
258 struct tnbr *
259 tnbr_check(struct ldpd_conf *xconf, struct tnbr *tnbr)
260 {
261 if (!(tnbr->flags & (F_TNBR_CONFIGURED|F_TNBR_DYNAMIC)) &&
262 tnbr->pw_count == 0) {
263 tnbr_del(xconf, tnbr);
264 return (NULL);
265 }
266
267 return (tnbr);
268 }
269
270 static void
271 tnbr_start(struct tnbr *tnbr)
272 {
273 send_hello(HELLO_TARGETED, NULL, tnbr);
274 tnbr_start_hello_timer(tnbr);
275 tnbr->state = TNBR_STA_ACTIVE;
276 }
277
278 static void
279 tnbr_stop(struct tnbr *tnbr)
280 {
281 tnbr_stop_hello_timer(tnbr);
282 if (tnbr->adj)
283 adj_del(tnbr->adj, S_SHUTDOWN);
284 tnbr->state = TNBR_STA_DOWN;
285 }
286
287 void
288 tnbr_update(struct tnbr *tnbr)
289 {
290 int socket_ok, rtr_id_ok;
291
292 if ((ldp_af_global_get(&global, tnbr->af))->ldp_edisc_socket != -1)
293 socket_ok = 1;
294 else
295 socket_ok = 0;
296
297 if (ldp_rtr_id_get(leconf) != INADDR_ANY)
298 rtr_id_ok = 1;
299 else
300 rtr_id_ok = 0;
301
302 if (tnbr->state == TNBR_STA_DOWN) {
303 if (!socket_ok || !rtr_id_ok)
304 return;
305
306 tnbr_start(tnbr);
307 } else if (tnbr->state == TNBR_STA_ACTIVE) {
308 if (socket_ok && rtr_id_ok)
309 return;
310
311 tnbr_stop(tnbr);
312 }
313 }
314
315 void
316 tnbr_update_all(int af)
317 {
318 struct tnbr *tnbr;
319
320 /* update targeted neighbors */
321 RB_FOREACH(tnbr, tnbr_head, &leconf->tnbr_tree)
322 if (tnbr->af == af || af == AF_UNSPEC)
323 tnbr_update(tnbr);
324 }
325
326 uint16_t
327 tnbr_get_hello_holdtime(struct tnbr *tnbr)
328 {
329 if ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime != 0)
330 return ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime);
331
332 return (leconf->thello_holdtime);
333 }
334
335 uint16_t
336 tnbr_get_hello_interval(struct tnbr *tnbr)
337 {
338 if ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval != 0)
339 return ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval);
340
341 return (leconf->thello_interval);
342 }
343
344 /* target neighbors timers */
345
346 /* ARGSUSED */
347 static int
348 tnbr_hello_timer(struct thread *thread)
349 {
350 struct tnbr *tnbr = THREAD_ARG(thread);
351
352 tnbr->hello_timer = NULL;
353 send_hello(HELLO_TARGETED, NULL, tnbr);
354 tnbr_start_hello_timer(tnbr);
355
356 return (0);
357 }
358
359 static void
360 tnbr_start_hello_timer(struct tnbr *tnbr)
361 {
362 thread_cancel(&tnbr->hello_timer);
363 tnbr->hello_timer = NULL;
364 thread_add_timer(master, tnbr_hello_timer, tnbr, tnbr_get_hello_interval(tnbr),
365 &tnbr->hello_timer);
366 }
367
368 static void
369 tnbr_stop_hello_timer(struct tnbr *tnbr)
370 {
371 thread_cancel(&tnbr->hello_timer);
372 }
373
374 struct ctl_adj *
375 adj_to_ctl(struct adj *adj)
376 {
377 static struct ctl_adj actl;
378
379 actl.af = adj_get_af(adj);
380 actl.id = adj->lsr_id;
381 actl.type = adj->source.type;
382 switch (adj->source.type) {
383 case HELLO_LINK:
384 memcpy(actl.ifname, adj->source.link.ia->iface->name,
385 sizeof(actl.ifname));
386 actl.src_addr = adj->source.link.src_addr;
387 break;
388 case HELLO_TARGETED:
389 actl.src_addr = adj->source.target->addr;
390 break;
391 }
392 actl.holdtime = adj->holdtime;
393 actl.holdtime_remaining =
394 thread_timer_remain_second(adj->inactivity_timer);
395 actl.trans_addr = adj->trans_addr;
396 actl.ds_tlv = adj->ds_tlv;
397
398 return (&actl);
399 }