blob: 8079bd17b5f71abf13967008fc43629c2f1bd839 [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
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; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24#include <stdio.h>
25#include <limits.h>
26#include <string.h>
27#include <zebra.h>
28#include <net/ethernet.h>
29
30
31#include "log.h"
32#include "memory.h"
33#include "hash.h"
34#include "vty.h"
35#include "linklist.h"
36#include "thread.h"
37#include "if.h"
38#include "stream.h"
39
40#include "isisd/dict.h"
41#include "isisd/include-netbsd/iso.h"
42#include "isisd/isis_constants.h"
43#include "isisd/isis_common.h"
44#include "isisd/isisd.h"
45#include "isisd/isis_circuit.h"
46#include "isisd/isis_adjacency.h"
47#include "isisd/isis_misc.h"
48#include "isisd/isis_dr.h"
49#include "isisd/isis_dynhn.h"
50#include "isisd/isis_pdu.h"
51
52
53extern struct isis *isis;
54
55
56struct isis_adjacency *
57adj_alloc (u_char *id)
58{
59 struct isis_adjacency *adj;
60
61 adj = XMALLOC (MTYPE_ISIS_ADJACENCY, sizeof (struct isis_adjacency));
62 memset (adj, 0, sizeof (struct isis_adjacency));
63 memcpy (adj->sysid, id, ISIS_SYS_ID_LEN);
64
65 return adj;
66}
67
68struct isis_adjacency *
69isis_new_adj (u_char *id, u_char *snpa, int level,
70 struct isis_circuit *circuit)
71{
72
73 struct isis_adjacency *adj;
74 int i;
75
76 adj = adj_alloc (id); /* P2P kludge */
77
78 if (adj == NULL){
79 zlog_err ("Out of memory!");
80 return NULL;
81 }
82
83 memcpy (adj->snpa, snpa, 6);
84 adj->circuit = circuit;
85 adj->level = level;
86 adj->flaps = 0;
87 adj->last_flap = time (NULL);
88 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
89 listnode_add (circuit->u.bc.adjdb[level-1], adj);
90 adj->dischanges[level - 1] = 0;
91 for (i = 0; i < DIS_RECORDS; i++) /* clear N DIS state change records */
92 {
93 adj->dis_record[(i * ISIS_LEVELS) + level - 1].dis
94 = ISIS_UNKNOWN_DIS;
95 adj->dis_record[(i * ISIS_LEVELS) + level - 1].last_dis_change
96 = time (NULL);
97 }
98 }
99
100 return adj;
101}
102
103struct isis_adjacency *
104isis_adj_lookup (u_char *sysid, struct list *adjdb)
105{
106 struct isis_adjacency *adj;
107 struct listnode *node;
108
109 for (node = listhead (adjdb); node; nextnode (node)) {
110 adj = getdata (node);
111 if (memcmp (adj->sysid, sysid, ISIS_SYS_ID_LEN) == 0)
112 return adj;
113 }
114
115 return NULL;
116}
117
118
119struct isis_adjacency *
120isis_adj_lookup_snpa (u_char *ssnpa, struct list *adjdb)
121{
122 struct listnode *node;
123 struct isis_adjacency *adj;
124
125 for (node = listhead (adjdb); node; nextnode (node)) {
126 adj = getdata (node);
127 if (memcmp (adj->snpa, ssnpa, ETH_ALEN) == 0)
128 return adj;
129 }
130
131 return NULL;
132}
133
134/*
135 * When we recieve a NULL list, we will know its p2p
136 */
137void
138isis_delete_adj (struct isis_adjacency *adj, struct list *adjdb)
139{
140 struct isis_adjacency *adj2;
141 struct listnode *node;
142
143 if (adjdb) {
144 for (node = listhead (adjdb); node; nextnode (node)) {
145 adj2 = getdata (node);
146 if (adj2 == adj)
147 break;
148 }
149 listnode_delete (adjdb, node);
150 }
151
152 if (adj->ipv4_addrs)
153 list_delete (adj->ipv4_addrs);
154#ifdef HAVE_IPV6
155 if (adj->ipv6_addrs)
156 list_delete (adj->ipv6_addrs);
157#endif
158 if (adj) {
159 XFREE (MTYPE_ISIS_ADJACENCY,adj);
160 } else {
161 zlog_info ("tried to delete a non-existent adjacency");
162 }
163
164
165
166 return;
167}
168
169void
170isis_adj_state_change (struct isis_adjacency *adj, enum isis_adj_state state,
171 char *reason)
172
173{
174 int old_state;
175 int level = adj->level;
176 struct isis_circuit *circuit;
177
178 old_state = adj->adj_state;
179 adj->adj_state = state;
180
181 circuit = adj->circuit;
182
183 if (isis->debugs & DEBUG_ADJ_PACKETS) {
184 zlog_info ("ISIS-Adj (%s): Adjacency state change %d->%d: %s",
185 circuit->area->area_tag,
186 old_state,
187 state,
188 reason ? reason : "unspecified");
189 }
190
191 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
192 if (state == ISIS_ADJ_UP)
193 circuit->upadjcount[level-1]++;
194 if (state == ISIS_ADJ_DOWN) {
195 isis_delete_adj (adj, adj->circuit->u.bc.adjdb[level - 1]);
196 circuit->upadjcount[level-1]--;
197 }
198
199 list_delete_all_node (circuit->u.bc.lan_neighs[level - 1]);
200 isis_adj_build_neigh_list (circuit->u.bc.adjdb[level - 1],
201 circuit->u.bc.lan_neighs[level - 1]);
202 } else if (state == ISIS_ADJ_UP) { /* p2p interface */
203 if (adj->sys_type == ISIS_SYSTYPE_UNKNOWN)
204 send_hello (circuit, 1);
205
206 /* update counter & timers for debugging purposes */
207 adj->last_flap = time(NULL);
208 adj->flaps++;
209
210 /* 7.3.17 - going up on P2P -> send CSNP */
211 /* FIXME: yup, I know its wrong... but i will do it! (for now) */
212 send_csnp (circuit,1);
213 send_csnp (circuit,2);
214 } else if (state == ISIS_ADJ_DOWN) { /* p2p interface */
215 adj->circuit->u.p2p.neighbor = NULL;
216 isis_delete_adj (adj, NULL);
217 }
218 return;
219}
220
221
222void
223isis_adj_print (struct isis_adjacency *adj)
224{
225 struct isis_dynhn *dyn;
226 struct listnode *node;
227 struct in_addr *ipv4_addr;
228#ifdef HAVE_IPV6
229 struct in6_addr *ipv6_addr;
230 u_char ip6 [INET6_ADDRSTRLEN];
231#endif /* HAVE_IPV6 */
232
233 if(!adj)
234 return;
235 dyn = dynhn_find_by_id (adj->sysid);
236 if (dyn)
237 zlog_info ("%s", dyn->name.name);
238
239 zlog_info ("SystemId %20s SNPA %s, level %d\nHolding Time %d",
240 adj->sysid ? sysid_print (adj->sysid) : "unknown" ,
241 snpa_print (adj->snpa),
242 adj->level, adj->hold_time);
243 if (adj->ipv4_addrs && listcount (adj->ipv4_addrs) > 0) {
244 zlog_info ("IPv4 Addresses:");
245
246 for (node = listhead (adj->ipv4_addrs); node; nextnode (node)) {
247 ipv4_addr = getdata (node);
248 zlog_info ("%s", inet_ntoa(*ipv4_addr));
249 }
250 }
251
252#ifdef HAVE_IPV6
253 if (adj->ipv6_addrs && listcount (adj->ipv6_addrs) > 0) {
254 zlog_info ("IPv6 Addresses:");
255 for (node = listhead (adj->ipv6_addrs); node; nextnode (node)) {
256 ipv6_addr = getdata (node);
257 inet_ntop (AF_INET6, ipv6_addr, ip6, INET6_ADDRSTRLEN);
258 zlog_info ("%s", ip6);
259 }
260 }
261#endif /* HAVE_IPV6 */
262 zlog_info ("Speaks: %s", nlpid2string(&adj->nlpids));
263
264
265 return;
266}
267
268int
269isis_adj_expire (struct thread *thread)
270{
271 struct isis_adjacency *adj;
272 int level;
273
274 /*
275 * Get the adjacency
276 */
277 adj = THREAD_ARG (thread);
278 assert (adj);
279 level = adj->level;
280
281 /* trigger the adj expire event */
282 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "holding time expired");
283
284 return 0;
285}
286
287const char *
288adj_state2string (int state)
289{
290
291 switch (state) {
292 case ISIS_ADJ_INITIALIZING:
293 return "Initializing";
294 case ISIS_ADJ_UP:
295 return "Up";
296 case ISIS_ADJ_DOWN:
297 return "Down";
298 default:
299 return "Unknown";
300 }
301
302 return NULL; /* not reached */
303}
304
305/*
306 * show clns/isis neighbor (detail)
307 */
308void
309isis_adj_print_vty2 (struct isis_adjacency *adj, struct vty *vty, char detail)
310{
311
312#ifdef HAVE_IPV6
313 struct in6_addr *ipv6_addr;
314 u_char ip6 [INET6_ADDRSTRLEN];
315#endif /* HAVE_IPV6 */
316 struct in_addr *ip_addr;
317 time_t now;
318 struct isis_dynhn *dyn;
319 int level;
320 struct listnode *node;
321
322 dyn = dynhn_find_by_id (adj->sysid);
323 if (dyn)
324 vty_out (vty, " %-20s", dyn->name.name);
325 else if (adj->sysid){
326 vty_out (vty, " %-20s", sysid_print (adj->sysid));
327 } else {
328 vty_out (vty, " unknown ");
329 }
330
331 if (detail == ISIS_UI_LEVEL_BRIEF) {
332 if (adj->circuit)
333 vty_out (vty, "%-12s",adj->circuit->interface->name);
334 else
335 vty_out (vty, "NULL circuit!");
336 vty_out (vty, "%-3u", adj->level); /* level */
337 vty_out (vty, "%-13s", adj_state2string (adj->adj_state));
338 now = time (NULL);
339 vty_out (vty, "%-9lu", adj->last_upd + adj->hold_time - now);
340 vty_out (vty, "%-10s", snpa_print (adj->snpa));
341 vty_out (vty, "%s", VTY_NEWLINE);
342 }
343
344 if (detail == ISIS_UI_LEVEL_DETAIL) {
345 level = adj->level;
346 if (adj->circuit)
347 vty_out (vty, "%s Interface: %s",
348 VTY_NEWLINE,
349 adj->circuit->interface->name); /* interface name */
350 else
351 vty_out (vty, "NULL circuit!%s", VTY_NEWLINE);
352 vty_out (vty, ", Level: %u", adj->level); /* level */
353 vty_out (vty, ", State: %s", adj_state2string (adj->adj_state));
354 now = time (NULL);
355 vty_out (vty, ", Expires in %s",
356 time2string (adj->last_upd + adj->hold_time - now));
357 vty_out (vty, "%s Adjacency flaps: %u",
358 VTY_NEWLINE,
359 adj->flaps);
360 vty_out (vty, ", Last: %s ago", time2string(now - adj->last_flap));
361 vty_out (vty, "%s Circuit type: %s",
362 VTY_NEWLINE,
363 circuit_t2string(adj->circuit_t));
364 vty_out (vty, ", Speaks: %s", nlpid2string(&adj->nlpids));
365 vty_out (vty, "%s SNPA: %s",
366 VTY_NEWLINE,
367 snpa_print (adj->snpa));
368 dyn = dynhn_find_by_id (adj->lanid);
369 if (dyn)
370 vty_out (vty, ", LAN id: %s.%02x",
371 dyn->name.name,
372 adj->lanid[ISIS_SYS_ID_LEN]);
373 else
374 vty_out (vty, ", LAN id: %s.%02x",
375 sysid_print (adj->lanid),
376 adj->lanid[ISIS_SYS_ID_LEN]);
377
378 vty_out (vty, "%s Priority: %u",
379 VTY_NEWLINE,
380 adj->prio[adj->level-1]);
381
382 vty_out (vty, ", %s, DIS flaps: %u, Last: %s ago%s",
383 isis_disflag2string(adj->dis_record[ISIS_LEVELS+level-1].dis),
384 adj->dischanges[level-1],
385 time2string (now -
386 (adj->dis_record[ISIS_LEVELS + level - 1].last_dis_change)),
387 VTY_NEWLINE);
388
389 if (adj->ipv4_addrs && listcount (adj->ipv4_addrs) > 0) {
390 vty_out (vty, " IPv4 Addresses:%s", VTY_NEWLINE);
391 for (node = listhead (adj->ipv4_addrs);node; nextnode (node)) {
392 ip_addr = getdata (node);
393 vty_out (vty, " %s%s", inet_ntoa(*ip_addr), VTY_NEWLINE);
394 }
395 }
396#ifdef HAVE_IPV6
397 if (adj->ipv6_addrs && listcount (adj->ipv6_addrs) > 0) {
398 vty_out (vty, " IPv6 Addresses:%s", VTY_NEWLINE);
399 for (node = listhead (adj->ipv6_addrs); node; nextnode (node)) {
400 ipv6_addr = getdata (node);
401 inet_ntop (AF_INET6, ipv6_addr, ip6, INET6_ADDRSTRLEN);
402 vty_out (vty, " %s%s", ip6, VTY_NEWLINE);
403 }
404 }
405#endif /* HAVE_IPV6 */
406 vty_out (vty, "%s", VTY_NEWLINE);
407 }
408 return;
409}
410
411void
412isis_adj_print_vty (struct isis_adjacency *adj, struct vty *vty) {
413 isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_BRIEF);
414}
415
416void
417isis_adj_print_vty_detail (struct isis_adjacency *adj, struct vty *vty) {
418 isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_DETAIL);
419}
420
421void
422isis_adj_print_vty_extensive (struct isis_adjacency *adj, struct vty *vty) {
423 isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_EXTENSIVE);
424}
425
426void
427isis_adj_p2p_print_vty (struct isis_adjacency *adj, struct vty *vty)
428{
429 isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_BRIEF);
430}
431
432void
433isis_adj_p2p_print_vty_detail (struct isis_adjacency *adj, struct vty *vty)
434{
435 isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_DETAIL);
436}
437
438void
439isis_adj_p2p_print_vty_extensive (struct isis_adjacency *adj, struct vty *vty)
440{
441 isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_EXTENSIVE);
442}
443
444void
445isis_adjdb_iterate (struct list *adjdb, void (*func)(struct isis_adjacency*,
446 void *), void *arg)
447{
448 struct listnode *node;
449 struct isis_adjacency *adj;
450 for (node = listhead (adjdb); node; nextnode (node)) {
451 adj = getdata (node);
452 (*func)(adj, arg);
453 }
454}
455
456void
457isis_adj_build_neigh_list (struct list *adjdb, struct list *list)
458
459{
460 struct isis_adjacency *adj;
461 struct listnode *node;
462
463
464 if (!list) {
465 zlog_warn ("isis_adj_build_neigh_list(): NULL list");
466 return;
467 }
468
469 for (node = listhead (adjdb); node; nextnode (node)) {
470 adj = getdata (node);
471 if (!adj) {
472 zlog_warn ("isis_adj_build_neigh_list(): NULL adj");
473 return;
474 }
475
476 if ((adj->adj_state == ISIS_ADJ_UP ||
477 adj->adj_state == ISIS_ADJ_INITIALIZING))
478 listnode_add (list, adj->snpa);
479 }
480 return;
481}
482
483void
484isis_adj_build_up_list (struct list *adjdb, struct list *list)
485{
486 struct isis_adjacency *adj;
487 struct listnode *node;
488
489 if (!list) {
490 zlog_warn ("isis_adj_build_up_list(): NULL list");
491 return;
492 }
493
494 for (node = listhead (adjdb); node; nextnode (node)) {
495 adj = getdata (node);
496
497 if (!adj) {
498 zlog_warn ("isis_adj_build_up_list(): NULL adj");
499 return;
500 }
501
502 if (adj->adj_state == ISIS_ADJ_UP)
503 listnode_add (list, adj);
504 }
505
506 return;
507}
508