]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_adjacency.c
Merge pull request #1966 from donaldsharp/vrf_late_to_the_party
[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
52 extern struct isis *isis;
53
54 static struct isis_adjacency *adj_alloc(const uint8_t *id)
55 {
56 struct isis_adjacency *adj;
57
58 adj = XCALLOC(MTYPE_ISIS_ADJACENCY, sizeof(struct isis_adjacency));
59 memcpy(adj->sysid, id, ISIS_SYS_ID_LEN);
60
61 return adj;
62 }
63
64 struct isis_adjacency *isis_new_adj(const uint8_t *id, const uint8_t *snpa,
65 int level, struct isis_circuit *circuit)
66 {
67 struct isis_adjacency *adj;
68 int i;
69
70 adj = adj_alloc(id); /* P2P kludge */
71
72 if (snpa) {
73 memcpy(adj->snpa, snpa, ETH_ALEN);
74 } else {
75 memset(adj->snpa, ' ', ETH_ALEN);
76 }
77
78 adj->circuit = circuit;
79 adj->level = level;
80 adj->flaps = 0;
81 adj->last_flap = time(NULL);
82 adj->threeway_state = ISIS_THREEWAY_DOWN;
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;
97 }
98
99 struct isis_adjacency *isis_adj_lookup(const uint8_t *sysid, struct list *adjdb)
100 {
101 struct isis_adjacency *adj;
102 struct listnode *node;
103
104 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
105 if (memcmp(adj->sysid, sysid, ISIS_SYS_ID_LEN) == 0)
106 return adj;
107
108 return NULL;
109 }
110
111 struct isis_adjacency *isis_adj_lookup_snpa(const uint8_t *ssnpa,
112 struct list *adjdb)
113 {
114 struct listnode *node;
115 struct isis_adjacency *adj;
116
117 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
118 if (memcmp(adj->snpa, ssnpa, ETH_ALEN) == 0)
119 return adj;
120
121 return NULL;
122 }
123
124 void isis_delete_adj(void *arg)
125 {
126 struct isis_adjacency *adj = arg;
127
128 if (!adj)
129 return;
130
131 THREAD_TIMER_OFF(adj->t_expire);
132
133 /* remove from SPF trees */
134 spftree_area_adj_del(adj->circuit->area, adj);
135
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);
142
143 adj_mt_finish(adj);
144
145 XFREE(MTYPE_ISIS_ADJACENCY, adj);
146 return;
147 }
148
149 static const char *adj_state2string(int state)
150 {
151
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 */
164 }
165
166 void 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
172 if (tw_adj && !adj->circuit->disable_threeway_adj) {
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
211 void isis_adj_state_change(struct isis_adjacency *adj,
212 enum isis_adj_state new_state, const char *reason)
213 {
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)
236 adj_name = dyn->hostname;
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);
264
265 circuit->upadjcount[level - 1]--;
266 if (circuit->upadjcount[level - 1] == 0)
267 isis_circuit_lsp_queue_clean(circuit);
268
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]--;
318 if (circuit->upadjcount[level - 1] == 0)
319 isis_circuit_lsp_queue_clean(circuit);
320
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;
334 }
335
336
337 void isis_adj_print(struct isis_adjacency *adj)
338 {
339 struct isis_dynhn *dyn;
340
341 if (!adj)
342 return;
343 dyn = dynhn_find_by_id(adj->sysid);
344 if (dyn)
345 zlog_debug("%s", dyn->hostname);
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);
350 if (adj->ipv4_address_count) {
351 zlog_debug("IPv4 Address(es):");
352 for (unsigned int i = 0; i < adj->ipv4_address_count; i++)
353 zlog_debug("%s", inet_ntoa(adj->ipv4_addresses[i]));
354 }
355
356 if (adj->ipv6_address_count) {
357 zlog_debug("IPv6 Address(es):");
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);
363 }
364 }
365 zlog_debug("Speaks: %s", nlpid2string(&adj->nlpids));
366
367 return;
368 }
369
370 int isis_adj_expire(struct thread *thread)
371 {
372 struct isis_adjacency *adj;
373
374 /*
375 * Get the adjacency
376 */
377 adj = THREAD_ARG(thread);
378 assert(adj);
379 adj->t_expire = NULL;
380
381 /* trigger the adj expire event */
382 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "holding time expired");
383
384 return 0;
385 }
386
387 /*
388 * show isis neighbor [detail]
389 */
390 void isis_adj_print_vty(struct isis_adjacency *adj, struct vty *vty,
391 char detail)
392 {
393 time_t now;
394 struct isis_dynhn *dyn;
395 int level;
396
397 dyn = dynhn_find_by_id(adj->sysid);
398 if (dyn)
399 vty_out(vty, " %-20s", dyn->hostname);
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");
419 }
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)
460 vty_out(vty, ", LAN id: %s.%02x", dyn->hostname,
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],
476 time2string(now - (adj->dis_record[ISIS_LEVELS
477 + level - 1]
478 .last_dis_change)));
479 }
480 vty_out(vty, "\n");
481
482 if (adj->area_address_count) {
483 vty_out(vty, " Area Address(es):\n");
484 for (unsigned int i = 0; i < adj->area_address_count;
485 i++) {
486 vty_out(vty, " %s\n",
487 isonet_print(adj->area_addresses[i]
488 .area_addr,
489 adj->area_addresses[i]
490 .addr_len));
491 }
492 }
493 if (adj->ipv4_address_count) {
494 vty_out(vty, " IPv4 Address(es):\n");
495 for (unsigned int i = 0; i < adj->ipv4_address_count;
496 i++)
497 vty_out(vty, " %s\n",
498 inet_ntoa(adj->ipv4_addresses[i]));
499 }
500 if (adj->ipv6_address_count) {
501 vty_out(vty, " IPv6 Address(es):\n");
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);
508 }
509 }
510 vty_out(vty, "\n");
511 }
512 return;
513 }
514
515 void isis_adj_build_neigh_list(struct list *adjdb, struct list *list)
516 {
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;
523 }
524
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;
536 }
537
538 void isis_adj_build_up_list(struct list *adjdb, struct list *list)
539 {
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;
551 }
552
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 }
558
559 if (adj->adj_state == ISIS_ADJ_UP)
560 listnode_add(list, adj);
561 }
562
563 return;
564 }
565
566 int isis_adj_usage2levels(enum isis_adj_usage usage)
567 {
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;
579 }