]>
Commit | Line | Data |
---|---|---|
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((char *)a->source.link.ia->iface->name, | |
64 | (char *)b->source.link.ia->iface->name) < 0) | |
65 | return (-1); | |
66 | if (if_cmp_name_func((char *)a->source.link.ia->iface->name, | |
67 | (char *)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(const 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 = NULL; | |
200 | thread_add_timer(master, adj_itimer, adj, adj->holdtime, | |
201 | &adj->inactivity_timer); | |
202 | } | |
203 | ||
204 | void | |
205 | adj_stop_itimer(struct adj *adj) | |
206 | { | |
207 | THREAD_TIMER_OFF(adj->inactivity_timer); | |
208 | } | |
209 | ||
210 | /* targeted neighbors */ | |
211 | ||
212 | static __inline int | |
213 | tnbr_compare(const struct tnbr *a, const struct tnbr *b) | |
214 | { | |
215 | if (a->af < b->af) | |
216 | return (-1); | |
217 | if (a->af > b->af) | |
218 | return (1); | |
219 | ||
220 | return (ldp_addrcmp(a->af, &a->addr, &b->addr)); | |
221 | } | |
222 | ||
223 | struct tnbr * | |
224 | tnbr_new(int af, union ldpd_addr *addr) | |
225 | { | |
226 | struct tnbr *tnbr; | |
227 | ||
228 | if ((tnbr = calloc(1, sizeof(*tnbr))) == NULL) | |
229 | fatal(__func__); | |
230 | ||
231 | tnbr->af = af; | |
232 | tnbr->addr = *addr; | |
233 | tnbr->state = TNBR_STA_DOWN; | |
234 | ||
235 | return (tnbr); | |
236 | } | |
237 | ||
238 | static void | |
239 | tnbr_del(struct ldpd_conf *xconf, struct tnbr *tnbr) | |
240 | { | |
241 | tnbr_stop(tnbr); | |
242 | RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr); | |
243 | free(tnbr); | |
244 | } | |
245 | ||
246 | struct tnbr * | |
247 | tnbr_find(struct ldpd_conf *xconf, int af, union ldpd_addr *addr) | |
248 | { | |
249 | struct tnbr tnbr; | |
250 | tnbr.af = af; | |
251 | tnbr.addr = *addr; | |
252 | return (RB_FIND(tnbr_head, &xconf->tnbr_tree, &tnbr)); | |
253 | } | |
254 | ||
255 | struct tnbr * | |
256 | tnbr_check(struct ldpd_conf *xconf, struct tnbr *tnbr) | |
257 | { | |
258 | if (!(tnbr->flags & (F_TNBR_CONFIGURED|F_TNBR_DYNAMIC)) && | |
259 | tnbr->pw_count == 0) { | |
260 | tnbr_del(xconf, tnbr); | |
261 | return (NULL); | |
262 | } | |
263 | ||
264 | return (tnbr); | |
265 | } | |
266 | ||
267 | static void | |
268 | tnbr_start(struct tnbr *tnbr) | |
269 | { | |
270 | send_hello(HELLO_TARGETED, NULL, tnbr); | |
271 | tnbr_start_hello_timer(tnbr); | |
272 | tnbr->state = TNBR_STA_ACTIVE; | |
273 | } | |
274 | ||
275 | static void | |
276 | tnbr_stop(struct tnbr *tnbr) | |
277 | { | |
278 | tnbr_stop_hello_timer(tnbr); | |
279 | if (tnbr->adj) | |
280 | adj_del(tnbr->adj, S_SHUTDOWN); | |
281 | tnbr->state = TNBR_STA_DOWN; | |
282 | } | |
283 | ||
284 | void | |
285 | tnbr_update(struct tnbr *tnbr) | |
286 | { | |
287 | int socket_ok, rtr_id_ok; | |
288 | ||
289 | if ((ldp_af_global_get(&global, tnbr->af))->ldp_edisc_socket != -1) | |
290 | socket_ok = 1; | |
291 | else | |
292 | socket_ok = 0; | |
293 | ||
294 | if (ldp_rtr_id_get(leconf) != INADDR_ANY) | |
295 | rtr_id_ok = 1; | |
296 | else | |
297 | rtr_id_ok = 0; | |
298 | ||
299 | if (tnbr->state == TNBR_STA_DOWN) { | |
300 | if (!socket_ok || !rtr_id_ok) | |
301 | return; | |
302 | ||
303 | tnbr_start(tnbr); | |
304 | } else if (tnbr->state == TNBR_STA_ACTIVE) { | |
305 | if (socket_ok && rtr_id_ok) | |
306 | return; | |
307 | ||
308 | tnbr_stop(tnbr); | |
309 | } | |
310 | } | |
311 | ||
312 | void | |
313 | tnbr_update_all(int af) | |
314 | { | |
315 | struct tnbr *tnbr; | |
316 | ||
317 | /* update targeted neighbors */ | |
318 | RB_FOREACH(tnbr, tnbr_head, &leconf->tnbr_tree) | |
319 | if (tnbr->af == af || af == AF_UNSPEC) | |
320 | tnbr_update(tnbr); | |
321 | } | |
322 | ||
323 | uint16_t | |
324 | tnbr_get_hello_holdtime(struct tnbr *tnbr) | |
325 | { | |
326 | if ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime != 0) | |
327 | return ((ldp_af_conf_get(leconf, tnbr->af))->thello_holdtime); | |
328 | ||
329 | return (leconf->thello_holdtime); | |
330 | } | |
331 | ||
332 | uint16_t | |
333 | tnbr_get_hello_interval(struct tnbr *tnbr) | |
334 | { | |
335 | if ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval != 0) | |
336 | return ((ldp_af_conf_get(leconf, tnbr->af))->thello_interval); | |
337 | ||
338 | return (leconf->thello_interval); | |
339 | } | |
340 | ||
341 | /* target neighbors timers */ | |
342 | ||
343 | /* ARGSUSED */ | |
344 | static int | |
345 | tnbr_hello_timer(struct thread *thread) | |
346 | { | |
347 | struct tnbr *tnbr = THREAD_ARG(thread); | |
348 | ||
349 | tnbr->hello_timer = NULL; | |
350 | send_hello(HELLO_TARGETED, NULL, tnbr); | |
351 | tnbr_start_hello_timer(tnbr); | |
352 | ||
353 | return (0); | |
354 | } | |
355 | ||
356 | static void | |
357 | tnbr_start_hello_timer(struct tnbr *tnbr) | |
358 | { | |
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), | |
362 | &tnbr->hello_timer); | |
363 | } | |
364 | ||
365 | static void | |
366 | tnbr_stop_hello_timer(struct tnbr *tnbr) | |
367 | { | |
368 | THREAD_TIMER_OFF(tnbr->hello_timer); | |
369 | } | |
370 | ||
371 | struct ctl_adj * | |
372 | adj_to_ctl(struct adj *adj) | |
373 | { | |
374 | static struct ctl_adj actl; | |
375 | ||
376 | actl.af = adj_get_af(adj); | |
377 | actl.id = adj->lsr_id; | |
378 | actl.type = adj->source.type; | |
379 | switch (adj->source.type) { | |
380 | case HELLO_LINK: | |
381 | memcpy(actl.ifname, adj->source.link.ia->iface->name, | |
382 | sizeof(actl.ifname)); | |
383 | actl.src_addr = adj->source.link.src_addr; | |
384 | break; | |
385 | case HELLO_TARGETED: | |
386 | actl.src_addr = adj->source.target->addr; | |
387 | break; | |
388 | } | |
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; | |
394 | ||
395 | return (&actl); | |
396 | } |