]>
Commit | Line | Data |
---|---|---|
8429abe0 RW |
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 | ||
eac6e3f0 | 22 | #include <zebra.h> |
8429abe0 RW |
23 | |
24 | #include "ldpd.h" | |
25 | #include "ldpe.h" | |
26 | #include "log.h" | |
27 | ||
45926e58 | 28 | static __inline int adj_compare(const struct adj *, const struct adj *); |
eac6e3f0 | 29 | static int adj_itimer(struct thread *); |
45926e58 | 30 | static __inline int tnbr_compare(const struct tnbr *, const struct tnbr *); |
7989cdba | 31 | static void tnbr_del(struct ldpd_conf *, struct tnbr *); |
8a26d0cf RW |
32 | static void tnbr_start(struct tnbr *); |
33 | static void tnbr_stop(struct tnbr *); | |
eac6e3f0 | 34 | static int tnbr_hello_timer(struct thread *); |
8429abe0 RW |
35 | static void tnbr_start_hello_timer(struct tnbr *); |
36 | static void tnbr_stop_hello_timer(struct tnbr *); | |
37 | ||
057d48bd RW |
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) | |
7989cdba RW |
41 | RB_GENERATE(tnbr_head, tnbr, entry, tnbr_compare) |
42 | ||
057d48bd | 43 | static __inline int |
45926e58 | 44 | adj_compare(const struct adj *a, const struct adj *b) |
057d48bd | 45 | { |
f2725627 RW |
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 | ||
057d48bd RW |
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: | |
36de6e0e A |
63 | if (if_cmp_name_func(a->source.link.ia->iface->name, |
64 | b->source.link.ia->iface->name) < 0) | |
057d48bd | 65 | return (-1); |
36de6e0e A |
66 | if (if_cmp_name_func(a->source.link.ia->iface->name, |
67 | b->source.link.ia->iface->name) > 0) | |
057d48bd | 68 | return (1); |
057d48bd RW |
69 | return (ldp_addrcmp(a->source.link.ia->af, |
70 | &a->source.link.src_addr, &b->source.link.src_addr)); | |
71 | case HELLO_TARGETED: | |
057d48bd RW |
72 | return (ldp_addrcmp(a->source.target->af, |
73 | &a->source.target->addr, &b->source.target->addr)); | |
74 | default: | |
f2725627 | 75 | fatalx("adj_compare: unknown hello type"); |
057d48bd RW |
76 | } |
77 | ||
78 | return (0); | |
79 | } | |
80 | ||
8429abe0 RW |
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 | ||
903a7226 | 87 | log_debug("%s: lsr-id %pI4, %s", __func__, &lsr_id, |
8429abe0 RW |
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 | ||
057d48bd | 98 | RB_INSERT(global_adj_head, &global.adj_tree, adj); |
8429abe0 RW |
99 | |
100 | switch (source->type) { | |
101 | case HELLO_LINK: | |
057d48bd | 102 | RB_INSERT(ia_adj_head, &source->link.ia->adj_tree, adj); |
8429abe0 RW |
103 | break; |
104 | case HELLO_TARGETED: | |
105 | source->target->adj = adj; | |
106 | break; | |
107 | } | |
108 | ||
109 | return (adj); | |
110 | } | |
111 | ||
0e3451e5 RW |
112 | void |
113 | adj_del(struct adj *adj, uint32_t notif_status) | |
8429abe0 | 114 | { |
0e3451e5 RW |
115 | struct nbr *nbr = adj->nbr; |
116 | ||
903a7226 | 117 | log_debug("%s: lsr-id %pI4, %s (%s)", __func__, &adj->lsr_id, |
8429abe0 RW |
118 | log_hello_src(&adj->source), af_name(adj_get_af(adj))); |
119 | ||
120 | adj_stop_itimer(adj); | |
121 | ||
057d48bd | 122 | RB_REMOVE(global_adj_head, &global.adj_tree, adj); |
0e3451e5 RW |
123 | if (nbr) |
124 | RB_REMOVE(nbr_adj_head, &nbr->adj_tree, adj); | |
8429abe0 RW |
125 | switch (adj->source.type) { |
126 | case HELLO_LINK: | |
057d48bd | 127 | RB_REMOVE(ia_adj_head, &adj->source.link.ia->adj_tree, adj); |
e1894ff7 KS |
128 | |
129 | if (nbr) | |
130 | ldp_sync_fsm_adj_event(adj, LDP_SYNC_EVT_ADJ_DEL); | |
8429abe0 RW |
131 | break; |
132 | case HELLO_TARGETED: | |
133 | adj->source.target->adj = NULL; | |
134 | break; | |
135 | } | |
136 | ||
137 | free(adj); | |
8429abe0 RW |
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) { | |
8429abe0 RW |
145 | session_shutdown(nbr, notif_status, 0, 0); |
146 | nbr_del(nbr); | |
147 | } | |
148 | } | |
149 | ||
150 | struct adj * | |
f2725627 | 151 | adj_find(struct in_addr lsr_id, struct hello_source *source) |
8429abe0 | 152 | { |
057d48bd | 153 | struct adj adj; |
f2725627 | 154 | adj.lsr_id = lsr_id; |
057d48bd RW |
155 | adj.source = *source; |
156 | return (RB_FIND(global_adj_head, &global.adj_tree, &adj)); | |
8429abe0 RW |
157 | } |
158 | ||
159 | int | |
522faa1f | 160 | adj_get_af(const struct adj *adj) |
8429abe0 RW |
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 */ | |
eac6e3f0 RW |
175 | static int |
176 | adj_itimer(struct thread *thread) | |
8429abe0 | 177 | { |
eac6e3f0 RW |
178 | struct adj *adj = THREAD_ARG(thread); |
179 | ||
180 | adj->inactivity_timer = NULL; | |
8429abe0 | 181 | |
903a7226 | 182 | log_debug("%s: lsr-id %pI4", __func__, &adj->lsr_id); |
8429abe0 RW |
183 | |
184 | if (adj->source.type == HELLO_TARGETED) { | |
185 | if (!(adj->source.target->flags & F_TNBR_CONFIGURED) && | |
077d336a RW |
186 | adj->source.target->pw_count == 0 && |
187 | adj->source.target->rlfa_count == 0) { | |
8429abe0 | 188 | /* remove dynamic targeted neighbor */ |
7989cdba | 189 | tnbr_del(leconf, adj->source.target); |
eac6e3f0 | 190 | return (0); |
8429abe0 | 191 | } |
8429abe0 RW |
192 | } |
193 | ||
194 | adj_del(adj, S_HOLDTIME_EXP); | |
eac6e3f0 RW |
195 | |
196 | return (0); | |
8429abe0 RW |
197 | } |
198 | ||
199 | void | |
200 | adj_start_itimer(struct adj *adj) | |
201 | { | |
50478845 | 202 | thread_cancel(&adj->inactivity_timer); |
66e78ae6 QY |
203 | adj->inactivity_timer = NULL; |
204 | thread_add_timer(master, adj_itimer, adj, adj->holdtime, | |
205 | &adj->inactivity_timer); | |
8429abe0 RW |
206 | } |
207 | ||
208 | void | |
209 | adj_stop_itimer(struct adj *adj) | |
210 | { | |
50478845 | 211 | thread_cancel(&adj->inactivity_timer); |
8429abe0 RW |
212 | } |
213 | ||
214 | /* targeted neighbors */ | |
215 | ||
7989cdba | 216 | static __inline int |
45926e58 | 217 | tnbr_compare(const struct tnbr *a, const struct tnbr *b) |
7989cdba RW |
218 | { |
219 | if (a->af < b->af) | |
220 | return (-1); | |
221 | if (a->af > b->af) | |
222 | return (1); | |
223 | ||
224 | return (ldp_addrcmp(a->af, &a->addr, &b->addr)); | |
225 | } | |
226 | ||
8429abe0 | 227 | struct tnbr * |
eac6e3f0 | 228 | tnbr_new(int af, union ldpd_addr *addr) |
8429abe0 RW |
229 | { |
230 | struct tnbr *tnbr; | |
231 | ||
232 | if ((tnbr = calloc(1, sizeof(*tnbr))) == NULL) | |
233 | fatal(__func__); | |
234 | ||
235 | tnbr->af = af; | |
236 | tnbr->addr = *addr; | |
237 | tnbr->state = TNBR_STA_DOWN; | |
8429abe0 RW |
238 | |
239 | return (tnbr); | |
240 | } | |
241 | ||
242 | static void | |
7989cdba | 243 | tnbr_del(struct ldpd_conf *xconf, struct tnbr *tnbr) |
8429abe0 | 244 | { |
8a26d0cf | 245 | tnbr_stop(tnbr); |
7989cdba | 246 | RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr); |
8429abe0 RW |
247 | free(tnbr); |
248 | } | |
249 | ||
250 | struct tnbr * | |
251 | tnbr_find(struct ldpd_conf *xconf, int af, union ldpd_addr *addr) | |
252 | { | |
7989cdba RW |
253 | struct tnbr tnbr; |
254 | tnbr.af = af; | |
255 | tnbr.addr = *addr; | |
256 | return (RB_FIND(tnbr_head, &xconf->tnbr_tree, &tnbr)); | |
8429abe0 RW |
257 | } |
258 | ||
259 | struct tnbr * | |
7989cdba | 260 | tnbr_check(struct ldpd_conf *xconf, struct tnbr *tnbr) |
8429abe0 RW |
261 | { |
262 | if (!(tnbr->flags & (F_TNBR_CONFIGURED|F_TNBR_DYNAMIC)) && | |
077d336a | 263 | tnbr->pw_count == 0 && tnbr->rlfa_count == 0) { |
7989cdba | 264 | tnbr_del(xconf, tnbr); |
8429abe0 RW |
265 | return (NULL); |
266 | } | |
267 | ||
268 | return (tnbr); | |
269 | } | |
270 | ||
8a26d0cf RW |
271 | static void |
272 | tnbr_start(struct tnbr *tnbr) | |
273 | { | |
274 | send_hello(HELLO_TARGETED, NULL, tnbr); | |
275 | tnbr_start_hello_timer(tnbr); | |
276 | tnbr->state = TNBR_STA_ACTIVE; | |
277 | } | |
278 | ||
279 | static void | |
280 | tnbr_stop(struct tnbr *tnbr) | |
281 | { | |
282 | tnbr_stop_hello_timer(tnbr); | |
283 | if (tnbr->adj) | |
284 | adj_del(tnbr->adj, S_SHUTDOWN); | |
285 | tnbr->state = TNBR_STA_DOWN; | |
286 | } | |
287 | ||
8429abe0 RW |
288 | void |
289 | tnbr_update(struct tnbr *tnbr) | |
290 | { | |
291 | int socket_ok, rtr_id_ok; | |
292 | ||
293 | if ((ldp_af_global_get(&global, tnbr->af))->ldp_edisc_socket != -1) | |
294 | socket_ok = 1; | |
295 | else | |
296 | socket_ok = 0; | |
297 | ||
eac6e3f0 | 298 | if (ldp_rtr_id_get(leconf) != INADDR_ANY) |
8429abe0 RW |
299 | rtr_id_ok = 1; |
300 | else | |
301 | rtr_id_ok = 0; | |
302 | ||
303 | if (tnbr->state == TNBR_STA_DOWN) { | |
304 | if (!socket_ok || !rtr_id_ok) | |
305 | return; | |
306 | ||
8a26d0cf | 307 | tnbr_start(tnbr); |
8429abe0 RW |
308 | } else if (tnbr->state == TNBR_STA_ACTIVE) { |
309 | if (socket_ok && rtr_id_ok) | |
310 | return; | |
311 | ||
8a26d0cf | 312 | tnbr_stop(tnbr); |
8429abe0 RW |
313 | } |
314 | } | |
315 | ||
316 | void | |
317 | tnbr_update_all(int af) | |
318 | { | |
319 | struct tnbr *tnbr; | |
320 | ||
321 | /* update targeted neighbors */ | |
7989cdba | 322 | RB_FOREACH(tnbr, tnbr_head, &leconf->tnbr_tree) |
8429abe0 RW |
323 | if (tnbr->af == af || af == AF_UNSPEC) |
324 | tnbr_update(tnbr); | |
325 | } | |
326 | ||
eac6e3f0 RW |
327 | uint16_t |
328 | tnbr_get_hello_holdtime(struct tnbr *tnbr) | |
329 | { | |
330 | if ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime != 0) | |
331 | return ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime); | |
332 | ||
333 | return (leconf->thello_holdtime); | |
334 | } | |
335 | ||
336 | uint16_t | |
337 | tnbr_get_hello_interval(struct tnbr *tnbr) | |
338 | { | |
339 | if ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval != 0) | |
340 | return ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval); | |
341 | ||
342 | return (leconf->thello_interval); | |
343 | } | |
344 | ||
8429abe0 RW |
345 | /* target neighbors timers */ |
346 | ||
347 | /* ARGSUSED */ | |
eac6e3f0 RW |
348 | static int |
349 | tnbr_hello_timer(struct thread *thread) | |
8429abe0 | 350 | { |
eac6e3f0 | 351 | struct tnbr *tnbr = THREAD_ARG(thread); |
8429abe0 | 352 | |
eac6e3f0 | 353 | tnbr->hello_timer = NULL; |
8429abe0 RW |
354 | send_hello(HELLO_TARGETED, NULL, tnbr); |
355 | tnbr_start_hello_timer(tnbr); | |
eac6e3f0 RW |
356 | |
357 | return (0); | |
8429abe0 RW |
358 | } |
359 | ||
360 | static void | |
361 | tnbr_start_hello_timer(struct tnbr *tnbr) | |
362 | { | |
50478845 | 363 | thread_cancel(&tnbr->hello_timer); |
66e78ae6 QY |
364 | tnbr->hello_timer = NULL; |
365 | thread_add_timer(master, tnbr_hello_timer, tnbr, tnbr_get_hello_interval(tnbr), | |
366 | &tnbr->hello_timer); | |
8429abe0 RW |
367 | } |
368 | ||
369 | static void | |
370 | tnbr_stop_hello_timer(struct tnbr *tnbr) | |
371 | { | |
50478845 | 372 | thread_cancel(&tnbr->hello_timer); |
8429abe0 RW |
373 | } |
374 | ||
375 | struct ctl_adj * | |
376 | adj_to_ctl(struct adj *adj) | |
377 | { | |
378 | static struct ctl_adj actl; | |
379 | ||
380 | actl.af = adj_get_af(adj); | |
381 | actl.id = adj->lsr_id; | |
382 | actl.type = adj->source.type; | |
383 | switch (adj->source.type) { | |
384 | case HELLO_LINK: | |
385 | memcpy(actl.ifname, adj->source.link.ia->iface->name, | |
386 | sizeof(actl.ifname)); | |
0f7b5df9 | 387 | actl.src_addr = adj->source.link.src_addr; |
8429abe0 RW |
388 | break; |
389 | case HELLO_TARGETED: | |
390 | actl.src_addr = adj->source.target->addr; | |
391 | break; | |
392 | } | |
393 | actl.holdtime = adj->holdtime; | |
0f7b5df9 RW |
394 | actl.holdtime_remaining = |
395 | thread_timer_remain_second(adj->inactivity_timer); | |
8429abe0 | 396 | actl.trans_addr = adj->trans_addr; |
0f7b5df9 | 397 | actl.ds_tlv = adj->ds_tlv; |
8429abe0 RW |
398 | |
399 | return (&actl); | |
400 | } |