]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_lsp.c
882c857a41f04f01c030f4d08aabd82fccea7cfa
[mirror_frr.git] / isisd / isis_lsp.c
1 /*
2 * IS-IS Rout(e)ing protocol - isis_lsp.c
3 * LSP processing
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 <stdlib.h>
25 #include <stdio.h>
26 #include <zebra.h>
27
28 #include "linklist.h"
29 #include "thread.h"
30 #include "vty.h"
31 #include "stream.h"
32 #include "memory.h"
33 #include "log.h"
34 #include "prefix.h"
35 #include "command.h"
36 #include "hash.h"
37 #include "if.h"
38
39 #include "isisd/dict.h"
40 #include "isisd/isis_constants.h"
41 #include "isisd/isis_common.h"
42 #include "isisd/isis_circuit.h"
43 #include "isisd/isisd.h"
44 #include "isisd/isis_tlv.h"
45 #include "isisd/isis_lsp.h"
46 #include "isisd/isis_pdu.h"
47 #include "isisd/isis_dynhn.h"
48 #include "isisd/isis_misc.h"
49 #include "isisd/isis_flags.h"
50 #include "isisd/iso_checksum.h"
51 #include "isisd/isis_csm.h"
52 #include "isisd/isis_adjacency.h"
53 #include "isisd/isis_spf.h"
54
55 #ifdef TOPOLOGY_GENERATE
56 #include "spgrid.h"
57 #endif
58
59 #define LSP_MEMORY_PREASSIGN
60
61 extern struct isis *isis;
62 extern struct thread_master *master;
63 extern struct in_addr router_id_zebra;
64
65 /* staticly assigned vars for printing purposes */
66 char lsp_bits_string[200]; /* FIXME: enough ? */
67
68 int
69 lsp_id_cmp (u_char * id1, u_char * id2)
70 {
71 return memcmp (id1, id2, ISIS_SYS_ID_LEN + 2);
72 }
73
74 dict_t *
75 lsp_db_init (void)
76 {
77 dict_t *dict;
78
79 dict = dict_create (DICTCOUNT_T_MAX, (dict_comp_t) lsp_id_cmp);
80
81 return dict;
82 }
83
84 struct isis_lsp *
85 lsp_search (u_char * id, dict_t * lspdb)
86 {
87 dnode_t *node;
88
89 #ifdef EXTREME_DEBUG
90 dnode_t *dn;
91
92 zlog_debug ("searching db");
93 for (dn = dict_first (lspdb); dn; dn = dict_next (lspdb, dn))
94 {
95 zlog_debug ("%s\t%pX", rawlspid_print ((char *) dnode_getkey (dn)),
96 dnode_get (dn));
97 }
98 #endif /* EXTREME DEBUG */
99
100 node = dict_lookup (lspdb, id);
101
102 if (node)
103 return (struct isis_lsp *) dnode_get (node);
104
105 return NULL;
106 }
107
108 static void
109 lsp_clear_data (struct isis_lsp *lsp)
110 {
111 if (!lsp)
112 return;
113
114 if (lsp->own_lsp)
115 {
116 if (lsp->tlv_data.nlpids)
117 XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.nlpids);
118 if (lsp->tlv_data.hostname)
119 XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.hostname);
120 }
121 if (lsp->tlv_data.is_neighs)
122 list_delete (lsp->tlv_data.is_neighs);
123 if (lsp->tlv_data.te_is_neighs)
124 list_delete (lsp->tlv_data.te_is_neighs);
125 if (lsp->tlv_data.area_addrs)
126 list_delete (lsp->tlv_data.area_addrs);
127 if (lsp->tlv_data.es_neighs)
128 list_delete (lsp->tlv_data.es_neighs);
129 if (lsp->tlv_data.ipv4_addrs)
130 list_delete (lsp->tlv_data.ipv4_addrs);
131 if (lsp->tlv_data.ipv4_int_reachs)
132 list_delete (lsp->tlv_data.ipv4_int_reachs);
133 if (lsp->tlv_data.ipv4_ext_reachs)
134 list_delete (lsp->tlv_data.ipv4_ext_reachs);
135 if (lsp->tlv_data.te_ipv4_reachs)
136 list_delete (lsp->tlv_data.te_ipv4_reachs);
137 #ifdef HAVE_IPV6
138 if (lsp->tlv_data.ipv6_addrs)
139 list_delete (lsp->tlv_data.ipv6_addrs);
140 if (lsp->tlv_data.ipv6_reachs)
141 list_delete (lsp->tlv_data.ipv6_reachs);
142 #endif /* HAVE_IPV6 */
143
144 memset (&lsp->tlv_data, 0, sizeof (struct tlvs));
145
146 return;
147 }
148
149 static void
150 lsp_destroy (struct isis_lsp *lsp)
151 {
152 if (!lsp)
153 return;
154
155 lsp_clear_data (lsp);
156
157 if (LSP_FRAGMENT (lsp->lsp_header->lsp_id) == 0 && lsp->lspu.frags)
158 {
159 list_delete (lsp->lspu.frags);
160 }
161
162 if (lsp->pdu)
163 stream_free (lsp->pdu);
164 XFREE (MTYPE_ISIS_LSP, lsp);
165 }
166
167 void
168 lsp_db_destroy (dict_t * lspdb)
169 {
170 dnode_t *dnode, *next;
171 struct isis_lsp *lsp;
172
173 dnode = dict_first (lspdb);
174 while (dnode)
175 {
176 next = dict_next (lspdb, dnode);
177 lsp = dnode_get (dnode);
178 lsp_destroy (lsp);
179 dict_delete_free (lspdb, dnode);
180 dnode = next;
181 }
182
183 dict_free (lspdb);
184
185 return;
186 }
187
188 /*
189 * Remove all the frags belonging to the given lsp
190 */
191 static void
192 lsp_remove_frags (struct list *frags, dict_t * lspdb)
193 {
194 dnode_t *dnode;
195 struct listnode *lnode, *lnnode;
196 struct isis_lsp *lsp;
197
198 for (ALL_LIST_ELEMENTS (frags, lnode, lnnode, lsp))
199 {
200 dnode = dict_lookup (lspdb, lsp->lsp_header->lsp_id);
201 lsp_destroy (lsp);
202 dnode_destroy (dict_delete (lspdb, dnode));
203 }
204
205 list_delete_all_node (frags);
206
207 return;
208 }
209
210 void
211 lsp_search_and_destroy (u_char * id, dict_t * lspdb)
212 {
213 dnode_t *node;
214 struct isis_lsp *lsp;
215
216 node = dict_lookup (lspdb, id);
217 if (node)
218 {
219 node = dict_delete (lspdb, node);
220 lsp = dnode_get (node);
221 /*
222 * If this is a zero lsp, remove all the frags now
223 */
224 if (LSP_FRAGMENT (lsp->lsp_header->lsp_id) == 0)
225 {
226 if (lsp->lspu.frags)
227 lsp_remove_frags (lsp->lspu.frags, lspdb);
228 }
229 else
230 {
231 /*
232 * else just remove this frag, from the zero lsps' frag list
233 */
234 if (lsp->lspu.zero_lsp && lsp->lspu.zero_lsp->lspu.frags)
235 listnode_delete (lsp->lspu.zero_lsp->lspu.frags, lsp);
236 }
237 lsp_destroy (lsp);
238 dnode_destroy (node);
239 }
240 }
241
242 /*
243 * Compares a LSP to given values
244 * Params are given in net order
245 */
246 int
247 lsp_compare (char *areatag, struct isis_lsp *lsp, u_int32_t seq_num,
248 u_int16_t checksum, u_int16_t rem_lifetime)
249 {
250 /* no point in double ntohl on seqnum */
251 if (lsp->lsp_header->seq_num == seq_num &&
252 lsp->lsp_header->checksum == checksum &&
253 /*comparing with 0, no need to do ntohl */
254 ((lsp->lsp_header->rem_lifetime == 0 && rem_lifetime == 0) ||
255 (lsp->lsp_header->rem_lifetime != 0 && rem_lifetime != 0)))
256 {
257 if (isis->debugs & DEBUG_SNP_PACKETS)
258 {
259 zlog_debug ("ISIS-Snp (%s): LSP %s seq 0x%08x, cksum 0x%04x,"
260 " lifetime %us",
261 areatag,
262 rawlspid_print (lsp->lsp_header->lsp_id),
263 ntohl (lsp->lsp_header->seq_num),
264 ntohs (lsp->lsp_header->checksum),
265 ntohs (lsp->lsp_header->rem_lifetime));
266 zlog_debug ("ISIS-Snp (%s): is equal to ours seq 0x%08x,"
267 " cksum 0x%04x, lifetime %us",
268 areatag,
269 ntohl (seq_num), ntohs (checksum), ntohs (rem_lifetime));
270 }
271 return LSP_EQUAL;
272 }
273
274 if (ntohl (seq_num) >= ntohl (lsp->lsp_header->seq_num))
275 {
276 if (isis->debugs & DEBUG_SNP_PACKETS)
277 {
278 zlog_debug ("ISIS-Snp (%s): LSP %s seq 0x%08x, cksum 0x%04x,"
279 " lifetime %us",
280 areatag,
281 rawlspid_print (lsp->lsp_header->lsp_id),
282 ntohl (seq_num), ntohs (checksum), ntohs (rem_lifetime));
283 zlog_debug ("ISIS-Snp (%s): is newer than ours seq 0x%08x, "
284 "cksum 0x%04x, lifetime %us",
285 areatag,
286 ntohl (lsp->lsp_header->seq_num),
287 ntohs (lsp->lsp_header->checksum),
288 ntohs (lsp->lsp_header->rem_lifetime));
289 }
290 return LSP_NEWER;
291 }
292 if (isis->debugs & DEBUG_SNP_PACKETS)
293 {
294 zlog_debug
295 ("ISIS-Snp (%s): LSP %s seq 0x%08x, cksum 0x%04x, lifetime %us",
296 areatag, rawlspid_print (lsp->lsp_header->lsp_id), ntohl (seq_num),
297 ntohs (checksum), ntohs (rem_lifetime));
298 zlog_debug ("ISIS-Snp (%s): is older than ours seq 0x%08x,"
299 " cksum 0x%04x, lifetime %us", areatag,
300 ntohl (lsp->lsp_header->seq_num),
301 ntohs (lsp->lsp_header->checksum),
302 ntohs (lsp->lsp_header->rem_lifetime));
303 }
304
305 return LSP_OLDER;
306 }
307
308 void
309 lsp_inc_seqnum (struct isis_lsp *lsp, u_int32_t seq_num)
310 {
311 u_int32_t newseq;
312
313 if (seq_num == 0 || ntohl (lsp->lsp_header->seq_num) > seq_num)
314 newseq = ntohl (lsp->lsp_header->seq_num) + 1;
315 else
316 newseq = seq_num++;
317
318 lsp->lsp_header->seq_num = htonl (newseq);
319 iso_csum_create (STREAM_DATA (lsp->pdu) + 12,
320 ntohs (lsp->lsp_header->pdu_len) - 12, 12);
321
322 return;
323 }
324
325 /*
326 * Genetates checksum for LSP and its frags
327 */
328 static void
329 lsp_seqnum_update (struct isis_lsp *lsp0)
330 {
331 struct isis_lsp *lsp;
332 struct listnode *node, *nnode;
333
334 lsp_inc_seqnum (lsp0, 0);
335
336 if (!lsp0->lspu.frags)
337 return;
338
339 for (ALL_LIST_ELEMENTS (lsp0->lspu.frags, node, nnode, lsp))
340 lsp_inc_seqnum (lsp, 0);
341
342 return;
343 }
344
345 int
346 isis_lsp_authinfo_check (struct stream *stream, struct isis_area *area,
347 int pdulen, struct isis_passwd *passwd)
348 {
349 uint32_t expected = 0, found;
350 struct tlvs tlvs;
351 int retval = 0;
352
353 expected |= TLVFLAG_AUTH_INFO;
354 retval = parse_tlvs (area->area_tag, stream->data +
355 ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
356 pdulen - ISIS_FIXED_HDR_LEN
357 - ISIS_LSP_HDR_LEN, &expected, &found, &tlvs);
358 if (retval || !(found & TLVFLAG_AUTH_INFO))
359 return 1; /* Auth fail (parsing failed or no auth-tlv) */
360
361 return authentication_check (passwd, &tlvs.auth_info);
362 }
363
364 static void
365 lsp_update_data (struct isis_lsp *lsp, struct stream *stream,
366 struct isis_area *area)
367 {
368 uint32_t expected = 0, found;
369 int retval;
370
371 /* copying only the relevant part of our stream */
372 lsp->pdu = stream_dup (stream);
373
374 /* setting pointers to the correct place */
375 lsp->isis_header = (struct isis_fixed_hdr *) (STREAM_DATA (lsp->pdu));
376 lsp->lsp_header = (struct isis_link_state_hdr *) (STREAM_DATA (lsp->pdu) +
377 ISIS_FIXED_HDR_LEN);
378 lsp->age_out = ZERO_AGE_LIFETIME;
379 lsp->installed = time (NULL);
380 /*
381 * Get LSP data i.e. TLVs
382 */
383 expected |= TLVFLAG_AUTH_INFO;
384 expected |= TLVFLAG_AREA_ADDRS;
385 expected |= TLVFLAG_IS_NEIGHS;
386 if ((lsp->lsp_header->lsp_bits & 3) == 3) /* a level 2 LSP */
387 expected |= TLVFLAG_PARTITION_DESIG_LEVEL2_IS;
388 expected |= TLVFLAG_NLPID;
389 if (area->dynhostname)
390 expected |= TLVFLAG_DYN_HOSTNAME;
391 if (area->newmetric)
392 {
393 expected |= TLVFLAG_TE_IS_NEIGHS;
394 expected |= TLVFLAG_TE_IPV4_REACHABILITY;
395 expected |= TLVFLAG_TE_ROUTER_ID;
396 }
397 expected |= TLVFLAG_IPV4_ADDR;
398 expected |= TLVFLAG_IPV4_INT_REACHABILITY;
399 expected |= TLVFLAG_IPV4_EXT_REACHABILITY;
400 #ifdef HAVE_IPV6
401 expected |= TLVFLAG_IPV6_ADDR;
402 expected |= TLVFLAG_IPV6_REACHABILITY;
403 #endif /* HAVE_IPV6 */
404
405 retval = parse_tlvs (area->area_tag, lsp->pdu->data +
406 ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
407 ntohs (lsp->lsp_header->pdu_len) - ISIS_FIXED_HDR_LEN
408 - ISIS_LSP_HDR_LEN, &expected, &found, &lsp->tlv_data);
409
410 if (found & TLVFLAG_DYN_HOSTNAME)
411 {
412 if (area->dynhostname)
413 isis_dynhn_insert (lsp->lsp_header->lsp_id, lsp->tlv_data.hostname,
414 (lsp->lsp_header->lsp_bits & LSPBIT_IST) ==
415 IS_LEVEL_1_AND_2 ? IS_LEVEL_2 :
416 (lsp->lsp_header->lsp_bits & LSPBIT_IST));
417 }
418
419 }
420
421 void
422 lsp_update (struct isis_lsp *lsp, struct isis_link_state_hdr *lsp_hdr,
423 struct stream *stream, struct isis_area *area, int level)
424 {
425 dnode_t *dnode = NULL;
426
427 /* Remove old LSP from LSP database. */
428 dnode = dict_lookup (area->lspdb[level - 1], lsp->lsp_header->lsp_id);
429 if (dnode)
430 dnode_destroy (dict_delete (area->lspdb[level - 1], dnode));
431
432 /* free the old lsp data */
433 XFREE (MTYPE_STREAM_DATA, lsp->pdu);
434 lsp_clear_data (lsp);
435
436 /* rebuild the lsp data */
437 lsp_update_data (lsp, stream, area);
438
439 /* set the new values for lsp header */
440 memcpy (lsp->lsp_header, lsp_hdr, ISIS_LSP_HDR_LEN);
441
442 if (dnode)
443 lsp_insert (lsp, area->lspdb[level - 1]);
444 }
445
446 /* creation of LSP directly from what we received */
447 struct isis_lsp *
448 lsp_new_from_stream_ptr (struct stream *stream,
449 u_int16_t pdu_len, struct isis_lsp *lsp0,
450 struct isis_area *area)
451 {
452 struct isis_lsp *lsp;
453
454 lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
455 lsp_update_data (lsp, stream, area);
456
457 if (lsp0 == NULL)
458 {
459 /*
460 * zero lsp -> create the list for fragments
461 */
462 lsp->lspu.frags = list_new ();
463 }
464 else
465 {
466 /*
467 * a fragment -> set the backpointer and add this to zero lsps frag list
468 */
469 lsp->lspu.zero_lsp = lsp0;
470 listnode_add (lsp0->lspu.frags, lsp);
471 }
472
473 return lsp;
474 }
475
476 struct isis_lsp *
477 lsp_new (u_char * lsp_id, u_int16_t rem_lifetime, u_int32_t seq_num,
478 u_int8_t lsp_bits, u_int16_t checksum, int level)
479 {
480 struct isis_lsp *lsp;
481
482 lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
483 if (!lsp)
484 {
485 /* FIXME: set lspdbol bit */
486 zlog_warn ("lsp_new(): out of memory");
487 return NULL;
488 }
489 #ifdef LSP_MEMORY_PREASSIGN
490 lsp->pdu = stream_new (1514); /*Should be minimal mtu? yup... */
491 #else
492 /* We need to do realloc on TLVs additions */
493 lsp->pdu = malloc (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
494 #endif /* LSP_MEMORY_PREASSIGN */
495 if (LSP_FRAGMENT (lsp_id) == 0)
496 lsp->lspu.frags = list_new ();
497 lsp->isis_header = (struct isis_fixed_hdr *) (STREAM_DATA (lsp->pdu));
498 lsp->lsp_header = (struct isis_link_state_hdr *)
499 (STREAM_DATA (lsp->pdu) + ISIS_FIXED_HDR_LEN);
500
501 /* at first we fill the FIXED HEADER */
502 (level == 1) ? fill_fixed_hdr (lsp->isis_header, L1_LINK_STATE) :
503 fill_fixed_hdr (lsp->isis_header, L2_LINK_STATE);
504
505 /* now for the LSP HEADER */
506 /* Minimal LSP PDU size */
507 lsp->lsp_header->pdu_len = htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
508 memcpy (lsp->lsp_header->lsp_id, lsp_id, ISIS_SYS_ID_LEN + 2);
509 lsp->lsp_header->checksum = checksum; /* Provided in network order */
510 lsp->lsp_header->seq_num = htonl (seq_num);
511 lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
512 lsp->lsp_header->lsp_bits = lsp_bits;
513 lsp->level = level;
514 lsp->age_out = ZERO_AGE_LIFETIME;
515
516 stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
517
518 if (isis->debugs & DEBUG_EVENTS)
519 zlog_debug ("New LSP with ID %s-%02x-%02x seqnum %08x",
520 sysid_print (lsp_id), LSP_PSEUDO_ID (lsp->lsp_header->lsp_id),
521 LSP_FRAGMENT (lsp->lsp_header->lsp_id),
522 ntohl (lsp->lsp_header->seq_num));
523
524 return lsp;
525 }
526
527 void
528 lsp_insert (struct isis_lsp *lsp, dict_t * lspdb)
529 {
530 dict_alloc_insert (lspdb, lsp->lsp_header->lsp_id, lsp);
531 }
532
533 /*
534 * Build a list of LSPs with non-zero ht bounded by start and stop ids
535 */
536 void
537 lsp_build_list_nonzero_ht (u_char * start_id, u_char * stop_id,
538 struct list *list, dict_t * lspdb)
539 {
540 dnode_t *first, *last, *curr;
541
542 first = dict_lower_bound (lspdb, start_id);
543 if (!first)
544 return;
545
546 last = dict_upper_bound (lspdb, stop_id);
547
548 curr = first;
549
550 if (((struct isis_lsp *) (curr->dict_data))->lsp_header->rem_lifetime)
551 listnode_add (list, first->dict_data);
552
553 while (curr)
554 {
555 curr = dict_next (lspdb, curr);
556 if (curr &&
557 ((struct isis_lsp *) (curr->dict_data))->lsp_header->rem_lifetime)
558 listnode_add (list, curr->dict_data);
559 if (curr == last)
560 break;
561 }
562
563 return;
564 }
565
566 /*
567 * Build a list of all LSPs bounded by start and stop ids
568 */
569 void
570 lsp_build_list (u_char * start_id, u_char * stop_id,
571 struct list *list, dict_t * lspdb)
572 {
573 dnode_t *first, *last, *curr;
574
575 first = dict_lower_bound (lspdb, start_id);
576 if (!first)
577 return;
578
579 last = dict_upper_bound (lspdb, stop_id);
580
581 curr = first;
582
583 listnode_add (list, first->dict_data);
584
585 while (curr)
586 {
587 curr = dict_next (lspdb, curr);
588 if (curr)
589 listnode_add (list, curr->dict_data);
590 if (curr == last)
591 break;
592 }
593
594 return;
595 }
596
597 /*
598 * Build a list of LSPs with SSN flag set for the given circuit
599 */
600 void
601 lsp_build_list_ssn (struct isis_circuit *circuit, struct list *list,
602 dict_t * lspdb)
603 {
604 dnode_t *dnode, *next;
605 struct isis_lsp *lsp;
606
607 dnode = dict_first (lspdb);
608 while (dnode != NULL)
609 {
610 next = dict_next (lspdb, dnode);
611 lsp = dnode_get (dnode);
612 if (ISIS_CHECK_FLAG (lsp->SSNflags, circuit))
613 listnode_add (list, lsp);
614 dnode = next;
615 }
616
617 return;
618 }
619
620 static void
621 lsp_set_time (struct isis_lsp *lsp)
622 {
623 assert (lsp);
624
625 if (lsp->lsp_header->rem_lifetime == 0)
626 {
627 if (lsp->age_out != 0)
628 lsp->age_out--;
629 return;
630 }
631
632 /* If we are turning 0 */
633 /* ISO 10589 - 7.3.16.4 first paragraph */
634
635 if (ntohs (lsp->lsp_header->rem_lifetime) == 1)
636 {
637 /* 7.3.16.4 a) set SRM flags on all */
638 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
639 /* 7.3.16.4 b) retain only the header FIXME */
640 /* 7.3.16.4 c) record the time to purge FIXME (other way to do it) */
641 }
642
643 lsp->lsp_header->rem_lifetime =
644 htons (ntohs (lsp->lsp_header->rem_lifetime) - 1);
645 }
646
647 static void
648 lspid_print (u_char * lsp_id, u_char * trg, char dynhost, char frag)
649 {
650 struct isis_dynhn *dyn = NULL;
651 u_char id[SYSID_STRLEN];
652
653 if (dynhost)
654 dyn = dynhn_find_by_id (lsp_id);
655 else
656 dyn = NULL;
657
658 if (dyn)
659 sprintf ((char *)id, "%.14s", dyn->name.name);
660 else if (!memcmp (isis->sysid, lsp_id, ISIS_SYS_ID_LEN) & dynhost)
661 sprintf ((char *)id, "%.14s", unix_hostname ());
662 else
663 {
664 memcpy (id, sysid_print (lsp_id), 15);
665 }
666 if (frag)
667 sprintf ((char *)trg, "%s.%02x-%02x", id, LSP_PSEUDO_ID (lsp_id),
668 LSP_FRAGMENT (lsp_id));
669 else
670 sprintf ((char *)trg, "%s.%02x", id, LSP_PSEUDO_ID (lsp_id));
671 }
672
673 /* Convert the lsp attribute bits to attribute string */
674 const char *
675 lsp_bits2string (u_char * lsp_bits)
676 {
677 char *pos = lsp_bits_string;
678
679 if (!*lsp_bits)
680 return " none";
681
682 /* we only focus on the default metric */
683 pos += sprintf (pos, "%d/",
684 ISIS_MASK_LSP_ATT_DEFAULT_BIT (*lsp_bits) ? 1 : 0);
685
686 pos += sprintf (pos, "%d/",
687 ISIS_MASK_LSP_PARTITION_BIT (*lsp_bits) ? 1 : 0);
688
689 pos += sprintf (pos, "%d", ISIS_MASK_LSP_OL_BIT (*lsp_bits) ? 1 : 0);
690
691 *(pos) = '\0';
692
693 return lsp_bits_string;
694 }
695
696 /* this function prints the lsp on show isis database */
697 static void
698 lsp_print (dnode_t * node, struct vty *vty, char dynhost)
699 {
700 struct isis_lsp *lsp = dnode_get (node);
701 u_char LSPid[255];
702
703 lspid_print (lsp->lsp_header->lsp_id, LSPid, dynhost, 1);
704 vty_out (vty, "%-21s%c ", LSPid, lsp->own_lsp ? '*' : ' ');
705 vty_out (vty, "0x%08x ", ntohl (lsp->lsp_header->seq_num));
706 vty_out (vty, "0x%04x ", ntohs (lsp->lsp_header->checksum));
707
708 if (ntohs (lsp->lsp_header->rem_lifetime) == 0)
709 vty_out (vty, " (%2u)", lsp->age_out);
710 else
711 vty_out (vty, "%5u", ntohs (lsp->lsp_header->rem_lifetime));
712
713 vty_out (vty, " %s%s",
714 lsp_bits2string (&lsp->lsp_header->lsp_bits), VTY_NEWLINE);
715 }
716
717 static void
718 lsp_print_detail (dnode_t * node, struct vty *vty, char dynhost)
719 {
720 struct isis_lsp *lsp = dnode_get (node);
721 struct area_addr *area_addr;
722 int i;
723 struct listnode *lnode, *lnnode;
724 struct is_neigh *is_neigh;
725 struct te_is_neigh *te_is_neigh;
726 struct ipv4_reachability *ipv4_reach;
727 struct in_addr *ipv4_addr;
728 struct te_ipv4_reachability *te_ipv4_reach;
729 #ifdef HAVE_IPV6
730 struct ipv6_reachability *ipv6_reach;
731 struct in6_addr in6;
732 #endif
733 u_char LSPid[255];
734 u_char hostname[255];
735 u_char buff[BUFSIZ];
736 u_char ipv4_reach_prefix[20];
737 u_char ipv4_reach_mask[20];
738 u_char ipv4_address[20];
739
740 lspid_print (lsp->lsp_header->lsp_id, LSPid, dynhost, 1);
741 lsp_print (node, vty, dynhost);
742
743 /* for all area address */
744 if (lsp->tlv_data.area_addrs)
745 for (ALL_LIST_ELEMENTS (lsp->tlv_data.area_addrs, lnode,
746 lnnode, area_addr))
747 {
748 vty_out (vty, " Area Address: %s%s",
749 isonet_print (area_addr->area_addr, area_addr->addr_len),
750 VTY_NEWLINE);
751 }
752
753 /* for the nlpid tlv */
754 if (lsp->tlv_data.nlpids)
755 {
756 for (i = 0; i < lsp->tlv_data.nlpids->count; i++)
757 {
758 switch (lsp->tlv_data.nlpids->nlpids[i])
759 {
760 case NLPID_IP:
761 case NLPID_IPV6:
762 vty_out (vty, " NLPID: 0x%X%s",
763 lsp->tlv_data.nlpids->nlpids[i], VTY_NEWLINE);
764 break;
765 default:
766 vty_out (vty, " NLPID: %s%s", "unknown", VTY_NEWLINE);
767 break;
768 }
769 }
770 }
771
772 /* for the hostname tlv */
773 if (lsp->tlv_data.hostname)
774 {
775 bzero (hostname, sizeof (hostname));
776 memcpy (hostname, lsp->tlv_data.hostname->name,
777 lsp->tlv_data.hostname->namelen);
778 vty_out (vty, " Hostname: %s%s", hostname, VTY_NEWLINE);
779 }
780
781 if (lsp->tlv_data.ipv4_addrs)
782 for (ALL_LIST_ELEMENTS (lsp->tlv_data.ipv4_addrs, lnode,
783 lnnode, ipv4_addr))
784 {
785 memcpy (ipv4_address, inet_ntoa (*ipv4_addr), sizeof (ipv4_address));
786 vty_out (vty, " IP: %s%s", ipv4_address, VTY_NEWLINE);
787 }
788
789 /* TE router id */
790 if (lsp->tlv_data.router_id)
791 {
792 memcpy (ipv4_address, inet_ntoa (lsp->tlv_data.router_id->id),
793 sizeof (ipv4_address));
794 vty_out (vty, " Router ID: %s%s", ipv4_address, VTY_NEWLINE);
795 }
796
797 /* for the IS neighbor tlv */
798 if (lsp->tlv_data.is_neighs)
799 for (ALL_LIST_ELEMENTS (lsp->tlv_data.is_neighs, lnode, lnnode, is_neigh))
800 {
801 lspid_print (is_neigh->neigh_id, LSPid, dynhost, 0);
802 vty_out (vty, " Metric: %-10d IS %s%s",
803 is_neigh->metrics.metric_default, LSPid, VTY_NEWLINE);
804 }
805
806 /* for the internal reachable tlv */
807 if (lsp->tlv_data.ipv4_int_reachs)
808 for (ALL_LIST_ELEMENTS (lsp->tlv_data.ipv4_int_reachs, lnode,
809 lnnode, ipv4_reach))
810 {
811 memcpy (ipv4_reach_prefix, inet_ntoa (ipv4_reach->prefix),
812 sizeof (ipv4_reach_prefix));
813 memcpy (ipv4_reach_mask, inet_ntoa (ipv4_reach->mask),
814 sizeof (ipv4_reach_mask));
815 vty_out (vty, " Metric: %-10d IP-Internal %s %s%s",
816 ipv4_reach->metrics.metric_default, ipv4_reach_prefix,
817 ipv4_reach_mask, VTY_NEWLINE);
818 }
819
820 /* for the external reachable tlv */
821 if (lsp->tlv_data.ipv4_ext_reachs)
822 for (ALL_LIST_ELEMENTS (lsp->tlv_data.ipv4_ext_reachs, lnode,
823 lnnode, ipv4_reach))
824 {
825 memcpy (ipv4_reach_prefix, inet_ntoa (ipv4_reach->prefix),
826 sizeof (ipv4_reach_prefix));
827 memcpy (ipv4_reach_mask, inet_ntoa (ipv4_reach->mask),
828 sizeof (ipv4_reach_mask));
829 vty_out (vty, " Metric: %-10d IP-External %s %s%s",
830 ipv4_reach->metrics.metric_default, ipv4_reach_prefix,
831 ipv4_reach_mask, VTY_NEWLINE);
832 }
833
834 /* IPv6 tlv */
835 #ifdef HAVE_IPV6
836 if (lsp->tlv_data.ipv6_reachs)
837 for (ALL_LIST_ELEMENTS (lsp->tlv_data.ipv6_reachs, lnode,
838 lnnode, ipv6_reach))
839 {
840 memset (&in6, 0, sizeof (in6));
841 memcpy (in6.s6_addr, ipv6_reach->prefix,
842 PSIZE (ipv6_reach->prefix_len));
843 inet_ntop (AF_INET6, &in6, (char *)buff, BUFSIZ);
844 if ((ipv6_reach->control_info &&
845 CTRL_INFO_DISTRIBUTION) == DISTRIBUTION_INTERNAL)
846 vty_out (vty, " Metric: %-10d IPv6-Internal %s/%d%s",
847 ntohl (ipv6_reach->metric),
848 buff, ipv6_reach->prefix_len, VTY_NEWLINE);
849 else
850 vty_out (vty, " Metric: %-10d IPv6-External %s/%d%s",
851 ntohl (ipv6_reach->metric),
852 buff, ipv6_reach->prefix_len, VTY_NEWLINE);
853 }
854 #endif
855
856 /* TE IS neighbor tlv */
857 if (lsp->tlv_data.te_is_neighs)
858 for (ALL_LIST_ELEMENTS (lsp->tlv_data.te_is_neighs, lnode,
859 lnnode, te_is_neigh))
860 {
861 uint32_t metric;
862 memcpy (&metric, te_is_neigh->te_metric, 3);
863 lspid_print (te_is_neigh->neigh_id, LSPid, dynhost, 0);
864 vty_out (vty, " Metric: %-10d IS-Extended %s%s",
865 ntohl (metric << 8), LSPid, VTY_NEWLINE);
866 }
867
868 /* TE IPv4 tlv */
869 if (lsp->tlv_data.te_ipv4_reachs)
870 for (ALL_LIST_ELEMENTS (lsp->tlv_data.te_ipv4_reachs, lnode,
871 lnnode, te_ipv4_reach))
872 {
873 /* FIXME: There should be better way to output this stuff. */
874 vty_out (vty, " Metric: %-10d IP-Extended %s/%d%s",
875 ntohl (te_ipv4_reach->te_metric),
876 inet_ntoa (newprefix2inaddr (&te_ipv4_reach->prefix_start,
877 te_ipv4_reach->control)),
878 te_ipv4_reach->control & 0x3F, VTY_NEWLINE);
879 }
880
881 return;
882 }
883
884 /* print all the lsps info in the local lspdb */
885 int
886 lsp_print_all (struct vty *vty, dict_t * lspdb, char detail, char dynhost)
887 {
888
889 dnode_t *node = dict_first (lspdb), *next;
890 int lsp_count = 0;
891
892 /* print the title, for both modes */
893 vty_out (vty, "LSP ID LSP Seq Num LSP Checksum "
894 "LSP Holdtime ATT/P/OL%s", VTY_NEWLINE);
895
896 if (detail == ISIS_UI_LEVEL_BRIEF)
897 {
898 while (node != NULL)
899 {
900 /* I think it is unnecessary, so I comment it out */
901 /* dict_contains (lspdb, node); */
902 next = dict_next (lspdb, node);
903 lsp_print (node, vty, dynhost);
904 node = next;
905 lsp_count++;
906 }
907 }
908 else if (detail == ISIS_UI_LEVEL_DETAIL)
909 {
910 while (node != NULL)
911 {
912 next = dict_next (lspdb, node);
913 lsp_print_detail (node, vty, dynhost);
914 node = next;
915 lsp_count++;
916 }
917 }
918
919 return lsp_count;
920 }
921
922 #define FRAG_THOLD(S,T) \
923 ((STREAM_SIZE(S)*T)/100)
924
925 /* stream*, area->lsp_frag_threshold, increment */
926 #define FRAG_NEEDED(S,T,I) \
927 (STREAM_SIZE(S)-STREAM_REMAIN(S)+(I) > FRAG_THOLD(S,T))
928
929 /* FIXME: It shouldn't be necessary to pass tlvsize here, TLVs can have
930 * variable length (TE TLVs, sub TLVs). */
931 static void
932 lsp_tlv_fit (struct isis_lsp *lsp, struct list **from, struct list **to,
933 int tlvsize, int frag_thold,
934 int tlv_build_func (struct list *, struct stream *))
935 {
936 int count, i;
937
938 /* can we fit all ? */
939 if (!FRAG_NEEDED (lsp->pdu, frag_thold, listcount (*from) * tlvsize + 2))
940 {
941 tlv_build_func (*from, lsp->pdu);
942 *to = *from;
943 *from = NULL;
944 }
945 else if (!FRAG_NEEDED (lsp->pdu, frag_thold, tlvsize + 2))
946 {
947 /* fit all we can */
948 count = FRAG_THOLD (lsp->pdu, frag_thold) - 2 -
949 (STREAM_SIZE (lsp->pdu) - STREAM_REMAIN (lsp->pdu));
950 if (count)
951 count = count / tlvsize;
952 for (i = 0; i < count; i++)
953 {
954 listnode_add (*to, listgetdata (listhead (*from)));
955 listnode_delete (*from, listgetdata (listhead (*from)));
956 }
957 tlv_build_func (*to, lsp->pdu);
958 }
959 lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
960 return;
961 }
962
963 static struct isis_lsp *
964 lsp_next_frag (u_char frag_num, struct isis_lsp *lsp0, struct isis_area *area,
965 int level)
966 {
967 struct isis_lsp *lsp;
968 u_char frag_id[ISIS_SYS_ID_LEN + 2];
969
970 memcpy (frag_id, lsp0->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 1);
971 LSP_FRAGMENT (frag_id) = frag_num;
972 lsp = lsp_search (frag_id, area->lspdb[level - 1]);
973 if (lsp)
974 {
975 /*
976 * Clear the TLVs, but inherit the authinfo
977 */
978 lsp_clear_data (lsp);
979 if (lsp0->tlv_data.auth_info.type)
980 {
981 memcpy (&lsp->tlv_data.auth_info, &lsp->tlv_data.auth_info,
982 sizeof (struct isis_passwd));
983 tlv_add_authinfo (lsp->tlv_data.auth_info.type,
984 lsp->tlv_data.auth_info.len,
985 lsp->tlv_data.auth_info.passwd, lsp->pdu);
986 }
987 return lsp;
988 }
989 lsp = lsp_new (frag_id, area->max_lsp_lifetime[level - 1], 0, area->is_type,
990 0, level);
991 lsp->own_lsp = 1;
992 lsp_insert (lsp, area->lspdb[level - 1]);
993 listnode_add (lsp0->lspu.frags, lsp);
994 lsp->lspu.zero_lsp = lsp0;
995 /*
996 * Copy the authinfo from zero LSP
997 */
998 if (lsp0->tlv_data.auth_info.type)
999 {
1000 memcpy (&lsp->tlv_data.auth_info, &lsp->tlv_data.auth_info,
1001 sizeof (struct isis_passwd));
1002 tlv_add_authinfo (lsp->tlv_data.auth_info.type,
1003 lsp->tlv_data.auth_info.len,
1004 lsp->tlv_data.auth_info.passwd, lsp->pdu);
1005 }
1006 return lsp;
1007 }
1008
1009 /*
1010 * Builds the LSP data part. This func creates a new frag whenever
1011 * area->lsp_frag_threshold is exceeded.
1012 */
1013 static void
1014 lsp_build_nonpseudo (struct isis_lsp *lsp, struct isis_area *area)
1015 {
1016 struct is_neigh *is_neigh;
1017 struct te_is_neigh *te_is_neigh;
1018 struct listnode *node, *nnode, *ipnode, *ipnnode;
1019 int level = lsp->level;
1020 struct isis_circuit *circuit;
1021 struct prefix_ipv4 *ipv4;
1022 struct ipv4_reachability *ipreach;
1023 struct te_ipv4_reachability *te_ipreach;
1024 struct isis_adjacency *nei;
1025 #ifdef HAVE_IPV6
1026 struct prefix_ipv6 *ipv6, *ip6prefix;
1027 struct ipv6_reachability *ip6reach;
1028 #endif /* HAVE_IPV6 */
1029 struct tlvs tlv_data;
1030 struct isis_lsp *lsp0 = lsp;
1031 struct isis_passwd *passwd;
1032 struct in_addr *routerid;
1033
1034 /*
1035 * First add the tlvs related to area
1036 */
1037
1038 /* Area addresses */
1039 if (lsp->tlv_data.area_addrs == NULL)
1040 lsp->tlv_data.area_addrs = list_new ();
1041 list_add_list (lsp->tlv_data.area_addrs, area->area_addrs);
1042 /* Protocols Supported */
1043 if (area->ip_circuits > 0
1044 #ifdef HAVE_IPV6
1045 || area->ipv6_circuits > 0
1046 #endif /* HAVE_IPV6 */
1047 )
1048 {
1049 lsp->tlv_data.nlpids = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct nlpids));
1050 lsp->tlv_data.nlpids->count = 0;
1051 if (area->ip_circuits > 0)
1052 {
1053 lsp->tlv_data.nlpids->count++;
1054 lsp->tlv_data.nlpids->nlpids[0] = NLPID_IP;
1055 }
1056 #ifdef HAVE_IPV6
1057 if (area->ipv6_circuits > 0)
1058 {
1059 lsp->tlv_data.nlpids->count++;
1060 lsp->tlv_data.nlpids->nlpids[lsp->tlv_data.nlpids->count - 1] =
1061 NLPID_IPV6;
1062 }
1063 #endif /* HAVE_IPV6 */
1064 }
1065 /* Dynamic Hostname */
1066 if (area->dynhostname)
1067 {
1068 lsp->tlv_data.hostname = XMALLOC (MTYPE_ISIS_TLV,
1069 sizeof (struct hostname));
1070
1071 memcpy (lsp->tlv_data.hostname->name, unix_hostname (),
1072 strlen (unix_hostname ()));
1073 lsp->tlv_data.hostname->namelen = strlen (unix_hostname ());
1074 }
1075
1076 /*
1077 * Building the zero lsp
1078 */
1079
1080 /* Reset stream endp. Stream is always there and on every LSP refresh only
1081 * TLV part of it is overwritten. So we must seek past header we will not
1082 * touch. */
1083 stream_reset (lsp->pdu);
1084 stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
1085
1086 /*
1087 * Add the authentication info if its present
1088 */
1089 (level == 1) ? (passwd = &area->area_passwd) :
1090 (passwd = &area->domain_passwd);
1091 if (passwd->type)
1092 {
1093 memcpy (&lsp->tlv_data.auth_info, passwd, sizeof (struct isis_passwd));
1094 tlv_add_authinfo (passwd->type, passwd->len, passwd->passwd, lsp->pdu);
1095 }
1096 if (lsp->tlv_data.nlpids)
1097 tlv_add_nlpid (lsp->tlv_data.nlpids, lsp->pdu);
1098 if (lsp->tlv_data.hostname)
1099 tlv_add_dynamic_hostname (lsp->tlv_data.hostname, lsp->pdu);
1100 if (lsp->tlv_data.area_addrs && listcount (lsp->tlv_data.area_addrs) > 0)
1101 tlv_add_area_addrs (lsp->tlv_data.area_addrs, lsp->pdu);
1102
1103 memset (&tlv_data, 0, sizeof (struct tlvs));
1104 /*
1105 * IPv4 address TLV. We don't follow "C" vendor, but "J" vendor behavior -
1106 * one IPv4 address is put into LSP and this address is same as router id.
1107 */
1108 if (router_id_zebra.s_addr != 0)
1109 {
1110 if (lsp->tlv_data.ipv4_addrs == NULL)
1111 {
1112 lsp->tlv_data.ipv4_addrs = list_new ();
1113 lsp->tlv_data.ipv4_addrs->del = free_tlv;
1114 }
1115
1116 routerid = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct in_addr));
1117 routerid->s_addr = router_id_zebra.s_addr;
1118
1119 listnode_add (lsp->tlv_data.ipv4_addrs, routerid);
1120
1121 /*
1122 * FIXME: Using add_tlv() directly is hack, but tlv_add_ip_addrs()
1123 * expects list of prefix_ipv4 structures, but we have list of
1124 * in_addr structures.
1125 */
1126 add_tlv (IPV4_ADDR, IPV4_MAX_BYTELEN, (u_char *) &routerid->s_addr,
1127 lsp->pdu);
1128 }
1129
1130 #ifdef TOPOLOGY_GENERATE
1131 /* If topology exists (and we create topology for level 1 only), create
1132 * (hardcoded) link to topology. */
1133 if (area->topology && level == 1)
1134 {
1135 if (tlv_data.is_neighs == NULL)
1136 {
1137 tlv_data.is_neighs = list_new ();
1138 tlv_data.is_neighs->del = free_tlv;
1139 }
1140 is_neigh = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
1141 memset (is_neigh, 0, sizeof (struct is_neigh));
1142
1143 memcpy (&is_neigh->neigh_id, area->topology_baseis, ISIS_SYS_ID_LEN);
1144 is_neigh->neigh_id[ISIS_SYS_ID_LEN - 1] = (1 & 0xFF);
1145 is_neigh->neigh_id[ISIS_SYS_ID_LEN - 2] = ((1 >> 8) & 0xFF);
1146 is_neigh->metrics.metric_default = 0x01;
1147 is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
1148 is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
1149 is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
1150 listnode_add (tlv_data.is_neighs, is_neigh);
1151 }
1152 #endif /* TOPOLOGY_GENERATE */
1153
1154 /*
1155 * Then build lists of tlvs related to circuits
1156 */
1157 for (ALL_LIST_ELEMENTS (area->circuit_list, node, nnode, circuit))
1158 {
1159 if (circuit->state != C_STATE_UP)
1160 continue;
1161
1162 /*
1163 * Add IPv4 internal reachability of this circuit
1164 */
1165 if (circuit->ip_router && circuit->ip_addrs &&
1166 circuit->ip_addrs->count > 0)
1167 {
1168 if (area->oldmetric)
1169 {
1170 if (tlv_data.ipv4_int_reachs == NULL)
1171 {
1172 tlv_data.ipv4_int_reachs = list_new ();
1173 tlv_data.ipv4_int_reachs->del = free_tlv;
1174 }
1175 for (ALL_LIST_ELEMENTS (circuit->ip_addrs, ipnode, ipnnode, ipv4))
1176 {
1177 ipreach =
1178 XMALLOC (MTYPE_ISIS_TLV, sizeof (struct ipv4_reachability));
1179 ipreach->metrics = circuit->metrics[level - 1];
1180 masklen2ip (ipv4->prefixlen, &ipreach->mask);
1181 ipreach->prefix.s_addr = ((ipreach->mask.s_addr) &
1182 (ipv4->prefix.s_addr));
1183 listnode_add (tlv_data.ipv4_int_reachs, ipreach);
1184 }
1185 tlv_data.ipv4_int_reachs->del = free_tlv;
1186 }
1187 if (area->newmetric)
1188 {
1189 if (tlv_data.te_ipv4_reachs == NULL)
1190 {
1191 tlv_data.te_ipv4_reachs = list_new ();
1192 tlv_data.te_ipv4_reachs->del = free_tlv;
1193 }
1194 for (ALL_LIST_ELEMENTS (circuit->ip_addrs, ipnode, ipnnode, ipv4))
1195 {
1196 /* FIXME All this assumes that we have no sub TLVs. */
1197 te_ipreach = XCALLOC (MTYPE_ISIS_TLV,
1198 sizeof (struct te_ipv4_reachability) +
1199 ((ipv4->prefixlen + 7)/8) - 1);
1200 te_ipreach->te_metric = htonl (*circuit->te_metric);
1201 te_ipreach->control = (ipv4->prefixlen & 0x3F);
1202 memcpy (&te_ipreach->prefix_start, &ipv4->prefix.s_addr,
1203 (ipv4->prefixlen + 7)/8);
1204 listnode_add (tlv_data.te_ipv4_reachs, te_ipreach);
1205 }
1206 }
1207 }
1208 #ifdef HAVE_IPV6
1209 /*
1210 * Add IPv6 reachability of this circuit
1211 */
1212 if (circuit->ipv6_router && circuit->ipv6_non_link &&
1213 circuit->ipv6_non_link->count > 0)
1214 {
1215
1216 if (tlv_data.ipv6_reachs == NULL)
1217 {
1218 tlv_data.ipv6_reachs = list_new ();
1219 tlv_data.ipv6_reachs->del = free_tlv;
1220 }
1221 for (ALL_LIST_ELEMENTS (circuit->ipv6_non_link, ipnode, ipnnode,
1222 ipv6))
1223 {
1224 ip6reach =
1225 XCALLOC (MTYPE_ISIS_TLV, sizeof (struct ipv6_reachability));
1226 ip6reach->metric =
1227 htonl (circuit->metrics[level - 1].metric_default);
1228 ip6reach->control_info = 0;
1229 ip6reach->prefix_len = ipv6->prefixlen;
1230 memcpy (&ip6prefix, &ipv6, sizeof(ip6prefix));
1231 apply_mask_ipv6 (ip6prefix);
1232 memcpy (ip6reach->prefix, ip6prefix->prefix.s6_addr,
1233 sizeof (ip6reach->prefix));
1234 listnode_add (tlv_data.ipv6_reachs, ip6reach);
1235 }
1236 }
1237 #endif /* HAVE_IPV6 */
1238
1239 switch (circuit->circ_type)
1240 {
1241 case CIRCUIT_T_BROADCAST:
1242 if (level & circuit->circuit_is_type)
1243 {
1244 if (area->oldmetric)
1245 {
1246 if (tlv_data.is_neighs == NULL)
1247 {
1248 tlv_data.is_neighs = list_new ();
1249 tlv_data.is_neighs->del = free_tlv;
1250 }
1251 is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
1252 if (level == 1)
1253 memcpy (is_neigh->neigh_id,
1254 circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
1255 else
1256 memcpy (is_neigh->neigh_id,
1257 circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
1258 is_neigh->metrics = circuit->metrics[level - 1];
1259 listnode_add (tlv_data.is_neighs, is_neigh);
1260 tlv_data.is_neighs->del = free_tlv;
1261 }
1262 if (area->newmetric)
1263 {
1264 uint32_t metric;
1265
1266 if (tlv_data.te_is_neighs == NULL)
1267 {
1268 tlv_data.te_is_neighs = list_new ();
1269 tlv_data.te_is_neighs->del = free_tlv;
1270 }
1271 te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
1272 sizeof (struct te_is_neigh));
1273 if (level == 1)
1274 memcpy (te_is_neigh->neigh_id,
1275 circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
1276 else
1277 memcpy (te_is_neigh->neigh_id,
1278 circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
1279 metric = ((htonl(*circuit->te_metric) >> 8) & 0xffffff);
1280 memcpy (te_is_neigh->te_metric, &metric, 3);
1281 listnode_add (tlv_data.te_is_neighs, te_is_neigh);
1282 }
1283 }
1284 break;
1285 case CIRCUIT_T_P2P:
1286 nei = circuit->u.p2p.neighbor;
1287 if (nei && (level & nei->circuit_t))
1288 {
1289 if (area->oldmetric)
1290 {
1291 if (tlv_data.is_neighs == NULL)
1292 {
1293 tlv_data.is_neighs = list_new ();
1294 tlv_data.is_neighs->del = free_tlv;
1295 }
1296 is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
1297 memcpy (is_neigh->neigh_id, nei->sysid, ISIS_SYS_ID_LEN);
1298 is_neigh->metrics = circuit->metrics[level - 1];
1299 listnode_add (tlv_data.is_neighs, is_neigh);
1300 }
1301 if (area->newmetric)
1302 {
1303 uint32_t metric;
1304
1305 if (tlv_data.te_is_neighs == NULL)
1306 {
1307 tlv_data.te_is_neighs = list_new ();
1308 tlv_data.te_is_neighs->del = free_tlv;
1309 }
1310 te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
1311 sizeof (struct te_is_neigh));
1312 memcpy (te_is_neigh->neigh_id, nei->sysid, ISIS_SYS_ID_LEN);
1313 metric = ((htonl(*circuit->te_metric) >> 8) & 0xffffff);
1314 memcpy (te_is_neigh->te_metric, &metric, 3);
1315 listnode_add (tlv_data.te_is_neighs, te_is_neigh);
1316 }
1317 }
1318 break;
1319 case CIRCUIT_T_STATIC_IN:
1320 zlog_warn ("lsp_area_create: unsupported circuit type");
1321 break;
1322 case CIRCUIT_T_STATIC_OUT:
1323 zlog_warn ("lsp_area_create: unsupported circuit type");
1324 break;
1325 case CIRCUIT_T_DA:
1326 zlog_warn ("lsp_area_create: unsupported circuit type");
1327 break;
1328 default:
1329 zlog_warn ("lsp_area_create: unknown circuit type");
1330 }
1331 }
1332
1333 while (tlv_data.ipv4_int_reachs && listcount (tlv_data.ipv4_int_reachs))
1334 {
1335 if (lsp->tlv_data.ipv4_int_reachs == NULL)
1336 lsp->tlv_data.ipv4_int_reachs = list_new ();
1337 lsp_tlv_fit (lsp, &tlv_data.ipv4_int_reachs,
1338 &lsp->tlv_data.ipv4_int_reachs,
1339 IPV4_REACH_LEN, area->lsp_frag_threshold,
1340 tlv_add_ipv4_reachs);
1341 if (tlv_data.ipv4_int_reachs && listcount (tlv_data.ipv4_int_reachs))
1342 lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
1343 lsp0, area, level);
1344 }
1345 /* FIXME: We pass maximum te_ipv4_reachability length to the lsp_tlv_fit()
1346 * for now. lsp_tlv_fit() needs to be fixed to deal with variable length
1347 * TLVs (sub TLVs!). */
1348 while (tlv_data.te_ipv4_reachs && listcount (tlv_data.te_ipv4_reachs))
1349 {
1350 if (lsp->tlv_data.te_ipv4_reachs == NULL)
1351 lsp->tlv_data.te_ipv4_reachs = list_new ();
1352 lsp_tlv_fit (lsp, &tlv_data.te_ipv4_reachs,
1353 &lsp->tlv_data.te_ipv4_reachs,
1354 9, area->lsp_frag_threshold, tlv_add_te_ipv4_reachs);
1355 if (tlv_data.te_ipv4_reachs && listcount (tlv_data.te_ipv4_reachs))
1356 lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
1357 lsp0, area, level);
1358 }
1359
1360 #ifdef HAVE_IPV6
1361 while (tlv_data.ipv6_reachs && listcount (tlv_data.ipv6_reachs))
1362 {
1363 if (lsp->tlv_data.ipv6_reachs == NULL)
1364 lsp->tlv_data.ipv6_reachs = list_new ();
1365 lsp_tlv_fit (lsp, &tlv_data.ipv6_reachs,
1366 &lsp->tlv_data.ipv6_reachs,
1367 IPV6_REACH_LEN, area->lsp_frag_threshold,
1368 tlv_add_ipv6_reachs);
1369 if (tlv_data.ipv6_reachs && listcount (tlv_data.ipv6_reachs))
1370 lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
1371 lsp0, area, level);
1372 }
1373 #endif /* HAVE_IPV6 */
1374
1375 while (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
1376 {
1377 if (lsp->tlv_data.is_neighs == NULL)
1378 lsp->tlv_data.is_neighs = list_new ();
1379 lsp_tlv_fit (lsp, &tlv_data.is_neighs,
1380 &lsp->tlv_data.is_neighs,
1381 IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
1382 tlv_add_is_neighs);
1383 if (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
1384 lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
1385 lsp0, area, level);
1386 }
1387
1388 while (tlv_data.te_is_neighs && listcount (tlv_data.te_is_neighs))
1389 {
1390 if (lsp->tlv_data.te_is_neighs == NULL)
1391 lsp->tlv_data.te_is_neighs = list_new ();
1392 lsp_tlv_fit (lsp, &tlv_data.te_is_neighs, &lsp->tlv_data.te_is_neighs,
1393 IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
1394 tlv_add_te_is_neighs);
1395 if (tlv_data.te_is_neighs && listcount (tlv_data.te_is_neighs))
1396 lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
1397 lsp0, area, level);
1398 }
1399
1400 free_tlvs (&tlv_data);
1401 return;
1402 }
1403
1404 /*
1405 * 7.3.7 Generation on non-pseudonode LSPs
1406 */
1407 static int
1408 lsp_generate_non_pseudo (struct isis_area *area, int level)
1409 {
1410 struct isis_lsp *oldlsp, *newlsp;
1411 u_int32_t seq_num = 0;
1412 u_char lspid[ISIS_SYS_ID_LEN + 2];
1413
1414 memset (&lspid, 0, ISIS_SYS_ID_LEN + 2);
1415 memcpy (&lspid, isis->sysid, ISIS_SYS_ID_LEN);
1416
1417 /* only builds the lsp if the area shares the level */
1418 if ((area->is_type & level) == level)
1419 {
1420 oldlsp = lsp_search (lspid, area->lspdb[level - 1]);
1421 if (oldlsp)
1422 {
1423 seq_num = ntohl (oldlsp->lsp_header->seq_num);
1424 lsp_search_and_destroy (oldlsp->lsp_header->lsp_id,
1425 area->lspdb[level - 1]);
1426 /* FIXME: we should actually initiate a purge */
1427 }
1428 newlsp = lsp_new (lspid, area->max_lsp_lifetime[level - 1], seq_num,
1429 area->is_type, 0, level);
1430 newlsp->own_lsp = 1;
1431
1432 lsp_insert (newlsp, area->lspdb[level - 1]);
1433 /* build_lsp_data (newlsp, area); */
1434 lsp_build_nonpseudo (newlsp, area);
1435 /* time to calculate our checksum */
1436 lsp_seqnum_update (newlsp);
1437 }
1438
1439 /* DEBUG_ADJ_PACKETS */
1440 if (isis->debugs & DEBUG_ADJ_PACKETS)
1441 {
1442 /* FIXME: is this place right? fix missing info */
1443 zlog_debug ("ISIS-Upd (%s): Building L%d LSP", area->area_tag, level);
1444 }
1445
1446 return ISIS_OK;
1447 }
1448
1449 /*
1450 * 7.3.9 Generation of level 1 LSPs (non-pseudonode)
1451 */
1452 int
1453 lsp_l1_generate (struct isis_area *area)
1454 {
1455 THREAD_TIMER_ON (master, area->t_lsp_refresh[0], lsp_refresh_l1, area,
1456 MAX_LSP_GEN_INTERVAL);
1457
1458 return lsp_generate_non_pseudo (area, 1);
1459 }
1460
1461 /*
1462 * 7.3.9 Generation of level 2 LSPs (non-pseudonode)
1463 */
1464 int
1465 lsp_l2_generate (struct isis_area *area)
1466 {
1467 THREAD_TIMER_ON (master, area->t_lsp_refresh[1], lsp_refresh_l2, area,
1468 MAX_LSP_GEN_INTERVAL);
1469
1470 return lsp_generate_non_pseudo (area, 2);
1471 }
1472
1473 static int
1474 lsp_non_pseudo_regenerate (struct isis_area *area, int level)
1475 {
1476 dict_t *lspdb = area->lspdb[level - 1];
1477 struct isis_lsp *lsp, *frag;
1478 struct listnode *node;
1479 u_char lspid[ISIS_SYS_ID_LEN + 2];
1480
1481 memset (lspid, 0, ISIS_SYS_ID_LEN + 2);
1482 memcpy (lspid, isis->sysid, ISIS_SYS_ID_LEN);
1483
1484 lsp = lsp_search (lspid, lspdb);
1485
1486 if (!lsp)
1487 {
1488 zlog_err
1489 ("ISIS-Upd (%s): lsp_non_pseudo_regenerate(): no L%d LSP found!",
1490 area->area_tag, level);
1491
1492 return ISIS_ERROR;
1493 }
1494
1495 lsp_clear_data (lsp);
1496 lsp_build_nonpseudo (lsp, area);
1497 lsp->lsp_header->rem_lifetime = htons (isis_jitter
1498 (area->max_lsp_lifetime[level - 1],
1499 MAX_AGE_JITTER));
1500 lsp_seqnum_update (lsp);
1501
1502 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1503 {
1504 zlog_debug ("ISIS-Upd (%s): refreshing our L%d LSP %s, "
1505 "seq 0x%08x, cksum 0x%04x lifetime %us",
1506 area->area_tag,
1507 level,
1508 rawlspid_print (lsp->lsp_header->lsp_id),
1509 ntohl (lsp->lsp_header->seq_num),
1510 ntohs (lsp->lsp_header->checksum),
1511 ntohs (lsp->lsp_header->rem_lifetime));
1512 }
1513
1514 lsp->last_generated = time (NULL);
1515 area->lsp_regenerate_pending[level - 1] = 0;
1516 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
1517 for (ALL_LIST_ELEMENTS_RO (lsp->lspu.frags, node, frag))
1518 {
1519 frag->lsp_header->rem_lifetime = htons (isis_jitter
1520 (area->
1521 max_lsp_lifetime[level - 1],
1522 MAX_AGE_JITTER));
1523 ISIS_FLAGS_SET_ALL (frag->SRMflags);
1524 }
1525
1526 if (area->ip_circuits)
1527 isis_spf_schedule (area, level);
1528 #ifdef HAVE_IPV6
1529 if (area->ipv6_circuits)
1530 isis_spf_schedule6 (area, level);
1531 #endif
1532 return ISIS_OK;
1533 }
1534
1535 /*
1536 * Done at least every MAX_LSP_GEN_INTERVAL. Search own LSPs, update holding
1537 * time and set SRM
1538 */
1539 int
1540 lsp_refresh_l1 (struct thread *thread)
1541 {
1542 struct isis_area *area;
1543 unsigned long ref_time;
1544
1545 area = THREAD_ARG (thread);
1546 assert (area);
1547
1548 area->t_lsp_refresh[0] = NULL;
1549 if (area->is_type & IS_LEVEL_1)
1550 lsp_non_pseudo_regenerate (area, 1);
1551
1552 ref_time = area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
1553 MAX_LSP_GEN_INTERVAL : area->lsp_refresh[0];
1554
1555 THREAD_TIMER_ON (master, area->t_lsp_refresh[0], lsp_refresh_l1, area,
1556 isis_jitter (ref_time, MAX_AGE_JITTER));
1557
1558 return ISIS_OK;
1559 }
1560
1561 int
1562 lsp_refresh_l2 (struct thread *thread)
1563 {
1564 struct isis_area *area;
1565 unsigned long ref_time;
1566
1567 area = THREAD_ARG (thread);
1568 assert (area);
1569
1570 area->t_lsp_refresh[1] = NULL;
1571 if (area->is_type & IS_LEVEL_2)
1572 lsp_non_pseudo_regenerate (area, 2);
1573
1574 ref_time = area->lsp_refresh[1] > MAX_LSP_GEN_INTERVAL ?
1575 MAX_LSP_GEN_INTERVAL : area->lsp_refresh[1];
1576
1577 THREAD_TIMER_ON (master, area->t_lsp_refresh[1], lsp_refresh_l2, area,
1578 isis_jitter (ref_time, MAX_AGE_JITTER));
1579
1580 return ISIS_OK;
1581 }
1582
1583 /*
1584 * Something has changed -> regenerate LSP
1585 */
1586
1587 static int
1588 lsp_l1_regenerate (struct thread *thread)
1589 {
1590 struct isis_area *area;
1591
1592 area = THREAD_ARG (thread);
1593 area->lsp_regenerate_pending[0] = 0;
1594
1595 return lsp_non_pseudo_regenerate (area, 1);
1596 }
1597
1598 static int
1599 lsp_l2_regenerate (struct thread *thread)
1600 {
1601 struct isis_area *area;
1602
1603 area = THREAD_ARG (thread);
1604 area->lsp_regenerate_pending[1] = 0;
1605
1606 return lsp_non_pseudo_regenerate (area, 2);
1607 }
1608
1609 int
1610 lsp_regenerate_schedule (struct isis_area *area)
1611 {
1612 struct isis_lsp *lsp;
1613 u_char id[ISIS_SYS_ID_LEN + 2];
1614 time_t now, diff;
1615 memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
1616 LSP_PSEUDO_ID (id) = LSP_FRAGMENT (id) = 0;
1617 now = time (NULL);
1618 /*
1619 * First level 1
1620 */
1621 if (area->is_type & IS_LEVEL_1)
1622 {
1623 lsp = lsp_search (id, area->lspdb[0]);
1624 if (!lsp || area->lsp_regenerate_pending[0])
1625 goto L2;
1626 /*
1627 * Throttle avoidance
1628 */
1629 diff = now - lsp->last_generated;
1630 if (diff < MIN_LSP_GEN_INTERVAL)
1631 {
1632 area->lsp_regenerate_pending[0] = 1;
1633 thread_add_timer (master, lsp_l1_regenerate, area,
1634 MIN_LSP_GEN_INTERVAL - diff);
1635 goto L2;
1636 }
1637 else
1638 lsp_non_pseudo_regenerate (area, 1);
1639 }
1640 /*
1641 * then 2
1642 */
1643 L2:
1644 if (area->is_type & IS_LEVEL_2)
1645 {
1646 lsp = lsp_search (id, area->lspdb[1]);
1647 if (!lsp || area->lsp_regenerate_pending[1])
1648 return ISIS_OK;
1649 /*
1650 * Throttle avoidance
1651 */
1652 diff = now - lsp->last_generated;
1653 if (diff < MIN_LSP_GEN_INTERVAL)
1654 {
1655 area->lsp_regenerate_pending[1] = 1;
1656 thread_add_timer (master, lsp_l2_regenerate, area,
1657 MIN_LSP_GEN_INTERVAL - diff);
1658 return ISIS_OK;
1659 }
1660 else
1661 lsp_non_pseudo_regenerate (area, 2);
1662 }
1663
1664 return ISIS_OK;
1665 }
1666
1667 /*
1668 * Funcs for pseudonode LSPs
1669 */
1670
1671 /*
1672 * 7.3.8 and 7.3.10 Generation of level 1 and 2 pseudonode LSPs
1673 */
1674 static void
1675 lsp_build_pseudo (struct isis_lsp *lsp, struct isis_circuit *circuit,
1676 int level)
1677 {
1678 struct isis_adjacency *adj;
1679 struct is_neigh *is_neigh;
1680 struct te_is_neigh *te_is_neigh;
1681 struct es_neigh *es_neigh;
1682 struct list *adj_list;
1683 struct listnode *node, *nnode;
1684 struct isis_passwd *passwd;
1685
1686 assert (circuit);
1687 assert (circuit->circ_type == CIRCUIT_T_BROADCAST);
1688
1689 if (!circuit->u.bc.is_dr[level - 1])
1690 return; /* we are not DIS on this circuit */
1691
1692 lsp->level = level;
1693 if (level == 1)
1694 lsp->lsp_header->lsp_bits |= IS_LEVEL_1;
1695 else
1696 lsp->lsp_header->lsp_bits |= IS_LEVEL_2;
1697
1698 /*
1699 * add self to IS neighbours
1700 */
1701 if (circuit->area->oldmetric)
1702 {
1703 if (lsp->tlv_data.is_neighs == NULL)
1704 {
1705 lsp->tlv_data.is_neighs = list_new ();
1706 lsp->tlv_data.is_neighs->del = free_tlv;
1707 }
1708 is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
1709
1710 memcpy (&is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN);
1711 listnode_add (lsp->tlv_data.is_neighs, is_neigh);
1712 }
1713 if (circuit->area->newmetric)
1714 {
1715 if (lsp->tlv_data.te_is_neighs == NULL)
1716 {
1717 lsp->tlv_data.te_is_neighs = list_new ();
1718 lsp->tlv_data.te_is_neighs->del = free_tlv;
1719 }
1720 te_is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct te_is_neigh));
1721
1722 memcpy (&te_is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN);
1723 listnode_add (lsp->tlv_data.te_is_neighs, te_is_neigh);
1724 }
1725
1726 adj_list = list_new ();
1727 isis_adj_build_up_list (circuit->u.bc.adjdb[level - 1], adj_list);
1728
1729 for (ALL_LIST_ELEMENTS (adj_list, node, nnode, adj))
1730 {
1731 if (adj->circuit_t & level)
1732 {
1733 if ((level == 1 && adj->sys_type == ISIS_SYSTYPE_L1_IS) ||
1734 (level == 1 && adj->sys_type == ISIS_SYSTYPE_L2_IS &&
1735 adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
1736 (level == 2 && adj->sys_type == ISIS_SYSTYPE_L2_IS))
1737 {
1738 /* an IS neighbour -> add it */
1739 if (circuit->area->oldmetric)
1740 {
1741 is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
1742
1743 memcpy (&is_neigh->neigh_id, adj->sysid, ISIS_SYS_ID_LEN);
1744 listnode_add (lsp->tlv_data.is_neighs, is_neigh);
1745 }
1746 if (circuit->area->newmetric)
1747 {
1748 te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
1749 sizeof (struct te_is_neigh));
1750 memcpy (&te_is_neigh->neigh_id, adj->sysid, ISIS_SYS_ID_LEN);
1751 listnode_add (lsp->tlv_data.te_is_neighs, te_is_neigh);
1752 }
1753 }
1754 else if (level == 1 && adj->sys_type == ISIS_SYSTYPE_ES)
1755 {
1756 /* an ES neigbour add it, if we are building level 1 LSP */
1757 /* FIXME: the tlv-format is hard to use here */
1758 if (lsp->tlv_data.es_neighs == NULL)
1759 {
1760 lsp->tlv_data.es_neighs = list_new ();
1761 lsp->tlv_data.es_neighs->del = free_tlv;
1762 }
1763 es_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct es_neigh));
1764
1765 memcpy (&es_neigh->first_es_neigh, adj->sysid, ISIS_SYS_ID_LEN);
1766 listnode_add (lsp->tlv_data.es_neighs, es_neigh);
1767 }
1768 }
1769 }
1770
1771 /* Reset endp of stream to overwrite only TLV part of it. */
1772 stream_reset (lsp->pdu);
1773 stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
1774
1775 /*
1776 * Add the authentication info if it's present
1777 */
1778 (level == 1) ? (passwd = &circuit->area->area_passwd) :
1779 (passwd = &circuit->area->domain_passwd);
1780 if (passwd->type)
1781 {
1782 memcpy (&lsp->tlv_data.auth_info, passwd, sizeof (struct isis_passwd));
1783 tlv_add_authinfo (passwd->type, passwd->len, passwd->passwd, lsp->pdu);
1784 }
1785
1786 if (lsp->tlv_data.is_neighs && listcount (lsp->tlv_data.is_neighs) > 0)
1787 tlv_add_is_neighs (lsp->tlv_data.is_neighs, lsp->pdu);
1788
1789 if (lsp->tlv_data.te_is_neighs && listcount (lsp->tlv_data.te_is_neighs) > 0)
1790 tlv_add_te_is_neighs (lsp->tlv_data.te_is_neighs, lsp->pdu);
1791
1792 if (lsp->tlv_data.es_neighs && listcount (lsp->tlv_data.es_neighs) > 0)
1793 tlv_add_is_neighs (lsp->tlv_data.es_neighs, lsp->pdu);
1794
1795 lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
1796 iso_csum_create (STREAM_DATA (lsp->pdu) + 12,
1797 ntohs (lsp->lsp_header->pdu_len) - 12, 12);
1798
1799 list_delete (adj_list);
1800
1801 return;
1802 }
1803
1804 static int
1805 lsp_pseudo_regenerate (struct isis_circuit *circuit, int level)
1806 {
1807 dict_t *lspdb = circuit->area->lspdb[level - 1];
1808 struct isis_lsp *lsp;
1809 u_char lsp_id[ISIS_SYS_ID_LEN + 2];
1810
1811 memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
1812 LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
1813 LSP_FRAGMENT (lsp_id) = 0;
1814
1815 lsp = lsp_search (lsp_id, lspdb);
1816
1817 if (!lsp)
1818 {
1819 zlog_err ("lsp_pseudo_regenerate(): no l%d LSP %s found!", level,
1820 rawlspid_print (lsp_id));
1821 return ISIS_ERROR;
1822 }
1823 lsp_clear_data (lsp);
1824
1825 lsp_build_pseudo (lsp, circuit, level);
1826
1827 lsp->lsp_header->rem_lifetime =
1828 htons (isis_jitter (circuit->area->max_lsp_lifetime[level - 1],
1829 MAX_AGE_JITTER));
1830
1831 lsp_inc_seqnum (lsp, 0);
1832
1833 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1834 {
1835 zlog_debug ("ISIS-Upd (%s): refreshing pseudo LSP L%d %s",
1836 circuit->area->area_tag, level,
1837 rawlspid_print (lsp->lsp_header->lsp_id));
1838 }
1839
1840 lsp->last_generated = time (NULL);
1841 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
1842
1843 return ISIS_OK;
1844 }
1845
1846 int
1847 lsp_l1_refresh_pseudo (struct thread *thread)
1848 {
1849 struct isis_circuit *circuit;
1850 int retval;
1851 unsigned long ref_time;
1852
1853 circuit = THREAD_ARG (thread);
1854
1855 if (!circuit->u.bc.is_dr[0])
1856 return ISIS_ERROR; /* FIXME: purge and such */
1857
1858 circuit->u.bc.t_refresh_pseudo_lsp[0] = NULL;
1859
1860 retval = lsp_pseudo_regenerate (circuit, 1);
1861
1862 ref_time = circuit->area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
1863 MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[0];
1864
1865 THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[0],
1866 lsp_l1_refresh_pseudo, circuit,
1867 isis_jitter (ref_time, MAX_AGE_JITTER));
1868
1869 return retval;
1870 }
1871
1872 int
1873 lsp_l1_pseudo_generate (struct isis_circuit *circuit)
1874 {
1875 struct isis_lsp *lsp;
1876 u_char id[ISIS_SYS_ID_LEN + 2];
1877 unsigned long ref_time;
1878
1879 memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
1880 LSP_FRAGMENT (id) = 0;
1881 LSP_PSEUDO_ID (id) = circuit->circuit_id;
1882
1883 /*
1884 * If for some reason have a pseudo LSP in the db already -> regenerate
1885 */
1886 if (lsp_search (id, circuit->area->lspdb[0]))
1887 return lsp_pseudo_regenerate (circuit, 1);
1888 lsp = lsp_new (id, circuit->area->max_lsp_lifetime[0],
1889 1, circuit->area->is_type, 0, 1);
1890
1891 lsp_build_pseudo (lsp, circuit, 1);
1892
1893 lsp->own_lsp = 1;
1894 lsp_insert (lsp, circuit->area->lspdb[0]);
1895 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
1896
1897 ref_time = circuit->area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
1898 MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[0];
1899
1900 THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[0],
1901 lsp_l1_refresh_pseudo, circuit,
1902 isis_jitter (ref_time, MAX_AGE_JITTER));
1903
1904 return lsp_regenerate_schedule (circuit->area);
1905 }
1906
1907 int
1908 lsp_l2_refresh_pseudo (struct thread *thread)
1909 {
1910 struct isis_circuit *circuit;
1911 int retval;
1912 unsigned long ref_time;
1913 circuit = THREAD_ARG (thread);
1914
1915 if (!circuit->u.bc.is_dr[1])
1916 return ISIS_ERROR; /* FIXME: purge and such */
1917
1918 circuit->u.bc.t_refresh_pseudo_lsp[1] = NULL;
1919
1920 retval = lsp_pseudo_regenerate (circuit, 2);
1921
1922 ref_time = circuit->area->lsp_refresh[1] > MAX_LSP_GEN_INTERVAL ?
1923 MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[1];
1924
1925 THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[1],
1926 lsp_l2_refresh_pseudo, circuit,
1927 isis_jitter (ref_time, MAX_AGE_JITTER));
1928
1929 return retval;
1930 }
1931
1932 int
1933 lsp_l2_pseudo_generate (struct isis_circuit *circuit)
1934 {
1935 struct isis_lsp *lsp;
1936 u_char id[ISIS_SYS_ID_LEN + 2];
1937 unsigned long ref_time;
1938
1939 memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
1940 LSP_FRAGMENT (id) = 0;
1941 LSP_PSEUDO_ID (id) = circuit->circuit_id;
1942
1943 if (lsp_search (id, circuit->area->lspdb[1]))
1944 return lsp_pseudo_regenerate (circuit, 2);
1945
1946 lsp = lsp_new (id, circuit->area->max_lsp_lifetime[1],
1947 1, circuit->area->is_type, 0, 2);
1948
1949 lsp_build_pseudo (lsp, circuit, 2);
1950
1951 ref_time = circuit->area->lsp_refresh[1] > MAX_LSP_GEN_INTERVAL ?
1952 MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[1];
1953
1954
1955 lsp->own_lsp = 1;
1956 lsp_insert (lsp, circuit->area->lspdb[1]);
1957 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
1958
1959 THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[1],
1960 lsp_l2_refresh_pseudo, circuit,
1961 isis_jitter (ref_time, MAX_AGE_JITTER));
1962
1963 return lsp_regenerate_schedule (circuit->area);
1964 }
1965
1966 /*
1967 * Walk through LSPs for an area
1968 * - set remaining lifetime
1969 * - set LSPs with SRMflag set for sending
1970 */
1971 int
1972 lsp_tick (struct thread *thread)
1973 {
1974 struct isis_area *area;
1975 struct isis_circuit *circuit;
1976 struct isis_lsp *lsp;
1977 struct list *lsp_list;
1978 struct listnode *lspnode, *lspnnode, *cnode;
1979 dnode_t *dnode, *dnode_next;
1980 int level;
1981
1982 lsp_list = list_new ();
1983
1984 area = THREAD_ARG (thread);
1985 assert (area);
1986 area->t_tick = NULL;
1987 THREAD_TIMER_ON (master, area->t_tick, lsp_tick, area, 1);
1988
1989 /*
1990 * Build a list of LSPs with (any) SRMflag set
1991 * and removed the ones that have aged out
1992 */
1993 for (level = 0; level < ISIS_LEVELS; level++)
1994 {
1995 if (area->lspdb[level] && dict_count (area->lspdb[level]) > 0)
1996 {
1997 dnode = dict_first (area->lspdb[level]);
1998 while (dnode != NULL)
1999 {
2000 dnode_next = dict_next (area->lspdb[level], dnode);
2001 lsp = dnode_get (dnode);
2002 lsp_set_time (lsp);
2003 if (lsp->age_out == 0)
2004 {
2005
2006 zlog_debug ("ISIS-Upd (%s): L%u LSP %s seq 0x%08x aged out",
2007 area->area_tag,
2008 lsp->level,
2009 rawlspid_print (lsp->lsp_header->lsp_id),
2010 ntohl (lsp->lsp_header->seq_num));
2011 #ifdef TOPOLOGY_GENERATE
2012 if (lsp->from_topology)
2013 THREAD_TIMER_OFF (lsp->t_lsp_top_ref);
2014 #endif /* TOPOLOGY_GENERATE */
2015 lsp_destroy (lsp);
2016 dict_delete (area->lspdb[level], dnode);
2017 }
2018 else if (flags_any_set (lsp->SRMflags))
2019 listnode_add (lsp_list, lsp);
2020 dnode = dnode_next;
2021 }
2022
2023 /*
2024 * Send LSPs on circuits indicated by the SRMflags
2025 */
2026 if (listcount (lsp_list) > 0)
2027 {
2028 for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
2029 {
2030 for (ALL_LIST_ELEMENTS (lsp_list, lspnode, lspnnode, lsp))
2031 {
2032 if (ISIS_CHECK_FLAG (lsp->SRMflags, circuit))
2033 {
2034 /* FIXME: if same or elder lsp is already in lsp
2035 * queue */
2036 listnode_add (circuit->lsp_queue, lsp);
2037 thread_add_event (master, send_lsp, circuit, 0);
2038 }
2039 }
2040 }
2041 }
2042 list_delete_all_node (lsp_list);
2043 }
2044 }
2045
2046 list_delete (lsp_list);
2047
2048 return ISIS_OK;
2049 }
2050
2051 void
2052 lsp_purge_dr (u_char * id, struct isis_circuit *circuit, int level)
2053 {
2054 struct isis_lsp *lsp;
2055
2056 lsp = lsp_search (id, circuit->area->lspdb[level - 1]);
2057
2058 if (lsp && lsp->purged == 0)
2059 {
2060 lsp->lsp_header->rem_lifetime = htons (0);
2061 lsp->lsp_header->pdu_len =
2062 htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
2063 lsp->purged = 0;
2064 iso_csum_create (STREAM_DATA (lsp->pdu) + 12,
2065 ntohs (lsp->lsp_header->pdu_len) - 12, 12);
2066 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
2067 }
2068
2069 return;
2070 }
2071
2072 /*
2073 * Purge own LSP that is received and we don't have.
2074 * -> Do as in 7.3.16.4
2075 */
2076 void
2077 lsp_purge_non_exist (struct isis_link_state_hdr *lsp_hdr,
2078 struct isis_area *area)
2079 {
2080 struct isis_lsp *lsp;
2081
2082 /*
2083 * We need to create the LSP to be purged
2084 */
2085 zlog_debug ("LSP PURGE NON EXIST");
2086 lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
2087 /*FIXME: BUG BUG BUG! the lsp doesn't exist here! */
2088 /*did smt here, maybe good probably not */
2089 lsp->level = ((lsp_hdr->lsp_bits & LSPBIT_IST) == IS_LEVEL_1) ? 1 : 2;
2090 lsp->pdu = stream_new (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
2091 lsp->isis_header = (struct isis_fixed_hdr *) STREAM_DATA (lsp->pdu);
2092 fill_fixed_hdr (lsp->isis_header, (lsp->level == 1) ? L1_LINK_STATE
2093 : L2_LINK_STATE);
2094 lsp->lsp_header = (struct isis_link_state_hdr *) (STREAM_DATA (lsp->pdu) +
2095 ISIS_FIXED_HDR_LEN);
2096 memcpy (lsp->lsp_header, lsp_hdr, ISIS_LSP_HDR_LEN);
2097
2098 /*
2099 * Retain only LSP header
2100 */
2101 lsp->lsp_header->pdu_len = htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
2102 /*
2103 * Set the remaining lifetime to 0
2104 */
2105 lsp->lsp_header->rem_lifetime = 0;
2106 /*
2107 * Put the lsp into LSPdb
2108 */
2109 lsp_insert (lsp, area->lspdb[lsp->level - 1]);
2110
2111 /*
2112 * Send in to whole area
2113 */
2114 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
2115
2116 return;
2117 }
2118
2119 #ifdef TOPOLOGY_GENERATE
2120 static int
2121 top_lsp_refresh (struct thread *thread)
2122 {
2123 struct isis_lsp *lsp;
2124 unsigned long ref_time;
2125
2126 lsp = THREAD_ARG (thread);
2127 assert (lsp);
2128
2129 lsp->t_lsp_top_ref = NULL;
2130
2131 lsp_seqnum_update (lsp);
2132
2133 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
2134 if (isis->debugs & DEBUG_UPDATE_PACKETS)
2135 {
2136 zlog_debug ("ISIS-Upd (): refreshing Topology L1 %s",
2137 rawlspid_print (lsp->lsp_header->lsp_id));
2138 }
2139 lsp->lsp_header->rem_lifetime =
2140 htons (isis_jitter (lsp->area->max_lsp_lifetime[0], MAX_AGE_JITTER));
2141
2142 ref_time = lsp->area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
2143 MAX_LSP_GEN_INTERVAL : lsp->area->lsp_refresh[0];
2144
2145 THREAD_TIMER_ON (master, lsp->t_lsp_top_ref, top_lsp_refresh, lsp,
2146 isis_jitter (ref_time, MAX_LSP_GEN_JITTER));
2147
2148 return ISIS_OK;
2149 }
2150
2151 void
2152 generate_topology_lsps (struct isis_area *area)
2153 {
2154 struct listnode *node;
2155 int i, max = 0;
2156 struct arc *arc;
2157 u_char lspid[ISIS_SYS_ID_LEN + 2];
2158 struct isis_lsp *lsp;
2159 unsigned long ref_time;
2160
2161 /* first we find the maximal node */
2162 for (ALL_LIST_ELEMENTS_RO (area->topology, node, arc))
2163 {
2164 if (arc->from_node > max)
2165 max = arc->from_node;
2166 if (arc->to_node > max)
2167 max = arc->to_node;
2168 }
2169
2170 for (i = 1; i < (max + 1); i++)
2171 {
2172 memcpy (lspid, area->topology_baseis, ISIS_SYS_ID_LEN);
2173 LSP_PSEUDO_ID (lspid) = 0x00;
2174 LSP_FRAGMENT (lspid) = 0x00;
2175 lspid[ISIS_SYS_ID_LEN - 1] = (i & 0xFF);
2176 lspid[ISIS_SYS_ID_LEN - 2] = ((i >> 8) & 0xFF);
2177
2178 lsp = lsp_new (lspid, isis_jitter (area->max_lsp_lifetime[0],
2179 MAX_AGE_JITTER), 1, IS_LEVEL_1, 0, 1);
2180 if (!lsp)
2181 return;
2182 lsp->from_topology = 1;
2183 lsp->area = area;
2184
2185 /* Creating LSP data based on topology info. */
2186 build_topology_lsp_data (lsp, area, i);
2187 /* Checksum is also calculated here. */
2188 lsp_seqnum_update (lsp);
2189
2190 ref_time = area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
2191 MAX_LSP_GEN_INTERVAL : area->lsp_refresh[0];
2192
2193 THREAD_TIMER_ON (master, lsp->t_lsp_top_ref, top_lsp_refresh, lsp,
2194 isis_jitter (ref_time, MAX_LSP_GEN_JITTER));
2195 ISIS_FLAGS_SET_ALL (lsp->SRMflags);
2196 lsp_insert (lsp, area->lspdb[0]);
2197 }
2198 }
2199
2200 void
2201 remove_topology_lsps (struct isis_area *area)
2202 {
2203 struct isis_lsp *lsp;
2204 dnode_t *dnode, *dnode_next;
2205
2206 dnode = dict_first (area->lspdb[0]);
2207 while (dnode != NULL)
2208 {
2209 dnode_next = dict_next (area->lspdb[0], dnode);
2210 lsp = dnode_get (dnode);
2211 if (lsp->from_topology)
2212 {
2213 THREAD_TIMER_OFF (lsp->t_lsp_top_ref);
2214 lsp_destroy (lsp);
2215 dict_delete (area->lspdb[0], dnode);
2216 }
2217 dnode = dnode_next;
2218 }
2219 }
2220
2221 void
2222 build_topology_lsp_data (struct isis_lsp *lsp, struct isis_area *area,
2223 int lsp_top_num)
2224 {
2225 struct listnode *node, *nnode;
2226 struct arc *arc;
2227 struct is_neigh *is_neigh;
2228 char buff[200];
2229 struct tlvs tlv_data;
2230 struct isis_lsp *lsp0 = lsp;
2231
2232 /* Add area addresses. FIXME: Is it needed at all? */
2233 if (lsp->tlv_data.area_addrs == NULL)
2234 lsp->tlv_data.area_addrs = list_new ();
2235 list_add_list (lsp->tlv_data.area_addrs, area->area_addrs);
2236
2237 if (lsp->tlv_data.nlpids == NULL)
2238 lsp->tlv_data.nlpids = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct nlpids));
2239 lsp->tlv_data.nlpids->count = 1;
2240 lsp->tlv_data.nlpids->nlpids[0] = NLPID_IP;
2241
2242 if (area->dynhostname)
2243 {
2244 lsp->tlv_data.hostname = XMALLOC (MTYPE_ISIS_TLV,
2245 sizeof (struct hostname));
2246 memset (buff, 0x00, 200);
2247 sprintf (buff, "%s%d", area->topology_basedynh ? area->topology_basedynh :
2248 "feedme", lsp_top_num);
2249 memcpy (lsp->tlv_data.hostname->name, buff, strlen (buff));
2250 lsp->tlv_data.hostname->namelen = strlen (buff);
2251 }
2252
2253 if (lsp->tlv_data.nlpids)
2254 tlv_add_nlpid (lsp->tlv_data.nlpids, lsp->pdu);
2255 if (lsp->tlv_data.hostname)
2256 tlv_add_dynamic_hostname (lsp->tlv_data.hostname, lsp->pdu);
2257 if (lsp->tlv_data.area_addrs && listcount (lsp->tlv_data.area_addrs) > 0)
2258 tlv_add_area_addrs (lsp->tlv_data.area_addrs, lsp->pdu);
2259
2260 memset (&tlv_data, 0, sizeof (struct tlvs));
2261 if (tlv_data.is_neighs == NULL)
2262 tlv_data.is_neighs = list_new ();
2263
2264 /* Add reachability for this IS for simulated 1. */
2265 if (lsp_top_num == 1)
2266 {
2267 is_neigh = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
2268 memset (is_neigh, 0, sizeof (struct is_neigh));
2269
2270 memcpy (&is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN);
2271 LSP_PSEUDO_ID (is_neigh->neigh_id) = 0x00;
2272 /* Metric MUST NOT be 0, unless it's not alias TLV. */
2273 is_neigh->metrics.metric_default = 0x01;
2274 is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
2275 is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
2276 is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
2277 listnode_add (tlv_data.is_neighs, is_neigh);
2278 }
2279
2280 /* Add IS reachabilities. */
2281 for (ALL_LIST_ELEMENTS (area->topology, node, nnode, arc))
2282 {
2283 int to_lsp = 0;
2284
2285 if ((lsp_top_num != arc->from_node) && (lsp_top_num != arc->to_node))
2286 continue;
2287
2288 if (lsp_top_num == arc->from_node)
2289 to_lsp = arc->to_node;
2290 else
2291 to_lsp = arc->from_node;
2292
2293 is_neigh = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
2294 memset (is_neigh, 0, sizeof (struct is_neigh));
2295
2296 memcpy (&is_neigh->neigh_id, area->topology_baseis, ISIS_SYS_ID_LEN);
2297 is_neigh->neigh_id[ISIS_SYS_ID_LEN - 1] = (to_lsp & 0xFF);
2298 is_neigh->neigh_id[ISIS_SYS_ID_LEN - 2] = ((to_lsp >> 8) & 0xFF);
2299 is_neigh->metrics.metric_default = arc->distance;
2300 is_neigh->metrics.metric_delay = METRICS_UNSUPPORTED;
2301 is_neigh->metrics.metric_expense = METRICS_UNSUPPORTED;
2302 is_neigh->metrics.metric_error = METRICS_UNSUPPORTED;
2303 listnode_add (tlv_data.is_neighs, is_neigh);
2304 }
2305
2306 while (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
2307 {
2308 if (lsp->tlv_data.is_neighs == NULL)
2309 lsp->tlv_data.is_neighs = list_new ();
2310 lsp_tlv_fit (lsp, &tlv_data.is_neighs,
2311 &lsp->tlv_data.is_neighs,
2312 IS_NEIGHBOURS_LEN, area->lsp_frag_threshold,
2313 tlv_add_is_neighs);
2314 if (tlv_data.is_neighs && listcount (tlv_data.is_neighs))
2315 lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
2316 lsp0, area, IS_LEVEL_1);
2317 }
2318
2319 free_tlvs (&tlv_data);
2320 return;
2321 }
2322 #endif /* TOPOLOGY_GENERATE */