summaryrefslogtreecommitdiff
path: root/isisd/isis_tlv.c
blob: f3b2c338f5e9691b635966e2fb3f0cf4fa35ea03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
/*
 * IS-IS Rout(e)ing protocol - isis_tlv.c
 *                             IS-IS TLV related routines
 *
 * Copyright (C) 2001,2002   Sampo Saaristo
 *                           Tampere University of Technology      
 *                           Institute of Communications Engineering
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public Licenseas published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
 * more details.

 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <zebra.h>

#include "log.h"
#include "linklist.h"
#include "stream.h"
#include "memory.h"
#include "prefix.h"
#include "vty.h"
#include "if.h"

#include "isisd/dict.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
#include "isisd/isis_flags.h"
#include "isisd/isis_circuit.h"
#include "isisd/isis_tlv.h"
#include "isisd/isisd.h"
#include "isisd/isis_dynhn.h"
#include "isisd/isis_misc.h"
#include "isisd/isis_pdu.h"
#include "isisd/isis_lsp.h"

void
free_tlv (void *val)
{
  XFREE (MTYPE_ISIS_TLV, val);

  return;
}

/*
 * Called after parsing of a PDU. There shouldn't be any tlv's left, so this
 * is only a caution to avoid memory leaks
 */
void
free_tlvs (struct tlvs *tlvs)
{
  if (tlvs->area_addrs)
    list_delete (tlvs->area_addrs);
  if (tlvs->is_neighs)
    list_delete (tlvs->is_neighs);
  if (tlvs->te_is_neighs)
    list_delete (tlvs->te_is_neighs);
  if (tlvs->es_neighs)
    list_delete (tlvs->es_neighs);
  if (tlvs->lsp_entries)
    list_delete (tlvs->lsp_entries);
  if (tlvs->prefix_neighs)
    list_delete (tlvs->prefix_neighs);
  if (tlvs->lan_neighs)
    list_delete (tlvs->lan_neighs);
  if (tlvs->ipv4_addrs)
    list_delete (tlvs->ipv4_addrs);
  if (tlvs->ipv4_int_reachs)
    list_delete (tlvs->ipv4_int_reachs);
  if (tlvs->ipv4_ext_reachs)
    list_delete (tlvs->ipv4_ext_reachs);
  if (tlvs->te_ipv4_reachs)
    list_delete (tlvs->te_ipv4_reachs);
#ifdef HAVE_IPV6
  if (tlvs->ipv6_addrs)
    list_delete (tlvs->ipv6_addrs);
  if (tlvs->ipv6_reachs)
    list_delete (tlvs->ipv6_reachs);
#endif /* HAVE_IPV6 */

  memset (tlvs, 0, sizeof (struct tlvs));

  return;
}

/*
 * Parses the tlvs found in the variant length part of the PDU.
 * Caller tells with flags in "expected" which TLV's it is interested in.
 */
int
parse_tlvs (char *areatag, u_char * stream, int size, u_int32_t * expected,
	    u_int32_t * found, struct tlvs *tlvs, u_int32_t *auth_tlv_offset)
{
  u_char type, length;
  struct lan_neigh *lan_nei;
  struct area_addr *area_addr;
  struct is_neigh *is_nei;
  struct te_is_neigh *te_is_nei;
  struct es_neigh *es_nei;
  struct lsp_entry *lsp_entry;
  struct in_addr *ipv4_addr;
  struct ipv4_reachability *ipv4_reach;
  struct te_ipv4_reachability *te_ipv4_reach;
#ifdef HAVE_IPV6
  struct in6_addr *ipv6_addr;
  struct ipv6_reachability *ipv6_reach;
  int prefix_octets;
#endif /* HAVE_IPV6 */
  u_char virtual;
  int value_len, retval = ISIS_OK;
  u_char *start = stream, *pnt = stream, *endpnt;

  *found = 0;
  memset (tlvs, 0, sizeof (struct tlvs));

  while (pnt < stream + size - 2)
    {
      type = *pnt;
      length = *(pnt + 1);
      pnt += 2;
      value_len = 0;
      if (pnt + length > stream + size)
	{
	  zlog_warn ("ISIS-TLV (%s): TLV (type %d, length %d) exceeds packet "
		     "boundaries", areatag, type, length);
	  retval = ISIS_WARNING;
	  break;
	}
      switch (type)
	{
	case AREA_ADDRESSES:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        Address Length                         | 
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                         Area Address                          | 
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_AREA_ADDRS;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("TLV Area Adresses len %d", length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_AREA_ADDRS)
	    {
	      while (length > value_len)
		{
		  area_addr = (struct area_addr *) pnt;
		  value_len += area_addr->addr_len + 1;
		  pnt += area_addr->addr_len + 1;
		  if (!tlvs->area_addrs)
		    tlvs->area_addrs = list_new ();
		  listnode_add (tlvs->area_addrs, area_addr);
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case IS_NEIGHBOURS:
	  *found |= TLVFLAG_IS_NEIGHS;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IS Neighbours length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (TLVFLAG_IS_NEIGHS & *expected)
	    {
	      /* +-------+-------+-------+-------+-------+-------+-------+-------+
	       * |                        Virtual Flag                           | 
	       * +-------+-------+-------+-------+-------+-------+-------+-------+
	       */
	      virtual = *pnt;	/* FIXME: what is the use for this? */
	      pnt++;
	      value_len++;
	      /* +-------+-------+-------+-------+-------+-------+-------+-------+
	       * |   0   |  I/E  |               Default Metric                  | 
	       * +-------+-------+-------+-------+-------+-------+-------+-------+
	       * |   S   |  I/E  |               Delay Metric                    |
	       * +-------+-------+-------+-------+-------+-------+-------+-------+
	       * |   S   |  I/E  |               Expense Metric                  |
	       * +-------+-------+-------+-------+-------+-------+-------+-------+
	       * |   S   |  I/E  |               Error Metric                    |
	       * +-------+-------+-------+-------+-------+-------+-------+-------+
	       * |                        Neighbour ID                           |
	       * +---------------------------------------------------------------+
	       * :                                                               :
	       */
	      while (length > value_len)
		{
		  is_nei = (struct is_neigh *) pnt;
		  value_len += 4 + ISIS_SYS_ID_LEN + 1;
		  pnt += 4 + ISIS_SYS_ID_LEN + 1;
		  if (!tlvs->is_neighs)
		    tlvs->is_neighs = list_new ();
		  listnode_add (tlvs->is_neighs, is_nei);
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case TE_IS_NEIGHBOURS:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        Neighbour ID                           | 7
	   * +---------------------------------------------------------------+
	   * |                        TE Metric                              | 3
	   * +---------------------------------------------------------------+
	   * |                        SubTLVs Length                         | 1
	   * +---------------------------------------------------------------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_TE_IS_NEIGHS;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): Extended IS Neighbours length %d",
		     areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (TLVFLAG_TE_IS_NEIGHS & *expected)
	    {
	      while (length > value_len)
		{
		  te_is_nei = (struct te_is_neigh *) pnt;
		  value_len += 11;
		  pnt += 11;
		  /* FIXME - subtlvs are handled here, for now we skip */
		  value_len += te_is_nei->sub_tlvs_length;
		  pnt += te_is_nei->sub_tlvs_length;

		  if (!tlvs->te_is_neighs)
		    tlvs->te_is_neighs = list_new ();
		  listnode_add (tlvs->te_is_neighs, te_is_nei);
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case ES_NEIGHBOURS:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   0   |  I/E  |               Default Metric                  | 
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Delay Metric                    |
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Expense Metric                  |
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Error Metric                    |
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        Neighbour ID                           |
	   * +---------------------------------------------------------------+
	   * |                        Neighbour ID                           |
	   * +---------------------------------------------------------------+
	   * :                                                               :
	   */
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): ES Neighbours length %d",
		     areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  *found |= TLVFLAG_ES_NEIGHS;
	  if (*expected & TLVFLAG_ES_NEIGHS)
	    {
	      es_nei = (struct es_neigh *) pnt;
	      value_len += 4;
	      pnt += 4;
	      while (length > value_len)
		{
		  /* FIXME FIXME FIXME - add to the list */
		  /*          sys_id->id = pnt; */
		  value_len += ISIS_SYS_ID_LEN;
		  pnt += ISIS_SYS_ID_LEN;
		  /*  if (!es_nei->neigh_ids) es_nei->neigh_ids = sysid; */
		}
	      if (!tlvs->es_neighs)
		tlvs->es_neighs = list_new ();
	      listnode_add (tlvs->es_neighs, es_nei);
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case LAN_NEIGHBOURS:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        LAN Address                            | 
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_LAN_NEIGHS;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): LAN Neigbours length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (TLVFLAG_LAN_NEIGHS & *expected)
	    {
	      while (length > value_len)
		{
		  lan_nei = (struct lan_neigh *) pnt;
		  if (!tlvs->lan_neighs)
		    tlvs->lan_neighs = list_new ();
		  listnode_add (tlvs->lan_neighs, lan_nei);
		  value_len += ETH_ALEN;
		  pnt += ETH_ALEN;
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case PADDING:
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("TLV padding %d", length);
#endif /* EXTREME_TLV_DEBUG */
	  pnt += length;
	  break;

	case LSP_ENTRIES:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                     Remaining Lifetime                        | 2
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                         LSP ID                                | id+2
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                   LSP Sequence Number                         | 4
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        Checksum                               | 2
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   */
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): LSP Entries length %d", areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  *found |= TLVFLAG_LSP_ENTRIES;
	  if (TLVFLAG_LSP_ENTRIES & *expected)
	    {
	      while (length > value_len)
		{
		  lsp_entry = (struct lsp_entry *) pnt;
		  value_len += 10 + ISIS_SYS_ID_LEN;
		  pnt += 10 + ISIS_SYS_ID_LEN;
		  if (!tlvs->lsp_entries)
		    tlvs->lsp_entries = list_new ();
		  listnode_add (tlvs->lsp_entries, lsp_entry);
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case CHECKSUM:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                   16 bit fletcher CHECKSUM                    |
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_CHECKSUM;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): Checksum length %d", areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_CHECKSUM)
	    {
	      tlvs->checksum = (struct checksum *) pnt;
	    }
	  pnt += length;
	  break;

	case PROTOCOLS_SUPPORTED:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                       NLPID                                   |
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_NLPID;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): Protocols Supported length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_NLPID)
	    {
	      tlvs->nlpids = (struct nlpids *) (pnt - 1);
	    }
	  pnt += length;
	  break;

	case IPV4_ADDR:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * +                 IP version 4 address                          + 4
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_IPV4_ADDR;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IPv4 Address length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_IPV4_ADDR)
	    {
	      while (length > value_len)
		{
		  ipv4_addr = (struct in_addr *) pnt;
#ifdef EXTREME_TLV_DEBUG
		  zlog_debug ("ISIS-TLV (%s) : IP ADDR %s, pnt %p", areatag,
			      inet_ntoa (*ipv4_addr), pnt);
#endif /* EXTREME_TLV_DEBUG */
		  if (!tlvs->ipv4_addrs)
		    tlvs->ipv4_addrs = list_new ();
		  listnode_add (tlvs->ipv4_addrs, ipv4_addr);
		  value_len += 4;
		  pnt += 4;
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case AUTH_INFO:
	  *found |= TLVFLAG_AUTH_INFO;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IS-IS Authentication Information",
		      areatag);
#endif
	  if (*expected & TLVFLAG_AUTH_INFO)
	    {
	      tlvs->auth_info.type = *pnt;
              if (length == 0)
                {
                  zlog_warn ("ISIS-TLV (%s): TLV (type %d, length %d) "
                             "incorrect.", areatag, type, length);
                  return ISIS_WARNING;
                }
              --length;
	      tlvs->auth_info.len = length;
	      pnt++;
	      memcpy (tlvs->auth_info.passwd, pnt, length);
              /* Return the authentication tlv pos for later computation
               * of MD5 (RFC 5304, 2)
               */
              if (auth_tlv_offset)
                *auth_tlv_offset += (pnt - start - 3);
              pnt += length;
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case DYNAMIC_HOSTNAME:
	  *found |= TLVFLAG_DYN_HOSTNAME;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): Dynamic Hostname length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_DYN_HOSTNAME)
	    {
	      /* the length is also included in the pointed struct */
	      tlvs->hostname = (struct hostname *) (pnt - 1);
	    }
	  pnt += length;
	  break;

	case TE_ROUTER_ID:
	  /* +---------------------------------------------------------------+
	   * +                         Router ID                             + 4
	   * +---------------------------------------------------------------+
	   */
	  *found |= TLVFLAG_TE_ROUTER_ID;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): TE Router ID %d", areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_TE_ROUTER_ID)
	    tlvs->router_id = (struct te_router_id *) (pnt);
	  pnt += length;
	  break;

	case IPV4_INT_REACHABILITY:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   0   |  I/E  |               Default Metric                  | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Delay Metric                    | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Expense Metric                  | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Error Metric                    | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        ip address                             | 4
	   * +---------------------------------------------------------------+
	   * |                        address mask                           | 4
	   * +---------------------------------------------------------------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_IPV4_INT_REACHABILITY;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IPv4 internal Reachability length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_IPV4_INT_REACHABILITY)
	    {
	      while (length > value_len)
		{
		  ipv4_reach = (struct ipv4_reachability *) pnt;
		  if (!tlvs->ipv4_int_reachs)
		    tlvs->ipv4_int_reachs = list_new ();
		  listnode_add (tlvs->ipv4_int_reachs, ipv4_reach);
		  value_len += 12;
		  pnt += 12;
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case IPV4_EXT_REACHABILITY:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   0   |  I/E  |               Default Metric                  | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Delay Metric                    | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Expense Metric                  | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |   S   |  I/E  |               Error Metric                    | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        ip address                             | 4
	   * +---------------------------------------------------------------+
	   * |                        address mask                           | 4
	   * +---------------------------------------------------------------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_IPV4_EXT_REACHABILITY;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IPv4 external Reachability length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_IPV4_EXT_REACHABILITY)
	    {
	      while (length > value_len)
		{
		  ipv4_reach = (struct ipv4_reachability *) pnt;
		  if (!tlvs->ipv4_ext_reachs)
		    tlvs->ipv4_ext_reachs = list_new ();
		  listnode_add (tlvs->ipv4_ext_reachs, ipv4_reach);
		  value_len += 12;
		  pnt += 12;
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case TE_IPV4_REACHABILITY:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        TE Metric                              | 4
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |  U/D  | sTLV? |               Prefix Mask Len                 | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                           Prefix                              | 0-4
	   * +---------------------------------------------------------------+
	   * |                         sub tlvs                              |
	   * +---------------------------------------------------------------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_TE_IPV4_REACHABILITY;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IPv4 extended Reachability length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  endpnt = pnt + length;
	  if (*expected & TLVFLAG_TE_IPV4_REACHABILITY)
	    {
	      while (length > value_len)
		{
		  te_ipv4_reach = (struct te_ipv4_reachability *) pnt;
		  if ((te_ipv4_reach->control & 0x3F) > IPV4_MAX_BITLEN)
		    {
		      zlog_warn ("ISIS-TLV (%s): invalid IPv4 extended reach"
				 "ability prefix length %d", areatag,
				 te_ipv4_reach->control & 0x3F);
		      retval = ISIS_WARNING;
		      break;
		    }
		  if (!tlvs->te_ipv4_reachs)
		    tlvs->te_ipv4_reachs = list_new ();
		  listnode_add (tlvs->te_ipv4_reachs, te_ipv4_reach);
		  /* this trickery is permitable since no subtlvs are defined */
		  value_len += 5 + ((te_ipv4_reach->control & 0x3F) ?
				    ((((te_ipv4_reach->control & 0x3F) -
				       1) >> 3) + 1) : 0);
		  pnt += 5 + ((te_ipv4_reach->control & 0x3F) ?
		              ((((te_ipv4_reach->control & 0x3F) - 1) >> 3) + 1) : 0);
		}
	    }

	  pnt = endpnt;
	  break;

#ifdef  HAVE_IPV6
	case IPV6_ADDR:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * +                 IP version 6 address                          + 16
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * :                                                               :
	   */
	  *found |= TLVFLAG_IPV6_ADDR;
#ifdef EXTREME_TLV_DEBUG
	  zlog_debug ("ISIS-TLV (%s): IPv6 Address length %d",
		      areatag, length);
#endif /* EXTREME_TLV_DEBUG */
	  if (*expected & TLVFLAG_IPV6_ADDR)
	    {
	      while (length > value_len)
		{
		  ipv6_addr = (struct in6_addr *) pnt;
		  if (!tlvs->ipv6_addrs)
		    tlvs->ipv6_addrs = list_new ();
		  listnode_add (tlvs->ipv6_addrs, ipv6_addr);
		  value_len += 16;
		  pnt += 16;
		}
	    }
	  else
	    {
	      pnt += length;
	    }
	  break;

	case IPV6_REACHABILITY:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                 Default Metric                                | 4 
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                        Control Informantion                   |
	   * +---------------------------------------------------------------+
	   * |                        IPv6 Prefix Length                     |--+
	   * +---------------------------------------------------------------+  |
	   * |                        IPv6 Prefix                            |<-+
	   * +---------------------------------------------------------------+
	   */
	  *found |= TLVFLAG_IPV6_REACHABILITY;
	  endpnt = pnt + length;

	  if (*expected & TLVFLAG_IPV6_REACHABILITY)
	    {
	      while (length > value_len)
		{
		  ipv6_reach = (struct ipv6_reachability *) pnt;
		  if (ipv6_reach->prefix_len > IPV6_MAX_BITLEN)
		    {
		      zlog_warn ("ISIS-TLV (%s): invalid IPv6 extended reach"
				 "ability prefix length %d", areatag,
				 ipv6_reach->prefix_len);
		      retval = ISIS_WARNING;
		      break;
		    }

		  prefix_octets = ((ipv6_reach->prefix_len + 7) / 8);
		  value_len += prefix_octets + 6;
		  pnt += prefix_octets + 6;
		  /* FIXME: sub-tlvs */
		  if (!tlvs->ipv6_reachs)
		    tlvs->ipv6_reachs = list_new ();
		  listnode_add (tlvs->ipv6_reachs, ipv6_reach);
		}
	    }

	  pnt = endpnt;
	  break;
#endif /* HAVE_IPV6 */

	case WAY3_HELLO:
	  /* +---------------------------------------------------------------+
	   * |                  Adjacency state                              | 1
	   * +---------------------------------------------------------------+
	   * |                  Extended Local Circuit ID                    | 4
	   * +---------------------------------------------------------------+
	   * |                  Neighbor System ID (If known)                | 0-8
	   *                                      (probably 6)
	   * +---------------------------------------------------------------+
	   * |                  Neighbor Local Circuit ID (If known)         | 4
	   * +---------------------------------------------------------------+
	   */
	  *found |= TLVFLAG_3WAY_HELLO;
	  if (*expected & TLVFLAG_3WAY_HELLO)
	    {
	      while (length > value_len)
		{
		  /* FIXME: make this work */
/*           Adjacency State (one octet):
              0 = Up
              1 = Initializing
              2 = Down
            Extended Local Circuit ID (four octets)
            Neighbor System ID if known (zero to eight octets)
            Neighbor Extended Local Circuit ID (four octets, if Neighbor
              System ID is present) */
		  pnt += length;
		}
	    }
	  else
	    {
	      pnt += length;
	    }

	  break;
	case GRACEFUL_RESTART:
	  /* +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |         Reserved                      |  SA   |  RA   |  RR   | 1
	   * +-------+-------+-------+-------+-------+-------+-------+-------+
	   * |                          Remaining Time                       | 2
	   * +---------------------------------------------------------------+
	   * |                Restarting Neighbor ID (If known)              | 0-8
	   * +---------------------------------------------------------------+
	   */
	  *found |= TLVFLAG_GRACEFUL_RESTART;
	  if (*expected & TLVFLAG_GRACEFUL_RESTART)
	    {
	      /* FIXME: make this work */
	    }
	  pnt += length;
	  break;

	default:
	  zlog_warn ("ISIS-TLV (%s): unsupported TLV type %d, length %d",
		     areatag, type, length);

	  retval = ISIS_WARNING;
	  pnt += length;
	  break;
	}
    }

  return retval;
}

int
add_tlv (u_char tag, u_char len, u_char * value, struct stream *stream)
{
  if ((stream_get_size (stream) - stream_get_endp (stream)) <
      (((unsigned)len) + 2))
    {
      zlog_warn ("No room for TLV of type %d "
                 "(total size %d available %d required %d)",
                 tag, (int)stream_get_size (stream),
                 (int)(stream_get_size (stream) - stream_get_endp (stream)),
                 len+2);
      return ISIS_WARNING;
    }

  stream_putc (stream, tag);	/* TAG */
  stream_putc (stream, len);	/* LENGTH */
  stream_put (stream, value, (int) len);	/* VALUE */

#ifdef EXTREME_DEBUG
  zlog_debug ("Added TLV %d len %d", tag, len);
#endif /* EXTREME DEBUG */
  return ISIS_OK;
}

int
tlv_add_area_addrs (struct list *area_addrs, struct stream *stream)
{
  struct listnode *node;
  struct area_addr *area_addr;

  u_char value[255];
  u_char *pos = value;

  for (ALL_LIST_ELEMENTS_RO (area_addrs, node, area_addr))
    {
      if (pos - value + area_addr->addr_len > 255)
	goto err;
      *pos = area_addr->addr_len;
      pos++;
      memcpy (pos, area_addr->area_addr, (int) area_addr->addr_len);
      pos += area_addr->addr_len;
    }

  return add_tlv (AREA_ADDRESSES, pos - value, value, stream);

err:
  zlog_warn ("tlv_add_area_addrs(): TLV longer than 255");
  return ISIS_WARNING;
}

int
tlv_add_is_neighs (struct list *is_neighs, struct stream *stream)
{
  struct listnode *node;
  struct is_neigh *is_neigh;
  u_char value[255];
  u_char *pos = value;
  int retval;

  *pos = 0;			/*is_neigh->virtual; */
  pos++;

  for (ALL_LIST_ELEMENTS_RO (is_neighs, node, is_neigh))
    {
      if (pos - value + IS_NEIGHBOURS_LEN > 255)
	{
	  retval = add_tlv (IS_NEIGHBOURS, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      *pos = is_neigh->metrics.metric_default;
      pos++;
      *pos = is_neigh->metrics.metric_delay;
      pos++;
      *pos = is_neigh->metrics.metric_expense;
      pos++;
      *pos = is_neigh->metrics.metric_error;
      pos++;
      memcpy (pos, is_neigh->neigh_id, ISIS_SYS_ID_LEN + 1);
      pos += ISIS_SYS_ID_LEN + 1;
    }

  return add_tlv (IS_NEIGHBOURS, pos - value, value, stream);
}

int
tlv_add_te_is_neighs (struct list *te_is_neighs, struct stream *stream)
{
  struct listnode *node;
  struct te_is_neigh *te_is_neigh;
  u_char value[255];
  u_char *pos = value;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (te_is_neighs, node, te_is_neigh))
    {
      /* FIXME: This will be wrong if we are going to add TE sub TLVs. */
      if (pos - value + IS_NEIGHBOURS_LEN > 255)
        {
          retval = add_tlv (TE_IS_NEIGHBOURS, pos - value, value, stream);
          if (retval != ISIS_OK)
            return retval;
          pos = value;
        }
      
      memcpy (pos, te_is_neigh->neigh_id, ISIS_SYS_ID_LEN + 1);
      pos += ISIS_SYS_ID_LEN + 1;
      memcpy (pos, te_is_neigh->te_metric, 3);
      pos += 3;
      /* Sub TLVs length. */
      *pos = 0;
      pos++;
    }

  return add_tlv (TE_IS_NEIGHBOURS, pos - value, value, stream);
}

int
tlv_add_lan_neighs (struct list *lan_neighs, struct stream *stream)
{
  struct listnode *node;
  u_char *snpa;
  u_char value[255];
  u_char *pos = value;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (lan_neighs, node, snpa))
    {
      if (pos - value + ETH_ALEN > 255)
	{
	  retval = add_tlv (LAN_NEIGHBOURS, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      memcpy (pos, snpa, ETH_ALEN);
      pos += ETH_ALEN;
    }

  return add_tlv (LAN_NEIGHBOURS, pos - value, value, stream);
}

int
tlv_add_nlpid (struct nlpids *nlpids, struct stream *stream)
{
  return add_tlv (PROTOCOLS_SUPPORTED, nlpids->count, nlpids->nlpids, stream);
}

int
tlv_add_authinfo (u_char auth_type, u_char auth_len, u_char *auth_value,
		  struct stream *stream)
{
  u_char value[255];
  u_char *pos = value;
  *pos++ = auth_type;
  memcpy (pos, auth_value, auth_len);

  return add_tlv (AUTH_INFO, auth_len + 1, value, stream);
}

int
tlv_add_checksum (struct checksum *checksum, struct stream *stream)
{
  u_char value[255];
  u_char *pos = value;
  return add_tlv (CHECKSUM, pos - value, value, stream);
}

int
tlv_add_ip_addrs (struct list *ip_addrs, struct stream *stream)
{
  struct listnode *node;
  struct prefix_ipv4 *ipv4;
  u_char value[255];
  u_char *pos = value;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (ip_addrs, node, ipv4))
    {
      if (pos - value + IPV4_MAX_BYTELEN > 255)
	{
	  retval = add_tlv (IPV4_ADDR, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      *(u_int32_t *) pos = ipv4->prefix.s_addr;
      pos += IPV4_MAX_BYTELEN;
    }

  return add_tlv (IPV4_ADDR, pos - value, value, stream);
}

/* Used to add TLV containing just one IPv4 address - either IPv4 address TLV
 * (in case of LSP) or TE router ID TLV. */
int
tlv_add_in_addr (struct in_addr *addr, struct stream *stream, u_char tag)
{
  u_char value[255];
  u_char *pos = value;
  
  memcpy (pos, addr, IPV4_MAX_BYTELEN);
  pos += IPV4_MAX_BYTELEN;

  return add_tlv (tag, pos - value, value, stream);
}

int
tlv_add_dynamic_hostname (struct hostname *hostname, struct stream *stream)
{
  return add_tlv (DYNAMIC_HOSTNAME, hostname->namelen, hostname->name,
		  stream);
}

int
tlv_add_lsp_entries (struct list *lsps, struct stream *stream)
{
  struct listnode *node;
  struct isis_lsp *lsp;
  u_char value[255];
  u_char *pos = value;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (lsps, node, lsp))
    {
      if (pos - value + LSP_ENTRIES_LEN > 255)
	{
	  retval = add_tlv (LSP_ENTRIES, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      *((u_int16_t *) pos) = lsp->lsp_header->rem_lifetime;
      pos += 2;
      memcpy (pos, lsp->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 2);
      pos += ISIS_SYS_ID_LEN + 2;
      *((u_int32_t *) pos) = lsp->lsp_header->seq_num;
      pos += 4;
      *((u_int16_t *) pos) = lsp->lsp_header->checksum;
      pos += 2;
    }

  return add_tlv (LSP_ENTRIES, pos - value, value, stream);
}

int
tlv_add_ipv4_reachs (struct list *ipv4_reachs, struct stream *stream)
{
  struct listnode *node;
  struct ipv4_reachability *reach;
  u_char value[255];
  u_char *pos = value;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (ipv4_reachs, node, reach))
    {
      if (pos - value + IPV4_REACH_LEN > 255)
	{
	  retval =
	    add_tlv (IPV4_INT_REACHABILITY, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      *pos = reach->metrics.metric_default;
      pos++;
      *pos = reach->metrics.metric_delay;
      pos++;
      *pos = reach->metrics.metric_expense;
      pos++;
      *pos = reach->metrics.metric_error;
      pos++;
      *(u_int32_t *) pos = reach->prefix.s_addr;
      pos += IPV4_MAX_BYTELEN;
      *(u_int32_t *) pos = reach->mask.s_addr;
      pos += IPV4_MAX_BYTELEN;
    }

  return add_tlv (IPV4_INT_REACHABILITY, pos - value, value, stream);
}

int
tlv_add_te_ipv4_reachs (struct list *te_ipv4_reachs, struct stream *stream)
{
  struct listnode *node;
  struct te_ipv4_reachability *te_reach;
  u_char value[255];
  u_char *pos = value;
  u_char prefix_size;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (te_ipv4_reachs, node, te_reach))
    {
      prefix_size = ((((te_reach->control & 0x3F) - 1) >> 3) + 1);

      if (pos - value + (5 + prefix_size) > 255)
	{
	  retval =
	    add_tlv (TE_IPV4_REACHABILITY, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      *(u_int32_t *) pos = te_reach->te_metric;
      pos += 4;
      *pos = te_reach->control;
      pos++;
      memcpy (pos, &te_reach->prefix_start, prefix_size);
      pos += prefix_size;
    }

  return add_tlv (TE_IPV4_REACHABILITY, pos - value, value, stream);
}

#ifdef HAVE_IPV6
int
tlv_add_ipv6_addrs (struct list *ipv6_addrs, struct stream *stream)
{
  struct listnode *node;
  struct prefix_ipv6 *ipv6;
  u_char value[255];
  u_char *pos = value;
  int retval;

  for (ALL_LIST_ELEMENTS_RO (ipv6_addrs, node, ipv6))
    {
      if (pos - value + IPV6_MAX_BYTELEN > 255)
	{
	  retval = add_tlv (IPV6_ADDR, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      memcpy (pos, ipv6->prefix.s6_addr, IPV6_MAX_BYTELEN);
      pos += IPV6_MAX_BYTELEN;
    }

  return add_tlv (IPV6_ADDR, pos - value, value, stream);
}

int
tlv_add_ipv6_reachs (struct list *ipv6_reachs, struct stream *stream)
{
  struct listnode *node;
  struct ipv6_reachability *ip6reach;
  u_char value[255];
  u_char *pos = value;
  int retval, prefix_octets;

  for (ALL_LIST_ELEMENTS_RO (ipv6_reachs, node, ip6reach))
    {
      if (pos - value + IPV6_MAX_BYTELEN + 6 > 255)
	{
	  retval = add_tlv (IPV6_REACHABILITY, pos - value, value, stream);
	  if (retval != ISIS_OK)
	    return retval;
	  pos = value;
	}
      *(uint32_t *) pos = ip6reach->metric;
      pos += 4;
      *pos = ip6reach->control_info;
      pos++;
      prefix_octets = ((ip6reach->prefix_len + 7) / 8);
      *pos = ip6reach->prefix_len;
      pos++;
      memcpy (pos, ip6reach->prefix, prefix_octets);
      pos += prefix_octets;
    }

  return add_tlv (IPV6_REACHABILITY, pos - value, value, stream);
}
#endif /* HAVE_IPV6 */

int
tlv_add_padding (struct stream *stream)
{
  int fullpads, i, left;

  /*
   * How many times can we add full padding ?
   */
  fullpads = (stream_get_size (stream) - stream_get_endp (stream)) / 257;
  for (i = 0; i < fullpads; i++)
    {
      if (!stream_putc (stream, (u_char) PADDING))	/* TAG */
	goto err;
      if (!stream_putc (stream, (u_char) 255))	/* LENGHT */
	goto err;
      stream_put (stream, NULL, 255);		/* zero padding */
    }

  left = stream_get_size (stream) - stream_get_endp (stream);

  if (left < 2)
    return ISIS_OK;

  if (left == 2)
    {
      stream_putc (stream, PADDING);
      stream_putc (stream, 0);
      return ISIS_OK;
    }

  stream_putc (stream, PADDING);
  stream_putc (stream, left - 2);
  stream_put (stream, NULL, left-2);

  return ISIS_OK;

err:
  zlog_warn ("tlv_add_padding(): no room for tlv");
  return ISIS_WARNING;
}