]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_misc.c
Merge pull request #531 from qlyoung/fix-stack-ref
[mirror_frr.git] / isisd / isis_misc.c
CommitLineData
eb5d44eb 1/*
2 * IS-IS Rout(e)ing protocol - isis_misc.h
3 * Miscellanous routines
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
eb5d44eb 24#include <zebra.h>
eb5d44eb 25
26#include "stream.h"
27#include "vty.h"
28#include "hash.h"
29#include "if.h"
9e867fe6 30#include "command.h"
eb5d44eb 31
32#include "isisd/dict.h"
33#include "isisd/isis_constants.h"
34#include "isisd/isis_common.h"
3f045a08 35#include "isisd/isis_flags.h"
eb5d44eb 36#include "isisd/isis_circuit.h"
3f045a08 37#include "isisd/isis_csm.h"
eb5d44eb 38#include "isisd/isisd.h"
39#include "isisd/isis_misc.h"
40
41#include "isisd/isis_tlv.h"
42#include "isisd/isis_lsp.h"
43#include "isisd/isis_constants.h"
44#include "isisd/isis_adjacency.h"
3f045a08 45#include "isisd/isis_dynhn.h"
eb5d44eb 46
73d1aead 47/* staticly assigned vars for printing purposes */
48struct in_addr new_prefix;
49/* len of xxxx.xxxx.xxxx + place for #0 termination */
50char sysid[15];
51/* len of xxxx.xxxx.xxxx + place for #0 termination */
52char snpa[15];
53/* len of xx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xxxx.xx */
54char isonet[51];
55/* + place for #0 termination */
56/* len of xxxx.xxxx.xxxx.xx.xx + place for #0 termination */
57char lspid[21];
58/* len of xxYxxMxWxdxxhxxmxxs + place for #0 termination */
59char datestring[20];
60char nlpidstring[30];
61
eb5d44eb 62/*
63 * This converts the isonet to its printable format
64 */
1cd80845 65const char *
02e33d3e 66isonet_print (const u_char * from, int len)
f390d2c7 67{
eb5d44eb 68 int i = 0;
69 char *pos = isonet;
70
f390d2c7 71 if (!from)
eb5d44eb 72 return "unknown";
73
f390d2c7 74 while (i < len)
75 {
76 if (i & 1)
77 {
78 sprintf (pos, "%02x", *(from + i));
79 pos += 2;
80 }
81 else
82 {
83 if (i == (len - 1))
84 { /* No dot at the end of address */
85 sprintf (pos, "%02x", *(from + i));
86 pos += 2;
87 }
88 else
89 {
90 sprintf (pos, "%02x.", *(from + i));
91 pos += 3;
92 }
93 }
94 i++;
eb5d44eb 95 }
eb5d44eb 96 *(pos) = '\0';
97 return isonet;
98}
99
100/*
101 * Returns 0 on error, length of buff on ok
102 * extract dot from the dotted str, and insert all the number in a buff
103 */
104int
3f045a08 105dotformat2buff (u_char * buff, const char * dotted)
eb5d44eb 106{
107 int dotlen, len = 0;
3f045a08 108 const char *pos = dotted;
eb5d44eb 109 u_char number[3];
110 int nextdotpos = 2;
111
112 number[2] = '\0';
41b36e90 113 dotlen = strlen(dotted);
f390d2c7 114 if (dotlen > 50)
115 {
116 /* this can't be an iso net, its too long */
117 return 0;
eb5d44eb 118 }
119
f390d2c7 120 while ((pos - dotted) < dotlen && len < 20)
121 {
122 if (*pos == '.')
123 {
124 /* we expect the . at 2, and than every 5 */
125 if ((pos - dotted) != nextdotpos)
126 {
127 len = 0;
128 break;
129 }
130 nextdotpos += 5;
131 pos++;
132 continue;
133 }
134 /* we must have at least two chars left here */
135 if (dotlen - (pos - dotted) < 2)
136 {
137 len = 0;
138 break;
139 }
140
141 if ((isxdigit ((int) *pos)) && (isxdigit ((int) *(pos + 1))))
142 {
143 memcpy (number, pos, 2);
144 pos += 2;
145 }
146 else
147 {
148 len = 0;
149 break;
150 }
151
f7c43dcb 152 *(buff + len) = (char) strtol ((char *)number, NULL, 16);
f390d2c7 153 len++;
eb5d44eb 154 }
155
eb5d44eb 156 return len;
157}
f390d2c7 158
eb5d44eb 159/*
160 * conversion of XXXX.XXXX.XXXX to memory
161 */
162int
3f045a08 163sysid2buff (u_char * buff, const char * dotted)
f390d2c7 164{
eb5d44eb 165 int len = 0;
3f045a08 166 const char *pos = dotted;
eb5d44eb 167 u_char number[3];
168
169 number[2] = '\0';
170 // surely not a sysid_string if not 14 length
41b36e90 171 if (strlen (dotted) != 14)
f390d2c7 172 {
173 return 0;
eb5d44eb 174 }
175
f390d2c7 176 while (len < ISIS_SYS_ID_LEN)
177 {
178 if (*pos == '.')
179 {
180 /* the . is not positioned correctly */
181 if (((pos - dotted) != 4) && ((pos - dotted) != 9))
182 {
183 len = 0;
184 break;
185 }
186 pos++;
187 continue;
188 }
189 if ((isxdigit ((int) *pos)) && (isxdigit ((int) *(pos + 1))))
190 {
191 memcpy (number, pos, 2);
192 pos += 2;
193 }
194 else
195 {
196 len = 0;
197 break;
198 }
199
f7c43dcb 200 *(buff + len) = (char) strtol ((char *)number, NULL, 16);
f390d2c7 201 len++;
202 }
eb5d44eb 203
204 return len;
205
206}
207
208/*
209 * converts the nlpids struct (filled by TLV #129)
210 * into a string
211 */
212
213char *
f390d2c7 214nlpid2string (struct nlpids *nlpids)
215{
eb5d44eb 216 char *pos = nlpidstring;
217 int i;
218
f390d2c7 219 for (i = 0; i < nlpids->count; i++)
220 {
221 switch (nlpids->nlpids[i])
222 {
223 case NLPID_IP:
224 pos += sprintf (pos, "IPv4");
225 break;
226 case NLPID_IPV6:
227 pos += sprintf (pos, "IPv6");
228 break;
229 case NLPID_SNAP:
230 pos += sprintf (pos, "SNAP");
231 break;
232 case NLPID_CLNP:
233 pos += sprintf (pos, "CLNP");
234 break;
235 case NLPID_ESIS:
236 pos += sprintf (pos, "ES-IS");
237 break;
238 default:
239 pos += sprintf (pos, "unknown");
240 break;
241 }
242 if (nlpids->count - i > 1)
243 pos += sprintf (pos, ", ");
244
eb5d44eb 245 }
eb5d44eb 246
247 *(pos) = '\0';
f390d2c7 248
eb5d44eb 249 return nlpidstring;
250}
251
252/*
253 * supports the given af ?
254 */
f390d2c7 255int
eb5d44eb 256speaks (struct nlpids *nlpids, int family)
257{
258 int i, speaks = 0;
f390d2c7 259
260 if (nlpids == (struct nlpids *) NULL)
eb5d44eb 261 return speaks;
f390d2c7 262 for (i = 0; i < nlpids->count; i++)
263 {
264 if (family == AF_INET && nlpids->nlpids[i] == NLPID_IP)
265 speaks = 1;
266 if (family == AF_INET6 && nlpids->nlpids[i] == NLPID_IPV6)
267 speaks = 1;
268 }
eb5d44eb 269
270 return speaks;
271}
272
eb5d44eb 273/*
274 * Returns 0 on error, IS-IS Circuit Type on ok
275 */
f390d2c7 276int
3f045a08 277string2circuit_t (const char * str)
eb5d44eb 278{
f390d2c7 279
eb5d44eb 280 if (!str)
281 return 0;
f390d2c7 282
41b36e90 283 if (!strcmp (str, "level-1"))
eb5d44eb 284 return IS_LEVEL_1;
285
41b36e90 286 if (!strcmp (str, "level-2-only") || !strcmp (str, "level-2"))
eb5d44eb 287 return IS_LEVEL_2;
f390d2c7 288
41b36e90 289 if (!strcmp (str, "level-1-2"))
eb5d44eb 290 return IS_LEVEL_1_AND_2;
291
292 return 0;
293}
294
3f045a08
JB
295const char *
296circuit_state2string (int state)
297{
298
299 switch (state)
300 {
301 case C_STATE_INIT:
302 return "Init";
303 case C_STATE_CONF:
304 return "Config";
305 case C_STATE_UP:
306 return "Up";
307 default:
308 return "Unknown";
309 }
310 return NULL;
311}
312
313const char *
314circuit_type2string (int type)
315{
316
317 switch (type)
318 {
319 case CIRCUIT_T_P2P:
320 return "p2p";
321 case CIRCUIT_T_BROADCAST:
322 return "lan";
323 case CIRCUIT_T_LOOPBACK:
324 return "loopback";
325 default:
326 return "Unknown";
327 }
328 return NULL;
329}
330
eb5d44eb 331const char *
332circuit_t2string (int circuit_t)
333{
f390d2c7 334 switch (circuit_t)
335 {
336 case IS_LEVEL_1:
337 return "L1";
338 case IS_LEVEL_2:
339 return "L2";
340 case IS_LEVEL_1_AND_2:
341 return "L1L2";
342 default:
343 return "??";
344 }
345
346 return NULL; /* not reached */
eb5d44eb 347}
348
349const char *
350syst2string (int type)
351{
f390d2c7 352 switch (type)
353 {
354 case ISIS_SYSTYPE_ES:
355 return "ES";
356 case ISIS_SYSTYPE_IS:
357 return "IS";
358 case ISIS_SYSTYPE_L1_IS:
359 return "1";
360 case ISIS_SYSTYPE_L2_IS:
361 return "2";
362 default:
363 return "??";
364 }
365
366 return NULL; /* not reached */
eb5d44eb 367}
368
369/*
370 * Print functions - we print to static vars
371 */
1cd80845 372const char *
02e33d3e 373snpa_print (const u_char * from)
eb5d44eb 374{
375 int i = 0;
f7c43dcb 376 u_char *pos = (u_char *)snpa;
eb5d44eb 377
f390d2c7 378 if (!from)
eb5d44eb 379 return "unknown";
eb5d44eb 380
f390d2c7 381 while (i < ETH_ALEN - 1)
382 {
383 if (i & 1)
384 {
f7c43dcb 385 sprintf ((char *)pos, "%02x.", *(from + i));
f390d2c7 386 pos += 3;
387 }
388 else
389 {
f7c43dcb 390 sprintf ((char *)pos, "%02x", *(from + i));
f390d2c7 391 pos += 2;
392
393 }
394 i++;
eb5d44eb 395 }
eb5d44eb 396
f7c43dcb 397 sprintf ((char *)pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
eb5d44eb 398 pos += 2;
399 *(pos) = '\0';
400
401 return snpa;
402}
403
1cd80845 404const char *
02e33d3e 405sysid_print (const u_char * from)
eb5d44eb 406{
407 int i = 0;
408 char *pos = sysid;
409
f390d2c7 410 if (!from)
eb5d44eb 411 return "unknown";
412
f390d2c7 413 while (i < ISIS_SYS_ID_LEN - 1)
414 {
415 if (i & 1)
416 {
417 sprintf (pos, "%02x.", *(from + i));
418 pos += 3;
419 }
420 else
421 {
422 sprintf (pos, "%02x", *(from + i));
423 pos += 2;
424
425 }
426 i++;
427 }
eb5d44eb 428
429 sprintf (pos, "%02x", *(from + (ISIS_SYS_ID_LEN - 1)));
430 pos += 2;
431 *(pos) = '\0';
f390d2c7 432
eb5d44eb 433 return sysid;
434}
435
1cd80845 436const char *
02e33d3e 437rawlspid_print (const u_char * from)
eb5d44eb 438{
439 char *pos = lspid;
f390d2c7 440 if (!from)
eb5d44eb 441 return "unknown";
f390d2c7 442 memcpy (pos, sysid_print (from), 15);
eb5d44eb 443 pos += 14;
f390d2c7 444 sprintf (pos, ".%02x", LSP_PSEUDO_ID (from));
eb5d44eb 445 pos += 3;
f390d2c7 446 sprintf (pos, "-%02x", LSP_FRAGMENT (from));
eb5d44eb 447 pos += 3;
448
449 *(pos) = '\0';
450
451 return lspid;
452}
453
1cd80845 454const char *
f390d2c7 455time2string (u_int32_t time)
456{
457 char *pos = datestring;
eb5d44eb 458 u_int32_t rest;
459
f390d2c7 460 if (time == 0)
eb5d44eb 461 return "-";
462
f390d2c7 463 if (time / SECS_PER_YEAR)
464 pos += sprintf (pos, "%uY", time / SECS_PER_YEAR);
465 rest = time % SECS_PER_YEAR;
466 if (rest / SECS_PER_MONTH)
467 pos += sprintf (pos, "%uM", rest / SECS_PER_MONTH);
468 rest = rest % SECS_PER_MONTH;
469 if (rest / SECS_PER_WEEK)
470 pos += sprintf (pos, "%uw", rest / SECS_PER_WEEK);
471 rest = rest % SECS_PER_WEEK;
472 if (rest / SECS_PER_DAY)
473 pos += sprintf (pos, "%ud", rest / SECS_PER_DAY);
474 rest = rest % SECS_PER_DAY;
475 if (rest / SECS_PER_HOUR)
476 pos += sprintf (pos, "%uh", rest / SECS_PER_HOUR);
477 rest = rest % SECS_PER_HOUR;
478 if (rest / SECS_PER_MINUTE)
479 pos += sprintf (pos, "%um", rest / SECS_PER_MINUTE);
480 rest = rest % SECS_PER_MINUTE;
481 if (rest)
482 pos += sprintf (pos, "%us", rest);
483
eb5d44eb 484 *(pos) = 0;
485
486 return datestring;
487}
488
489/*
490 * routine to decrement a timer by a random
491 * number
492 *
493 * first argument is the timer and the second is
494 * the jitter
495 */
f390d2c7 496unsigned long
eb5d44eb 497isis_jitter (unsigned long timer, unsigned long jitter)
498{
f390d2c7 499 int j, k;
eb5d44eb 500
f390d2c7 501 if (jitter >= 100)
eb5d44eb 502 return timer;
503
504 if (timer == 1)
505 return timer;
506 /*
507 * randomizing just the percent value provides
508 * no good random numbers - hence the spread
509 * to RANDOM_SPREAD (100000), which is ok as
510 * most IS-IS timers are no longer than 16 bit
511 */
512
b06fd125 513 j = 1 + (int) ((RANDOM_SPREAD * random ()) / (RAND_MAX + 1.0));
eb5d44eb 514
f390d2c7 515 k = timer - (timer * (100 - jitter)) / 100;
eb5d44eb 516
517 timer = timer - (k * j / RANDOM_SPREAD);
518
519 return timer;
520}
521
522struct in_addr
f390d2c7 523newprefix2inaddr (u_char * prefix_start, u_char prefix_masklen)
eb5d44eb 524{
f390d2c7 525 memset (&new_prefix, 0, sizeof (new_prefix));
526 memcpy (&new_prefix, prefix_start, (prefix_masklen & 0x3F) ?
527 ((((prefix_masklen & 0x3F) - 1) >> 3) + 1) : 0);
eb5d44eb 528 return new_prefix;
529}
530
9e867fe6 531/*
532 * Returns host.name if any, otherwise
533 * it returns the system hostname.
534 */
535const char *
f390d2c7 536unix_hostname (void)
9e867fe6 537{
538 static struct utsname names;
539 const char *hostname;
9e867fe6 540
541 hostname = host.name;
f390d2c7 542 if (!hostname)
543 {
544 uname (&names);
545 hostname = names.nodename;
546 }
eb5d44eb 547
9e867fe6 548 return hostname;
549}
3f045a08
JB
550
551/*
552 * Returns the dynamic hostname associated with the passed system ID.
553 * If no dynamic hostname found then returns formatted system ID.
554 */
555const char *
02e33d3e 556print_sys_hostname (const u_char *sysid)
3f045a08
JB
557{
558 struct isis_dynhn *dyn;
559
560 if (!sysid)
561 return "nullsysid";
562
563 /* For our system ID return our host name */
564 if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0)
565 return unix_hostname();
566
567 dyn = dynhn_find_by_id (sysid);
568 if (dyn)
569 return (const char *)dyn->name.name;
570
571 return sysid_print (sysid);
572}
573
574/*
575 * This function is a generic utility that logs data of given length.
576 * Move this to a shared lib so that any protocol can use it.
577 */
578void
579zlog_dump_data (void *data, int len)
580{
581 int i;
582 unsigned char *p;
583 unsigned char c;
584 char bytestr[4];
585 char addrstr[10];
586 char hexstr[ 16*3 + 5];
587 char charstr[16*1 + 5];
588
589 p = data;
590 memset (bytestr, 0, sizeof(bytestr));
591 memset (addrstr, 0, sizeof(addrstr));
592 memset (hexstr, 0, sizeof(hexstr));
593 memset (charstr, 0, sizeof(charstr));
594
595 for (i = 1; i <= len; i++)
596 {
597 c = *p;
598 if (isalnum (c) == 0)
599 c = '.';
600
601 /* store address for this line */
602 if ((i % 16) == 1)
603 snprintf (addrstr, sizeof(addrstr), "%p", p);
604
605 /* store hex str (for left side) */
606 snprintf (bytestr, sizeof (bytestr), "%02X ", *p);
607 strncat (hexstr, bytestr, sizeof (hexstr) - strlen (hexstr) - 1);
608
609 /* store char str (for right side) */
610 snprintf (bytestr, sizeof (bytestr), "%c", c);
611 strncat (charstr, bytestr, sizeof (charstr) - strlen (charstr) - 1);
612
613 if ((i % 16) == 0)
614 {
615 /* line completed */
616 zlog_debug ("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr);
617 hexstr[0] = 0;
618 charstr[0] = 0;
619 }
620 else if ((i % 8) == 0)
621 {
622 /* half line: add whitespaces */
623 strncat (hexstr, " ", sizeof (hexstr) - strlen (hexstr) - 1);
624 strncat (charstr, " ", sizeof (charstr) - strlen (charstr) - 1);
625 }
626 p++; /* next byte */
627 }
628
629 /* print rest of buffer if not empty */
630 if (strlen (hexstr) > 0)
631 zlog_debug ("[%8.8s] %-50.50s %s", addrstr, hexstr, charstr);
632 return;
633}