]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_adjacency.c
zebra: Fix label manager memory leak (#5680)
[mirror_frr.git] / isisd / isis_adjacency.c
1 /*
2 * IS-IS Rout(e)ing protocol - isis_adjacency.c
3 * handling of IS-IS adjacencies
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <zebra.h>
25
26 #include "log.h"
27 #include "memory.h"
28 #include "hash.h"
29 #include "vty.h"
30 #include "linklist.h"
31 #include "thread.h"
32 #include "if.h"
33 #include "stream.h"
34
35 #include "isisd/isis_constants.h"
36 #include "isisd/isis_common.h"
37 #include "isisd/isis_flags.h"
38 #include "isisd/isisd.h"
39 #include "isisd/isis_circuit.h"
40 #include "isisd/isis_adjacency.h"
41 #include "isisd/isis_misc.h"
42 #include "isisd/isis_dr.h"
43 #include "isisd/isis_dynhn.h"
44 #include "isisd/isis_pdu.h"
45 #include "isisd/isis_lsp.h"
46 #include "isisd/isis_spf.h"
47 #include "isisd/isis_events.h"
48 #include "isisd/isis_mt.h"
49 #include "isisd/isis_tlvs.h"
50 #include "isisd/fabricd.h"
51 #include "isisd/isis_nb.h"
52
53 extern struct isis *isis;
54
55 static struct isis_adjacency *adj_alloc(const uint8_t *id)
56 {
57 struct isis_adjacency *adj;
58
59 adj = XCALLOC(MTYPE_ISIS_ADJACENCY, sizeof(struct isis_adjacency));
60 memcpy(adj->sysid, id, ISIS_SYS_ID_LEN);
61
62 return adj;
63 }
64
65 struct isis_adjacency *isis_new_adj(const uint8_t *id, const uint8_t *snpa,
66 int level, struct isis_circuit *circuit)
67 {
68 struct isis_adjacency *adj;
69 int i;
70
71 adj = adj_alloc(id); /* P2P kludge */
72
73 if (snpa) {
74 memcpy(adj->snpa, snpa, ETH_ALEN);
75 } else {
76 memset(adj->snpa, ' ', ETH_ALEN);
77 }
78
79 adj->circuit = circuit;
80 adj->level = level;
81 adj->flaps = 0;
82 adj->last_flap = time(NULL);
83 adj->threeway_state = ISIS_THREEWAY_DOWN;
84 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
85 listnode_add(circuit->u.bc.adjdb[level - 1], adj);
86 adj->dischanges[level - 1] = 0;
87 for (i = 0; i < DIS_RECORDS;
88 i++) /* clear N DIS state change records */
89 {
90 adj->dis_record[(i * ISIS_LEVELS) + level - 1].dis =
91 ISIS_UNKNOWN_DIS;
92 adj->dis_record[(i * ISIS_LEVELS) + level - 1]
93 .last_dis_change = time(NULL);
94 }
95 }
96
97 return adj;
98 }
99
100 struct isis_adjacency *isis_adj_lookup(const uint8_t *sysid, struct list *adjdb)
101 {
102 struct isis_adjacency *adj;
103 struct listnode *node;
104
105 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
106 if (memcmp(adj->sysid, sysid, ISIS_SYS_ID_LEN) == 0)
107 return adj;
108
109 return NULL;
110 }
111
112 struct isis_adjacency *isis_adj_lookup_snpa(const uint8_t *ssnpa,
113 struct list *adjdb)
114 {
115 struct listnode *node;
116 struct isis_adjacency *adj;
117
118 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
119 if (memcmp(adj->snpa, ssnpa, ETH_ALEN) == 0)
120 return adj;
121
122 return NULL;
123 }
124
125 DEFINE_HOOK(isis_adj_state_change_hook, (struct isis_adjacency *adj), (adj))
126
127 void isis_delete_adj(void *arg)
128 {
129 struct isis_adjacency *adj = arg;
130
131 if (!adj)
132 return;
133
134 THREAD_TIMER_OFF(adj->t_expire);
135 if (adj->adj_state != ISIS_ADJ_DOWN) {
136 adj->adj_state = ISIS_ADJ_DOWN;
137 hook_call(isis_adj_state_change_hook, adj);
138 }
139
140 /* remove from SPF trees */
141 spftree_area_adj_del(adj->circuit->area, adj);
142
143 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->area_addresses);
144 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->ipv4_addresses);
145 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->ipv6_addresses);
146
147 adj_mt_finish(adj);
148
149 XFREE(MTYPE_ISIS_ADJACENCY, adj);
150 return;
151 }
152
153 static const char *adj_state2string(int state)
154 {
155
156 switch (state) {
157 case ISIS_ADJ_INITIALIZING:
158 return "Initializing";
159 case ISIS_ADJ_UP:
160 return "Up";
161 case ISIS_ADJ_DOWN:
162 return "Down";
163 default:
164 return "Unknown";
165 }
166
167 return NULL; /* not reached */
168 }
169
170 void isis_adj_process_threeway(struct isis_adjacency *adj,
171 struct isis_threeway_adj *tw_adj,
172 enum isis_adj_usage adj_usage)
173 {
174 enum isis_threeway_state next_tw_state = ISIS_THREEWAY_DOWN;
175
176 if (tw_adj && !adj->circuit->disable_threeway_adj) {
177 if (tw_adj->state == ISIS_THREEWAY_DOWN) {
178 next_tw_state = ISIS_THREEWAY_INITIALIZING;
179 } else if (tw_adj->state == ISIS_THREEWAY_INITIALIZING) {
180 next_tw_state = ISIS_THREEWAY_UP;
181 } else if (tw_adj->state == ISIS_THREEWAY_UP) {
182 if (adj->threeway_state == ISIS_THREEWAY_DOWN)
183 next_tw_state = ISIS_THREEWAY_DOWN;
184 else
185 next_tw_state = ISIS_THREEWAY_UP;
186 }
187 } else {
188 next_tw_state = ISIS_THREEWAY_UP;
189 }
190
191 if (next_tw_state != adj->threeway_state) {
192 if (isis->debugs & DEBUG_ADJ_PACKETS) {
193 zlog_info("ISIS-Adj (%s): Threeway state change %s to %s",
194 adj->circuit->area->area_tag,
195 isis_threeway_state_name(adj->threeway_state),
196 isis_threeway_state_name(next_tw_state));
197 }
198 }
199
200 if (next_tw_state != ISIS_THREEWAY_DOWN)
201 fabricd_initial_sync_hello(adj->circuit);
202
203 if (next_tw_state == ISIS_THREEWAY_DOWN) {
204 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "Neighbor restarted");
205 return;
206 }
207
208 if (next_tw_state == ISIS_THREEWAY_UP) {
209 if (adj->adj_state != ISIS_ADJ_UP) {
210 isis_adj_state_change(adj, ISIS_ADJ_UP, NULL);
211 adj->adj_usage = adj_usage;
212 }
213 }
214
215 if (adj->threeway_state != next_tw_state) {
216 send_hello_sched(adj->circuit, 0, TRIGGERED_IIH_DELAY);
217 }
218
219 adj->threeway_state = next_tw_state;
220 }
221
222 void isis_adj_state_change(struct isis_adjacency *adj,
223 enum isis_adj_state new_state, const char *reason)
224 {
225 enum isis_adj_state old_state = adj->adj_state;
226 struct isis_circuit *circuit = adj->circuit;
227 bool del;
228
229 adj->adj_state = new_state;
230 if (new_state != old_state) {
231 send_hello_sched(circuit, adj->level, TRIGGERED_IIH_DELAY);
232 }
233
234 if (isis->debugs & DEBUG_ADJ_PACKETS) {
235 zlog_debug("ISIS-Adj (%s): Adjacency state change %d->%d: %s",
236 circuit->area->area_tag, old_state, new_state,
237 reason ? reason : "unspecified");
238 }
239
240 if (circuit->area->log_adj_changes) {
241 const char *adj_name;
242 struct isis_dynhn *dyn;
243
244 dyn = dynhn_find_by_id(adj->sysid);
245 if (dyn)
246 adj_name = dyn->hostname;
247 else
248 adj_name = sysid_print(adj->sysid);
249
250 zlog_info(
251 "%%ADJCHANGE: Adjacency to %s (%s) changed from %s to %s, %s",
252 adj_name, adj->circuit->interface->name,
253 adj_state2string(old_state),
254 adj_state2string(new_state),
255 reason ? reason : "unspecified");
256 }
257
258 circuit->adj_state_changes++;
259 #ifndef FABRICD
260 /* send northbound notification */
261 isis_notif_adj_state_change(adj, new_state, reason);
262 #endif /* ifndef FABRICD */
263
264 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
265 del = false;
266 for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
267 if ((adj->level & level) == 0)
268 continue;
269 if (new_state == ISIS_ADJ_UP) {
270 circuit->upadjcount[level - 1]++;
271 hook_call(isis_adj_state_change_hook, adj);
272 /* update counter & timers for debugging
273 * purposes */
274 adj->last_flap = time(NULL);
275 adj->flaps++;
276 } else if (new_state == ISIS_ADJ_DOWN) {
277 listnode_delete(circuit->u.bc.adjdb[level - 1],
278 adj);
279
280 circuit->upadjcount[level - 1]--;
281 if (circuit->upadjcount[level - 1] == 0)
282 isis_tx_queue_clean(circuit->tx_queue);
283
284 hook_call(isis_adj_state_change_hook, adj);
285 del = true;
286 }
287
288 if (circuit->u.bc.lan_neighs[level - 1]) {
289 list_delete_all_node(
290 circuit->u.bc.lan_neighs[level - 1]);
291 isis_adj_build_neigh_list(
292 circuit->u.bc.adjdb[level - 1],
293 circuit->u.bc.lan_neighs[level - 1]);
294 }
295
296 /* On adjacency state change send new pseudo LSP if we
297 * are the DR */
298 if (circuit->u.bc.is_dr[level - 1])
299 lsp_regenerate_schedule_pseudo(circuit, level);
300 }
301
302 if (del)
303 isis_delete_adj(adj);
304
305 } else if (circuit->circ_type == CIRCUIT_T_P2P) {
306 del = false;
307 for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
308 if ((adj->level & level) == 0)
309 continue;
310 if (new_state == ISIS_ADJ_UP) {
311 circuit->upadjcount[level - 1]++;
312 hook_call(isis_adj_state_change_hook, adj);
313
314 /* update counter & timers for debugging
315 * purposes */
316 adj->last_flap = time(NULL);
317 adj->flaps++;
318
319 if (level == IS_LEVEL_1) {
320 thread_add_timer(master, send_l1_csnp,
321 circuit, 0,
322 &circuit->t_send_csnp[0]);
323 } else {
324 thread_add_timer(master, send_l2_csnp,
325 circuit, 0,
326 &circuit->t_send_csnp[1]);
327 }
328 } else if (new_state == ISIS_ADJ_DOWN) {
329 if (adj->circuit->u.p2p.neighbor == adj)
330 adj->circuit->u.p2p.neighbor = NULL;
331 circuit->upadjcount[level - 1]--;
332 if (circuit->upadjcount[level - 1] == 0)
333 isis_tx_queue_clean(circuit->tx_queue);
334
335 hook_call(isis_adj_state_change_hook, adj);
336 del = true;
337 }
338 }
339
340 if (del)
341 isis_delete_adj(adj);
342 }
343 }
344
345
346 void isis_adj_print(struct isis_adjacency *adj)
347 {
348 struct isis_dynhn *dyn;
349
350 if (!adj)
351 return;
352 dyn = dynhn_find_by_id(adj->sysid);
353 if (dyn)
354 zlog_debug("%s", dyn->hostname);
355
356 zlog_debug("SystemId %20s SNPA %s, level %d\nHolding Time %d",
357 sysid_print(adj->sysid), snpa_print(adj->snpa), adj->level,
358 adj->hold_time);
359 if (adj->ipv4_address_count) {
360 zlog_debug("IPv4 Address(es):");
361 for (unsigned int i = 0; i < adj->ipv4_address_count; i++)
362 zlog_debug("%s", inet_ntoa(adj->ipv4_addresses[i]));
363 }
364
365 if (adj->ipv6_address_count) {
366 zlog_debug("IPv6 Address(es):");
367 for (unsigned int i = 0; i < adj->ipv6_address_count; i++) {
368 char buf[INET6_ADDRSTRLEN];
369 inet_ntop(AF_INET6, &adj->ipv6_addresses[i], buf,
370 sizeof(buf));
371 zlog_debug("%s", buf);
372 }
373 }
374 zlog_debug("Speaks: %s", nlpid2string(&adj->nlpids));
375
376 return;
377 }
378
379 const char *isis_adj_yang_state(enum isis_adj_state state)
380 {
381 switch (state) {
382 case ISIS_ADJ_DOWN:
383 return "down";
384 case ISIS_ADJ_UP:
385 return "up";
386 case ISIS_ADJ_INITIALIZING:
387 return "init";
388 default:
389 return "failed";
390 }
391 }
392
393 int isis_adj_expire(struct thread *thread)
394 {
395 struct isis_adjacency *adj;
396
397 /*
398 * Get the adjacency
399 */
400 adj = THREAD_ARG(thread);
401 assert(adj);
402 adj->t_expire = NULL;
403
404 /* trigger the adj expire event */
405 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "holding time expired");
406
407 return 0;
408 }
409
410 /*
411 * show isis neighbor [detail]
412 */
413 void isis_adj_print_vty(struct isis_adjacency *adj, struct vty *vty,
414 char detail)
415 {
416 time_t now;
417 struct isis_dynhn *dyn;
418 int level;
419
420 dyn = dynhn_find_by_id(adj->sysid);
421 if (dyn)
422 vty_out(vty, " %-20s", dyn->hostname);
423 else
424 vty_out(vty, " %-20s", sysid_print(adj->sysid));
425
426 if (detail == ISIS_UI_LEVEL_BRIEF) {
427 if (adj->circuit)
428 vty_out(vty, "%-12s", adj->circuit->interface->name);
429 else
430 vty_out(vty, "NULL circuit!");
431 vty_out(vty, "%-3u", adj->level); /* level */
432 vty_out(vty, "%-13s", adj_state2string(adj->adj_state));
433 now = time(NULL);
434 if (adj->last_upd)
435 vty_out(vty, "%-9llu",
436 (unsigned long long)adj->last_upd
437 + adj->hold_time - now);
438 else
439 vty_out(vty, "- ");
440 vty_out(vty, "%-10s", snpa_print(adj->snpa));
441 vty_out(vty, "\n");
442 }
443
444 if (detail == ISIS_UI_LEVEL_DETAIL) {
445 level = adj->level;
446 vty_out(vty, "\n");
447 if (adj->circuit)
448 vty_out(vty, " Interface: %s",
449 adj->circuit->interface->name);
450 else
451 vty_out(vty, " Interface: NULL circuit");
452 vty_out(vty, ", Level: %u", adj->level); /* level */
453 vty_out(vty, ", State: %s", adj_state2string(adj->adj_state));
454 now = time(NULL);
455 if (adj->last_upd)
456 vty_out(vty, ", Expires in %s",
457 time2string(adj->last_upd + adj->hold_time
458 - now));
459 else
460 vty_out(vty, ", Expires in %s",
461 time2string(adj->hold_time));
462 vty_out(vty, "\n");
463 vty_out(vty, " Adjacency flaps: %u", adj->flaps);
464 vty_out(vty, ", Last: %s ago",
465 time2string(now - adj->last_flap));
466 vty_out(vty, "\n");
467 vty_out(vty, " Circuit type: %s",
468 circuit_t2string(adj->circuit_t));
469 vty_out(vty, ", Speaks: %s", nlpid2string(&adj->nlpids));
470 vty_out(vty, "\n");
471 if (adj->mt_count != 1
472 || adj->mt_set[0] != ISIS_MT_IPV4_UNICAST) {
473 vty_out(vty, " Topologies:\n");
474 for (unsigned int i = 0; i < adj->mt_count; i++)
475 vty_out(vty, " %s\n",
476 isis_mtid2str(adj->mt_set[i]));
477 }
478 vty_out(vty, " SNPA: %s", snpa_print(adj->snpa));
479 if (adj->circuit
480 && (adj->circuit->circ_type == CIRCUIT_T_BROADCAST)) {
481 dyn = dynhn_find_by_id(adj->lanid);
482 if (dyn)
483 vty_out(vty, ", LAN id: %s.%02x", dyn->hostname,
484 adj->lanid[ISIS_SYS_ID_LEN]);
485 else
486 vty_out(vty, ", LAN id: %s.%02x",
487 sysid_print(adj->lanid),
488 adj->lanid[ISIS_SYS_ID_LEN]);
489
490 vty_out(vty, "\n");
491 vty_out(vty, " LAN Priority: %u",
492 adj->prio[adj->level - 1]);
493
494 vty_out(vty, ", %s, DIS flaps: %u, Last: %s ago",
495 isis_disflag2string(
496 adj->dis_record[ISIS_LEVELS + level - 1]
497 .dis),
498 adj->dischanges[level - 1],
499 time2string(now - (adj->dis_record[ISIS_LEVELS
500 + level - 1]
501 .last_dis_change)));
502 }
503 vty_out(vty, "\n");
504
505 if (adj->area_address_count) {
506 vty_out(vty, " Area Address(es):\n");
507 for (unsigned int i = 0; i < adj->area_address_count;
508 i++) {
509 vty_out(vty, " %s\n",
510 isonet_print(adj->area_addresses[i]
511 .area_addr,
512 adj->area_addresses[i]
513 .addr_len));
514 }
515 }
516 if (adj->ipv4_address_count) {
517 vty_out(vty, " IPv4 Address(es):\n");
518 for (unsigned int i = 0; i < adj->ipv4_address_count;
519 i++)
520 vty_out(vty, " %s\n",
521 inet_ntoa(adj->ipv4_addresses[i]));
522 }
523 if (adj->ipv6_address_count) {
524 vty_out(vty, " IPv6 Address(es):\n");
525 for (unsigned int i = 0; i < adj->ipv6_address_count;
526 i++) {
527 char buf[INET6_ADDRSTRLEN];
528 inet_ntop(AF_INET6, &adj->ipv6_addresses[i],
529 buf, sizeof(buf));
530 vty_out(vty, " %s\n", buf);
531 }
532 }
533 vty_out(vty, "\n");
534 }
535 return;
536 }
537
538 void isis_adj_build_neigh_list(struct list *adjdb, struct list *list)
539 {
540 struct isis_adjacency *adj;
541 struct listnode *node;
542
543 if (!list) {
544 zlog_warn("isis_adj_build_neigh_list(): NULL list");
545 return;
546 }
547
548 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
549 if (!adj) {
550 zlog_warn("isis_adj_build_neigh_list(): NULL adj");
551 return;
552 }
553
554 if ((adj->adj_state == ISIS_ADJ_UP
555 || adj->adj_state == ISIS_ADJ_INITIALIZING))
556 listnode_add(list, adj->snpa);
557 }
558 return;
559 }
560
561 void isis_adj_build_up_list(struct list *adjdb, struct list *list)
562 {
563 struct isis_adjacency *adj;
564 struct listnode *node;
565
566 if (adjdb == NULL) {
567 zlog_warn("isis_adj_build_up_list(): adjacency DB is empty");
568 return;
569 }
570
571 if (!list) {
572 zlog_warn("isis_adj_build_up_list(): NULL list");
573 return;
574 }
575
576 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
577 if (!adj) {
578 zlog_warn("isis_adj_build_up_list(): NULL adj");
579 return;
580 }
581
582 if (adj->adj_state == ISIS_ADJ_UP)
583 listnode_add(list, adj);
584 }
585
586 return;
587 }
588
589 int isis_adj_usage2levels(enum isis_adj_usage usage)
590 {
591 switch (usage) {
592 case ISIS_ADJ_LEVEL1:
593 return IS_LEVEL_1;
594 case ISIS_ADJ_LEVEL2:
595 return IS_LEVEL_2;
596 case ISIS_ADJ_LEVEL1AND2:
597 return IS_LEVEL_1 | IS_LEVEL_2;
598 default:
599 break;
600 }
601 return 0;
602 }