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