]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_adjacency.c
isisd: retrofit the 'max-lsp-lifetime' command
[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/dict.h"
36 #include "isisd/isis_constants.h"
37 #include "isisd/isis_common.h"
38 #include "isisd/isis_flags.h"
39 #include "isisd/isisd.h"
40 #include "isisd/isis_circuit.h"
41 #include "isisd/isis_adjacency.h"
42 #include "isisd/isis_misc.h"
43 #include "isisd/isis_dr.h"
44 #include "isisd/isis_dynhn.h"
45 #include "isisd/isis_pdu.h"
46 #include "isisd/isis_lsp.h"
47 #include "isisd/isis_spf.h"
48 #include "isisd/isis_events.h"
49 #include "isisd/isis_mt.h"
50 #include "isisd/isis_tlvs.h"
51 #include "isisd/fabricd.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 if (adj->area_addresses)
144 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->area_addresses);
145 if (adj->ipv4_addresses)
146 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->ipv4_addresses);
147 if (adj->ipv6_addresses)
148 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->ipv6_addresses);
149
150 adj_mt_finish(adj);
151
152 XFREE(MTYPE_ISIS_ADJACENCY, adj);
153 return;
154 }
155
156 static const char *adj_state2string(int state)
157 {
158
159 switch (state) {
160 case ISIS_ADJ_INITIALIZING:
161 return "Initializing";
162 case ISIS_ADJ_UP:
163 return "Up";
164 case ISIS_ADJ_DOWN:
165 return "Down";
166 default:
167 return "Unknown";
168 }
169
170 return NULL; /* not reached */
171 }
172
173 void isis_adj_process_threeway(struct isis_adjacency *adj,
174 struct isis_threeway_adj *tw_adj,
175 enum isis_adj_usage adj_usage)
176 {
177 enum isis_threeway_state next_tw_state = ISIS_THREEWAY_DOWN;
178
179 if (tw_adj && !adj->circuit->disable_threeway_adj) {
180 if (tw_adj->state == ISIS_THREEWAY_DOWN) {
181 next_tw_state = ISIS_THREEWAY_INITIALIZING;
182 } else if (tw_adj->state == ISIS_THREEWAY_INITIALIZING) {
183 next_tw_state = ISIS_THREEWAY_UP;
184 } else if (tw_adj->state == ISIS_THREEWAY_UP) {
185 if (adj->threeway_state == ISIS_THREEWAY_DOWN)
186 next_tw_state = ISIS_THREEWAY_DOWN;
187 else
188 next_tw_state = ISIS_THREEWAY_UP;
189 }
190 } else {
191 next_tw_state = ISIS_THREEWAY_UP;
192 }
193
194 if (next_tw_state != adj->threeway_state) {
195 if (isis->debugs & DEBUG_ADJ_PACKETS) {
196 zlog_info("ISIS-Adj (%s): Threeway state change %s to %s",
197 adj->circuit->area->area_tag,
198 isis_threeway_state_name(adj->threeway_state),
199 isis_threeway_state_name(next_tw_state));
200 }
201 }
202
203 if (next_tw_state != ISIS_THREEWAY_DOWN)
204 fabricd_initial_sync_hello(adj->circuit);
205
206 if (next_tw_state == ISIS_THREEWAY_DOWN) {
207 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "Neighbor restarted");
208 return;
209 }
210
211 if (next_tw_state == ISIS_THREEWAY_UP) {
212 if (adj->adj_state != ISIS_ADJ_UP) {
213 isis_adj_state_change(adj, ISIS_ADJ_UP, NULL);
214 adj->adj_usage = adj_usage;
215 }
216 }
217
218 if (adj->threeway_state != next_tw_state) {
219 send_hello_sched(adj->circuit, 0, TRIGGERED_IIH_DELAY);
220 }
221
222 adj->threeway_state = next_tw_state;
223 }
224
225 void isis_adj_state_change(struct isis_adjacency *adj,
226 enum isis_adj_state new_state, const char *reason)
227 {
228 enum isis_adj_state old_state = adj->adj_state;
229 struct isis_circuit *circuit = adj->circuit;
230 bool del;
231
232 adj->adj_state = new_state;
233 if (new_state != old_state) {
234 send_hello_sched(circuit, adj->level, TRIGGERED_IIH_DELAY);
235 }
236
237 if (isis->debugs & DEBUG_ADJ_PACKETS) {
238 zlog_debug("ISIS-Adj (%s): Adjacency state change %d->%d: %s",
239 circuit->area->area_tag, old_state, new_state,
240 reason ? reason : "unspecified");
241 }
242
243 if (circuit->area->log_adj_changes) {
244 const char *adj_name;
245 struct isis_dynhn *dyn;
246
247 dyn = dynhn_find_by_id(adj->sysid);
248 if (dyn)
249 adj_name = dyn->hostname;
250 else
251 adj_name = sysid_print(adj->sysid);
252
253 zlog_info(
254 "%%ADJCHANGE: Adjacency to %s (%s) changed from %s to %s, %s",
255 adj_name, adj->circuit->interface->name,
256 adj_state2string(old_state),
257 adj_state2string(new_state),
258 reason ? reason : "unspecified");
259 }
260
261 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
262 del = false;
263 for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
264 if ((adj->level & level) == 0)
265 continue;
266 if (new_state == ISIS_ADJ_UP) {
267 circuit->upadjcount[level - 1]++;
268 hook_call(isis_adj_state_change_hook, adj);
269 /* update counter & timers for debugging
270 * purposes */
271 adj->last_flap = time(NULL);
272 adj->flaps++;
273 } else if (new_state == ISIS_ADJ_DOWN) {
274 listnode_delete(circuit->u.bc.adjdb[level - 1],
275 adj);
276
277 circuit->upadjcount[level - 1]--;
278 if (circuit->upadjcount[level - 1] == 0)
279 isis_tx_queue_clean(circuit->tx_queue);
280
281 hook_call(isis_adj_state_change_hook, adj);
282 del = true;
283 }
284
285 if (circuit->u.bc.lan_neighs[level - 1]) {
286 list_delete_all_node(
287 circuit->u.bc.lan_neighs[level - 1]);
288 isis_adj_build_neigh_list(
289 circuit->u.bc.adjdb[level - 1],
290 circuit->u.bc.lan_neighs[level - 1]);
291 }
292
293 /* On adjacency state change send new pseudo LSP if we
294 * are the DR */
295 if (circuit->u.bc.is_dr[level - 1])
296 lsp_regenerate_schedule_pseudo(circuit, level);
297 }
298
299 if (del)
300 isis_delete_adj(adj);
301
302 } else if (circuit->circ_type == CIRCUIT_T_P2P) {
303 del = false;
304 for (int level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
305 if ((adj->level & level) == 0)
306 continue;
307 if (new_state == ISIS_ADJ_UP) {
308 circuit->upadjcount[level - 1]++;
309 hook_call(isis_adj_state_change_hook, adj);
310
311 /* update counter & timers for debugging
312 * purposes */
313 adj->last_flap = time(NULL);
314 adj->flaps++;
315
316 if (level == IS_LEVEL_1) {
317 thread_add_timer(master, send_l1_csnp,
318 circuit, 0,
319 &circuit->t_send_csnp[0]);
320 } else {
321 thread_add_timer(master, send_l2_csnp,
322 circuit, 0,
323 &circuit->t_send_csnp[1]);
324 }
325 } else if (new_state == ISIS_ADJ_DOWN) {
326 if (adj->circuit->u.p2p.neighbor == adj)
327 adj->circuit->u.p2p.neighbor = NULL;
328 circuit->upadjcount[level - 1]--;
329 if (circuit->upadjcount[level - 1] == 0)
330 isis_tx_queue_clean(circuit->tx_queue);
331
332 hook_call(isis_adj_state_change_hook, adj);
333 del = true;
334 }
335 }
336
337 if (del)
338 isis_delete_adj(adj);
339 }
340 }
341
342
343 void isis_adj_print(struct isis_adjacency *adj)
344 {
345 struct isis_dynhn *dyn;
346
347 if (!adj)
348 return;
349 dyn = dynhn_find_by_id(adj->sysid);
350 if (dyn)
351 zlog_debug("%s", dyn->hostname);
352
353 zlog_debug("SystemId %20s SNPA %s, level %d\nHolding Time %d",
354 sysid_print(adj->sysid), snpa_print(adj->snpa), adj->level,
355 adj->hold_time);
356 if (adj->ipv4_address_count) {
357 zlog_debug("IPv4 Address(es):");
358 for (unsigned int i = 0; i < adj->ipv4_address_count; i++)
359 zlog_debug("%s", inet_ntoa(adj->ipv4_addresses[i]));
360 }
361
362 if (adj->ipv6_address_count) {
363 zlog_debug("IPv6 Address(es):");
364 for (unsigned int i = 0; i < adj->ipv6_address_count; i++) {
365 char buf[INET6_ADDRSTRLEN];
366 inet_ntop(AF_INET6, &adj->ipv6_addresses[i], buf,
367 sizeof(buf));
368 zlog_debug("%s", buf);
369 }
370 }
371 zlog_debug("Speaks: %s", nlpid2string(&adj->nlpids));
372
373 return;
374 }
375
376 int isis_adj_expire(struct thread *thread)
377 {
378 struct isis_adjacency *adj;
379
380 /*
381 * Get the adjacency
382 */
383 adj = THREAD_ARG(thread);
384 assert(adj);
385 adj->t_expire = NULL;
386
387 /* trigger the adj expire event */
388 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "holding time expired");
389
390 return 0;
391 }
392
393 /*
394 * show isis neighbor [detail]
395 */
396 void isis_adj_print_vty(struct isis_adjacency *adj, struct vty *vty,
397 char detail)
398 {
399 time_t now;
400 struct isis_dynhn *dyn;
401 int level;
402
403 dyn = dynhn_find_by_id(adj->sysid);
404 if (dyn)
405 vty_out(vty, " %-20s", dyn->hostname);
406 else
407 vty_out(vty, " %-20s", sysid_print(adj->sysid));
408
409 if (detail == ISIS_UI_LEVEL_BRIEF) {
410 if (adj->circuit)
411 vty_out(vty, "%-12s", adj->circuit->interface->name);
412 else
413 vty_out(vty, "NULL circuit!");
414 vty_out(vty, "%-3u", adj->level); /* level */
415 vty_out(vty, "%-13s", adj_state2string(adj->adj_state));
416 now = time(NULL);
417 if (adj->last_upd)
418 vty_out(vty, "%-9llu",
419 (unsigned long long)adj->last_upd
420 + adj->hold_time - now);
421 else
422 vty_out(vty, "- ");
423 vty_out(vty, "%-10s", snpa_print(adj->snpa));
424 vty_out(vty, "\n");
425 }
426
427 if (detail == ISIS_UI_LEVEL_DETAIL) {
428 level = adj->level;
429 vty_out(vty, "\n");
430 if (adj->circuit)
431 vty_out(vty, " Interface: %s",
432 adj->circuit->interface->name);
433 else
434 vty_out(vty, " Interface: NULL circuit");
435 vty_out(vty, ", Level: %u", adj->level); /* level */
436 vty_out(vty, ", State: %s", adj_state2string(adj->adj_state));
437 now = time(NULL);
438 if (adj->last_upd)
439 vty_out(vty, ", Expires in %s",
440 time2string(adj->last_upd + adj->hold_time
441 - now));
442 else
443 vty_out(vty, ", Expires in %s",
444 time2string(adj->hold_time));
445 vty_out(vty, "\n");
446 vty_out(vty, " Adjacency flaps: %u", adj->flaps);
447 vty_out(vty, ", Last: %s ago",
448 time2string(now - adj->last_flap));
449 vty_out(vty, "\n");
450 vty_out(vty, " Circuit type: %s",
451 circuit_t2string(adj->circuit_t));
452 vty_out(vty, ", Speaks: %s", nlpid2string(&adj->nlpids));
453 vty_out(vty, "\n");
454 if (adj->mt_count != 1
455 || adj->mt_set[0] != ISIS_MT_IPV4_UNICAST) {
456 vty_out(vty, " Topologies:\n");
457 for (unsigned int i = 0; i < adj->mt_count; i++)
458 vty_out(vty, " %s\n",
459 isis_mtid2str(adj->mt_set[i]));
460 }
461 vty_out(vty, " SNPA: %s", snpa_print(adj->snpa));
462 if (adj->circuit
463 && (adj->circuit->circ_type == CIRCUIT_T_BROADCAST)) {
464 dyn = dynhn_find_by_id(adj->lanid);
465 if (dyn)
466 vty_out(vty, ", LAN id: %s.%02x", dyn->hostname,
467 adj->lanid[ISIS_SYS_ID_LEN]);
468 else
469 vty_out(vty, ", LAN id: %s.%02x",
470 sysid_print(adj->lanid),
471 adj->lanid[ISIS_SYS_ID_LEN]);
472
473 vty_out(vty, "\n");
474 vty_out(vty, " LAN Priority: %u",
475 adj->prio[adj->level - 1]);
476
477 vty_out(vty, ", %s, DIS flaps: %u, Last: %s ago",
478 isis_disflag2string(
479 adj->dis_record[ISIS_LEVELS + level - 1]
480 .dis),
481 adj->dischanges[level - 1],
482 time2string(now - (adj->dis_record[ISIS_LEVELS
483 + level - 1]
484 .last_dis_change)));
485 }
486 vty_out(vty, "\n");
487
488 if (adj->area_address_count) {
489 vty_out(vty, " Area Address(es):\n");
490 for (unsigned int i = 0; i < adj->area_address_count;
491 i++) {
492 vty_out(vty, " %s\n",
493 isonet_print(adj->area_addresses[i]
494 .area_addr,
495 adj->area_addresses[i]
496 .addr_len));
497 }
498 }
499 if (adj->ipv4_address_count) {
500 vty_out(vty, " IPv4 Address(es):\n");
501 for (unsigned int i = 0; i < adj->ipv4_address_count;
502 i++)
503 vty_out(vty, " %s\n",
504 inet_ntoa(adj->ipv4_addresses[i]));
505 }
506 if (adj->ipv6_address_count) {
507 vty_out(vty, " IPv6 Address(es):\n");
508 for (unsigned int i = 0; i < adj->ipv6_address_count;
509 i++) {
510 char buf[INET6_ADDRSTRLEN];
511 inet_ntop(AF_INET6, &adj->ipv6_addresses[i],
512 buf, sizeof(buf));
513 vty_out(vty, " %s\n", buf);
514 }
515 }
516 vty_out(vty, "\n");
517 }
518 return;
519 }
520
521 void isis_adj_build_neigh_list(struct list *adjdb, struct list *list)
522 {
523 struct isis_adjacency *adj;
524 struct listnode *node;
525
526 if (!list) {
527 zlog_warn("isis_adj_build_neigh_list(): NULL list");
528 return;
529 }
530
531 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
532 if (!adj) {
533 zlog_warn("isis_adj_build_neigh_list(): NULL adj");
534 return;
535 }
536
537 if ((adj->adj_state == ISIS_ADJ_UP
538 || adj->adj_state == ISIS_ADJ_INITIALIZING))
539 listnode_add(list, adj->snpa);
540 }
541 return;
542 }
543
544 void isis_adj_build_up_list(struct list *adjdb, struct list *list)
545 {
546 struct isis_adjacency *adj;
547 struct listnode *node;
548
549 if (adjdb == NULL) {
550 zlog_warn("isis_adj_build_up_list(): adjacency DB is empty");
551 return;
552 }
553
554 if (!list) {
555 zlog_warn("isis_adj_build_up_list(): NULL list");
556 return;
557 }
558
559 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
560 if (!adj) {
561 zlog_warn("isis_adj_build_up_list(): NULL adj");
562 return;
563 }
564
565 if (adj->adj_state == ISIS_ADJ_UP)
566 listnode_add(list, adj);
567 }
568
569 return;
570 }
571
572 int isis_adj_usage2levels(enum isis_adj_usage usage)
573 {
574 switch (usage) {
575 case ISIS_ADJ_LEVEL1:
576 return IS_LEVEL_1;
577 case ISIS_ADJ_LEVEL2:
578 return IS_LEVEL_2;
579 case ISIS_ADJ_LEVEL1AND2:
580 return IS_LEVEL_1 | IS_LEVEL_2;
581 default:
582 break;
583 }
584 return 0;
585 }