forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcep_msg_objects_encoding.c
1720 lines (1499 loc) · 55.2 KB
/
pcep_msg_objects_encoding.c
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of the PCEPlib, a PCEP protocol library.
*
* Copyright (C) 2020 Volta Networks https://voltanet.io/
*
* Author : Brady Johnson <brady@voltanet.io>
*
*/
/*
* Encoding and decoding for PCEP Objects.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include "pcep_msg_objects.h"
#include "pcep_msg_encoding.h"
#include "pcep_utils_logging.h"
#include "pcep_utils_memory.h"
void write_object_header(struct pcep_object_header *object_hdr,
uint16_t object_length, uint8_t *buf);
void pcep_decode_object_hdr(const uint8_t *obj_buf,
struct pcep_object_header *obj_hdr);
void set_ro_subobj_fields(struct pcep_object_ro_subobj *subobj, bool flag_l,
uint8_t subobj_type);
/*
* forward declarations for initialize_object_encoders()
*/
uint16_t pcep_encode_obj_open(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_rp(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_nopath(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_endpoints(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_association(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_bandwidth(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_metric(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_ro(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_lspa(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_svec(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_notify(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_error(struct pcep_object_header *error,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_close(struct pcep_object_header *close,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_srp(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_lsp(struct pcep_object_header *obj,
struct pcep_versioning *versioning, uint8_t *buf);
uint16_t pcep_encode_obj_vendor_info(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_inter_layer(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_switch_layer(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_req_adap_cap(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_server_ind(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
uint16_t pcep_encode_obj_objective_function(struct pcep_object_header *obj,
struct pcep_versioning *versioning,
uint8_t *buf);
typedef uint16_t (*object_encoder_funcptr)(struct pcep_object_header *,
struct pcep_versioning *versioning,
uint8_t *buf);
#define MAX_OBJECT_ENCODER_INDEX 64
#define PCEP_ENCODERS_ARGS \
struct pcep_object_header *, struct pcep_versioning *versioning, \
uint8_t *buf
uint16_t (*const object_encoders[MAX_OBJECT_ENCODER_INDEX])(
PCEP_ENCODERS_ARGS) = {
[PCEP_OBJ_CLASS_OPEN] = pcep_encode_obj_open,
[PCEP_OBJ_CLASS_RP] = pcep_encode_obj_rp,
[PCEP_OBJ_CLASS_NOPATH] = pcep_encode_obj_nopath,
[PCEP_OBJ_CLASS_ENDPOINTS] = pcep_encode_obj_endpoints,
[PCEP_OBJ_CLASS_BANDWIDTH] = pcep_encode_obj_bandwidth,
[PCEP_OBJ_CLASS_METRIC] = pcep_encode_obj_metric,
[PCEP_OBJ_CLASS_ERO] = pcep_encode_obj_ro,
[PCEP_OBJ_CLASS_RRO] = pcep_encode_obj_ro,
[PCEP_OBJ_CLASS_LSPA] = pcep_encode_obj_lspa,
[PCEP_OBJ_CLASS_IRO] = pcep_encode_obj_ro,
[PCEP_OBJ_CLASS_SVEC] = pcep_encode_obj_svec,
[PCEP_OBJ_CLASS_NOTF] = pcep_encode_obj_notify,
[PCEP_OBJ_CLASS_ERROR] = pcep_encode_obj_error,
[PCEP_OBJ_CLASS_CLOSE] = pcep_encode_obj_close,
[PCEP_OBJ_CLASS_LSP] = pcep_encode_obj_lsp,
[PCEP_OBJ_CLASS_SRP] = pcep_encode_obj_srp,
[PCEP_OBJ_CLASS_ASSOCIATION] = pcep_encode_obj_association,
[PCEP_OBJ_CLASS_INTER_LAYER] = pcep_encode_obj_inter_layer,
[PCEP_OBJ_CLASS_SWITCH_LAYER] = pcep_encode_obj_switch_layer,
[PCEP_OBJ_CLASS_REQ_ADAP_CAP] = pcep_encode_obj_req_adap_cap,
[PCEP_OBJ_CLASS_SERVER_IND] = pcep_encode_obj_server_ind,
[PCEP_OBJ_CLASS_VENDOR_INFO] = pcep_encode_obj_vendor_info,
[PCEP_OBJ_CLASS_OF] = pcep_encode_obj_objective_function,
};
/*
* forward declarations for initialize_object_decoders()
*/
struct pcep_object_header *pcep_decode_obj_open(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_rp(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_nopath(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_endpoints(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_association(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_bandwidth(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_metric(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_ro(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_lspa(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_svec(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_notify(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_error(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_close(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_srp(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *pcep_decode_obj_lsp(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_vendor_info(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_inter_layer(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_switch_layer(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_req_adap_cap(struct pcep_object_header *hdr,
const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_server_ind(struct pcep_object_header *hdr, const uint8_t *buf);
struct pcep_object_header *
pcep_decode_obj_objective_function(struct pcep_object_header *hdr,
const uint8_t *buf);
typedef struct pcep_object_header *(*object_decoder_funcptr)(
struct pcep_object_header *, const uint8_t *buf);
#define PCEP_DECODERS_ARGS struct pcep_object_header *, const uint8_t *buf
struct pcep_object_header *(*const object_decoders[MAX_OBJECT_ENCODER_INDEX])(
PCEP_DECODERS_ARGS) = {
[PCEP_OBJ_CLASS_OPEN] = pcep_decode_obj_open,
[PCEP_OBJ_CLASS_RP] = pcep_decode_obj_rp,
[PCEP_OBJ_CLASS_NOPATH] = pcep_decode_obj_nopath,
[PCEP_OBJ_CLASS_ENDPOINTS] = pcep_decode_obj_endpoints,
[PCEP_OBJ_CLASS_BANDWIDTH] = pcep_decode_obj_bandwidth,
[PCEP_OBJ_CLASS_METRIC] = pcep_decode_obj_metric,
[PCEP_OBJ_CLASS_ERO] = pcep_decode_obj_ro,
[PCEP_OBJ_CLASS_RRO] = pcep_decode_obj_ro,
[PCEP_OBJ_CLASS_LSPA] = pcep_decode_obj_lspa,
[PCEP_OBJ_CLASS_IRO] = pcep_decode_obj_ro,
[PCEP_OBJ_CLASS_SVEC] = pcep_decode_obj_svec,
[PCEP_OBJ_CLASS_NOTF] = pcep_decode_obj_notify,
[PCEP_OBJ_CLASS_ERROR] = pcep_decode_obj_error,
[PCEP_OBJ_CLASS_CLOSE] = pcep_decode_obj_close,
[PCEP_OBJ_CLASS_LSP] = pcep_decode_obj_lsp,
[PCEP_OBJ_CLASS_SRP] = pcep_decode_obj_srp,
[PCEP_OBJ_CLASS_ASSOCIATION] = pcep_decode_obj_association,
[PCEP_OBJ_CLASS_INTER_LAYER] = pcep_decode_obj_inter_layer,
[PCEP_OBJ_CLASS_SWITCH_LAYER] = pcep_decode_obj_switch_layer,
[PCEP_OBJ_CLASS_REQ_ADAP_CAP] = pcep_decode_obj_req_adap_cap,
[PCEP_OBJ_CLASS_SERVER_IND] = pcep_decode_obj_server_ind,
[PCEP_OBJ_CLASS_VENDOR_INFO] = pcep_decode_obj_vendor_info,
[PCEP_OBJ_CLASS_OF] = pcep_decode_obj_objective_function,
};
/* Object lengths, including the Object Header.
* Used by pcep_object_get_length() and pcep_object_has_tlvs() */
static uint8_t pcep_object_class_lengths[] = {
0, /* Object class 0 unused */
8, /* PCEP_OBJ_CLASS_OPEN = 1 */
12, /* PCEP_OBJ_CLASS_RP = 2 */
16, /* PCEP_OBJ_CLASS_NOPATH = 3, includes 8 for mandatory TLV */
0, /* PCEP_OBJ_CLASS_ENDPOINTS = 4, could be ipv4 or ipv6, setting to 0
*/
8, /* PCEP_OBJ_CLASS_BANDWIDTH = 5 */
12, /* PCEP_OBJ_CLASS_METRIC = 6 */
0, /* PCEP_OBJ_CLASS_ERO = 7, setting 0, ROs cannot have TLVs */
0, /* PCEP_OBJ_CLASS_RRO = 8, setting 0, ROs cannot have TLVs */
20, /* PCEP_OBJ_CLASS_LSPA = 9 */
0, /* PCEP_OBJ_CLASS_IRO = 10, setting 0, ROs cannot have TLVs */
0, /* PCEP_OBJ_CLASS_SVEC = 11, SVECs cannot have TLVs */
8, /* PCEP_OBJ_CLASS_NOTF = 12 */
8, /* PCEP_OBJ_CLASS_ERROR = 13 */
0, /* Object class 14 unused */
8, /* PCEP_OBJ_CLASS_CLOSE = 15 */
0, 0, 0, 0, 0, /* Object classes 16 - 20 are not used */
8, /* PCEP_OBJ_CLASS_OF = 21 */
0, 0, 0, 0, 0, /* Object classes 22 - 26 are not used */
0, 0, 0, 0, 0, /* Object classes 27 - 31 are not used */
8, /* PCEP_OBJ_CLASS_LSP = 32 */
12, /* PCEP_OBJ_CLASS_SRP = 33 */
12, /* PCEP_OBJ_CLASS_VENDOR_INFO = 34 */
0, /* Object class 35 unused */
0, /* PCEP_OBJ_CLASS_INTER_LAYER = 36, cannot have TLVs */
0, /* PCEP_OBJ_CLASS_SWITCH_LAYER = 37, cannot have TLVs */
0, /* PCEP_OBJ_CLASS_REQ_ADAP_CAP = 38, cannot have TLVs*/
8, /* PCEP_OBJ_CLASS_SERVER_IND = 39 */
0, /* PCEP_OBJ_CLASS_ASSOCIATION = 40, cannot have TLVs */
};
/*
* The TLVs can have strange length values, since they do not include padding in
* the TLV header length, but that extra padding must be taken into account by
* the enclosing object by rounding up to the next 4 byte boundary.
* Example returned lengths:
* normalize_length(4) = 4, normalize_length(5) = 8, normalize_length(6)
* = 8, normalize_length(7) = 8, normalize_length(8) = 8
* normalize_length(9) = 12, normalize_length(10) = 12, normalize_length(11) =
* 12, normalize_length(12) = 12, normalize_length(13) = 13...
*/
uint16_t normalize_pcep_tlv_length(uint16_t length)
{
return (length % 4 == 0) ? length : (length + (4 - (length % 4)));
}
/*
* Encoding functions
*/
uint16_t pcep_encode_object(struct pcep_object_header *object_hdr,
struct pcep_versioning *versioning, uint8_t *buf)
{
if (object_hdr->object_class >= MAX_OBJECT_ENCODER_INDEX) {
pcep_log(LOG_INFO,
"%s: Cannot encode unknown Object class [%d]",
__func__, object_hdr->object_class);
return 0;
}
object_encoder_funcptr obj_encoder =
object_encoders[object_hdr->object_class];
if (obj_encoder == NULL) {
pcep_log(LOG_INFO,
"%s: No object encoder found for Object class [%d]",
__func__, object_hdr->object_class);
return 0;
}
uint16_t object_length = OBJECT_HEADER_LENGTH
+ obj_encoder(object_hdr, versioning,
buf + OBJECT_HEADER_LENGTH);
double_linked_list_node *node =
(object_hdr->tlv_list == NULL ? NULL
: object_hdr->tlv_list->head);
for (; node != NULL; node = node->next_node) {
/* Returns the length of the TLV, including the TLV header */
object_length += pcep_encode_tlv(
(struct pcep_object_tlv_header *)node->data, versioning,
buf + object_length);
}
object_length = normalize_pcep_tlv_length(object_length);
write_object_header(object_hdr, object_length, buf);
object_hdr->encoded_object = buf;
object_hdr->encoded_object_length = object_length;
return object_length;
}
/* Object Header
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Object-Class | OT |Res|P|I| Object Length (bytes) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | |
* // (Object body) //
* | |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
*/
void write_object_header(struct pcep_object_header *object_hdr,
uint16_t object_length, uint8_t *buf)
{
buf[0] = object_hdr->object_class;
buf[1] = ((object_hdr->object_type << 4)
| (object_hdr->flag_p ? OBJECT_HEADER_FLAG_P : 0x00)
| (object_hdr->flag_i ? OBJECT_HEADER_FLAG_I : 0x00));
uint16_t net_order_length = htons(object_length);
memcpy(buf + 2, &net_order_length, sizeof(net_order_length));
}
/*
* Functions to encode objects
* - they will be passed a pointer to a buffer to write the object body,
* which is past the object header.
* - they should return the object body length, not including the object header
* length.
*/
uint16_t pcep_encode_obj_open(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_open *open = (struct pcep_object_open *)hdr;
obj_body_buf[0] = (open->open_version << 5) & 0xe0;
obj_body_buf[1] = open->open_keepalive;
obj_body_buf[2] = open->open_deadtimer;
obj_body_buf[3] = open->open_sid;
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_rp(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_rp *rp = (struct pcep_object_rp *)hdr;
obj_body_buf[3] = ((rp->flag_strict ? OBJECT_RP_FLAG_O : 0x00)
| (rp->flag_bidirectional ? OBJECT_RP_FLAG_B : 0x00)
| (rp->flag_reoptimization ? OBJECT_RP_FLAG_R : 0x00)
| (rp->flag_of ? OBJECT_RP_FLAG_OF : 0x00)
| (rp->priority & 0x07));
uint32_t *uint32_ptr = (uint32_t *)(obj_body_buf + 4);
*uint32_ptr = htonl(rp->request_id);
return LENGTH_2WORDS;
}
uint16_t pcep_encode_obj_notify(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_notify *notify = (struct pcep_object_notify *)hdr;
obj_body_buf[2] = notify->notification_type;
obj_body_buf[3] = notify->notification_value;
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_nopath(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_nopath *nopath = (struct pcep_object_nopath *)hdr;
obj_body_buf[0] = nopath->ni;
obj_body_buf[1] = ((nopath->flag_c) ? OBJECT_NOPATH_FLAG_C : 0x00);
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_association(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
uint16_t *uint16_ptr = (uint16_t *)obj_body_buf;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
if (hdr->object_type == PCEP_OBJ_TYPE_ASSOCIATION_IPV4) {
struct pcep_object_association_ipv4 *ipv4 =
(struct pcep_object_association_ipv4 *)hdr;
obj_body_buf[3] =
(ipv4->R_flag ? OBJECT_ASSOCIATION_FLAG_R : 0x00);
uint16_ptr[2] = htons(ipv4->association_type);
uint16_ptr[3] = htons(ipv4->association_id);
uint32_ptr[2] = ipv4->src.s_addr;
return LENGTH_3WORDS;
} else {
struct pcep_object_association_ipv6 *ipv6 =
(struct pcep_object_association_ipv6 *)hdr;
obj_body_buf[3] =
(ipv6->R_flag ? OBJECT_ASSOCIATION_FLAG_R : 0x00);
uint16_ptr[2] = htons(ipv6->association_type);
uint16_ptr[3] = htons(ipv6->association_id);
memcpy(uint32_ptr, &ipv6->src, sizeof(struct in6_addr));
return LENGTH_6WORDS;
}
}
uint16_t pcep_encode_obj_endpoints(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
if (hdr->object_type == PCEP_OBJ_TYPE_ENDPOINT_IPV4) {
struct pcep_object_endpoints_ipv4 *ipv4 =
(struct pcep_object_endpoints_ipv4 *)hdr;
uint32_ptr[0] = ipv4->src_ipv4.s_addr;
uint32_ptr[1] = ipv4->dst_ipv4.s_addr;
return LENGTH_2WORDS;
} else {
struct pcep_object_endpoints_ipv6 *ipv6 =
(struct pcep_object_endpoints_ipv6 *)hdr;
memcpy(uint32_ptr, &ipv6->src_ipv6, sizeof(struct in6_addr));
memcpy(&uint32_ptr[4], &ipv6->dst_ipv6,
sizeof(struct in6_addr));
return LENGTH_8WORDS;
}
}
uint16_t pcep_encode_obj_bandwidth(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_bandwidth *bandwidth =
(struct pcep_object_bandwidth *)hdr;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
/* Seems like the compiler doesn't correctly copy the float, so memcpy()
* it */
memcpy(uint32_ptr, &(bandwidth->bandwidth), sizeof(uint32_t));
*uint32_ptr = htonl(*uint32_ptr);
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_metric(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_metric *metric = (struct pcep_object_metric *)hdr;
obj_body_buf[2] = ((metric->flag_c ? OBJECT_METRIC_FLAC_C : 0x00)
| (metric->flag_b ? OBJECT_METRIC_FLAC_B : 0x00));
obj_body_buf[3] = metric->type;
uint32_t *uint32_ptr = (uint32_t *)(obj_body_buf + 4);
/* Seems like the compiler doesn't correctly copy the float, so memcpy()
* it */
memcpy(uint32_ptr, &(metric->value), sizeof(uint32_t));
*uint32_ptr = htonl(*uint32_ptr);
return LENGTH_2WORDS;
}
uint16_t pcep_encode_obj_lspa(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_lspa *lspa = (struct pcep_object_lspa *)hdr;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
uint32_ptr[0] = htonl(lspa->lspa_exclude_any);
uint32_ptr[1] = htonl(lspa->lspa_include_any);
uint32_ptr[2] = htonl(lspa->lspa_include_all);
obj_body_buf[12] = lspa->setup_priority;
obj_body_buf[13] = lspa->holding_priority;
obj_body_buf[14] =
(lspa->flag_local_protection ? OBJECT_LSPA_FLAG_L : 0x00);
return LENGTH_4WORDS;
}
uint16_t pcep_encode_obj_svec(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_svec *svec = (struct pcep_object_svec *)hdr;
obj_body_buf[3] =
((svec->flag_srlg_diverse ? OBJECT_SVEC_FLAG_S : 0x00)
| (svec->flag_node_diverse ? OBJECT_SVEC_FLAG_N : 0x00)
| (svec->flag_link_diverse ? OBJECT_SVEC_FLAG_L : 0x00));
if (svec->request_id_list == NULL) {
return LENGTH_1WORD;
}
int index = 1;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
double_linked_list_node *node = svec->request_id_list->head;
for (; node != NULL; node = node->next_node) {
uint32_ptr[index++] = htonl(*((uint32_t *)(node->data)));
}
return LENGTH_1WORD
+ (svec->request_id_list->num_entries * sizeof(uint32_t));
}
uint16_t pcep_encode_obj_error(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_error *error = (struct pcep_object_error *)hdr;
obj_body_buf[2] = error->error_type;
obj_body_buf[3] = error->error_value;
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_close(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_close *close = (struct pcep_object_close *)hdr;
obj_body_buf[3] = close->reason;
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_srp(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_srp *srp = (struct pcep_object_srp *)hdr;
obj_body_buf[3] = (srp->flag_lsp_remove ? OBJECT_SRP_FLAG_R : 0x00);
uint32_t *uint32_ptr = (uint32_t *)(obj_body_buf + 4);
*uint32_ptr = htonl(srp->srp_id_number);
return LENGTH_2WORDS;
}
uint16_t pcep_encode_obj_lsp(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_lsp *lsp = (struct pcep_object_lsp *)hdr;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
uint32_ptr[0] = htonl((lsp->plsp_id << 12) & 0xfffff000);
obj_body_buf[3] = ((lsp->flag_c ? OBJECT_LSP_FLAG_C : 0x00)
| ((lsp->operational_status << 4) & 0x70)
| (lsp->flag_a ? OBJECT_LSP_FLAG_A : 0x00)
| (lsp->flag_r ? OBJECT_LSP_FLAG_R : 0x00)
| (lsp->flag_s ? OBJECT_LSP_FLAG_S : 0x00)
| (lsp->flag_d ? OBJECT_LSP_FLAG_D : 0x00));
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_vendor_info(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_vendor_info *obj =
(struct pcep_object_vendor_info *)hdr;
uint32_t *uint32_ptr = (uint32_t *)obj_body_buf;
uint32_ptr[0] = htonl(obj->enterprise_number);
uint32_ptr[1] = htonl(obj->enterprise_specific_info);
return LENGTH_2WORDS;
}
uint16_t pcep_encode_obj_inter_layer(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_inter_layer *obj =
(struct pcep_object_inter_layer *)hdr;
obj_body_buf[3] = ((obj->flag_i ? OBJECT_INTER_LAYER_FLAG_I : 0x00)
| (obj->flag_m ? OBJECT_INTER_LAYER_FLAG_M : 0x00)
| (obj->flag_t ? OBJECT_INTER_LAYER_FLAG_T : 0x00));
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_switch_layer(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_switch_layer *obj =
(struct pcep_object_switch_layer *)hdr;
uint8_t buf_index = 0;
double_linked_list_node *node = obj->switch_layer_rows->head;
while (node != NULL) {
struct pcep_object_switch_layer_row *row = node->data;
if (row == NULL) {
break;
}
obj_body_buf[buf_index] = row->lsp_encoding_type;
obj_body_buf[buf_index + 1] = row->switching_type;
obj_body_buf[buf_index + 3] =
(row->flag_i ? OBJECT_SWITCH_LAYER_FLAG_I : 0x00);
buf_index += LENGTH_1WORD;
}
return buf_index;
}
uint16_t pcep_encode_obj_req_adap_cap(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_req_adap_cap *obj =
(struct pcep_object_req_adap_cap *)hdr;
obj_body_buf[0] = obj->switching_capability;
obj_body_buf[1] = obj->encoding;
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_server_ind(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_server_indication *obj =
(struct pcep_object_server_indication *)hdr;
obj_body_buf[0] = obj->switching_capability;
obj_body_buf[1] = obj->encoding;
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_objective_function(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_objective_function *obj =
(struct pcep_object_objective_function *)hdr;
uint16_t *uint16_ptr = (uint16_t *)obj_body_buf;
*uint16_ptr = htons(obj->of_code);
return LENGTH_1WORD;
}
uint16_t pcep_encode_obj_ro(struct pcep_object_header *hdr,
struct pcep_versioning *versioning,
uint8_t *obj_body_buf)
{
(void)versioning;
struct pcep_object_ro *ro = (struct pcep_object_ro *)hdr;
if (ro == NULL || ro->sub_objects == NULL) {
return 0;
}
/* RO Subobject format
*
* 0 1
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------//----------------+
* |L| Type | Length | (Subobject contents) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------//----------------+
*/
uint16_t index = 0;
double_linked_list_node *node = ro->sub_objects->head;
for (; node != NULL; node = node->next_node) {
struct pcep_object_ro_subobj *ro_subobj = node->data;
obj_body_buf[index++] =
((ro_subobj->flag_subobj_loose_hop ? 0x80 : 0x00)
| (ro_subobj->ro_subobj_type));
/* The length will be written below, depending on the subobj
* type */
uint8_t *length_ptr = &(obj_body_buf[index++]);
uint32_t *uint32_ptr = (uint32_t *)(obj_body_buf + index);
/* - The index has already been incremented past the header,
* and now points to the ro_subobj body. Below it just needs
* to be incremented past the body.
*
* - Each section below needs to write the total length,
* including the 2 byte subobj header. */
switch (ro_subobj->ro_subobj_type) {
case RO_SUBOBJ_TYPE_IPV4: {
struct pcep_ro_subobj_ipv4 *ipv4 =
(struct pcep_ro_subobj_ipv4 *)ro_subobj;
uint32_ptr[0] = ipv4->ip_addr.s_addr;
index += LENGTH_1WORD;
obj_body_buf[index++] = ipv4->prefix_length;
obj_body_buf[index++] =
(ipv4->flag_local_protection
? OBJECT_SUBOBJ_IP_FLAG_LOCAL_PROT
: 0x00);
*length_ptr = LENGTH_2WORDS;
} break;
case RO_SUBOBJ_TYPE_IPV6: {
struct pcep_ro_subobj_ipv6 *ipv6 =
(struct pcep_ro_subobj_ipv6 *)ro_subobj;
encode_ipv6(&ipv6->ip_addr, uint32_ptr);
index += LENGTH_4WORDS;
obj_body_buf[index++] = ipv6->prefix_length;
obj_body_buf[index++] =
(ipv6->flag_local_protection
? OBJECT_SUBOBJ_IP_FLAG_LOCAL_PROT
: 0x00);
*length_ptr = LENGTH_5WORDS;
} break;
case RO_SUBOBJ_TYPE_LABEL: {
struct pcep_ro_subobj_32label *label =
(struct pcep_ro_subobj_32label *)ro_subobj;
obj_body_buf[index++] =
(label->flag_global_label
? OBJECT_SUBOBJ_LABEL_FLAG_GLOGAL
: 0x00);
obj_body_buf[index++] = label->class_type;
uint32_ptr = (uint32_t *)(obj_body_buf + index);
*uint32_ptr = htonl(label->label);
*length_ptr = LENGTH_2WORDS;
index += LENGTH_1WORD;
} break;
case RO_SUBOBJ_TYPE_UNNUM: {
struct pcep_ro_subobj_unnum *unum =
(struct pcep_ro_subobj_unnum *)ro_subobj;
index += 2; /* increment past 2 reserved bytes */
uint32_ptr = (uint32_t *)(obj_body_buf + index);
uint32_ptr[0] = unum->router_id.s_addr;
uint32_ptr[1] = htonl(unum->interface_id);
*length_ptr = LENGTH_3WORDS;
index += LENGTH_2WORDS;
} break;
case RO_SUBOBJ_TYPE_ASN: {
struct pcep_ro_subobj_asn *asn =
(struct pcep_ro_subobj_asn *)ro_subobj;
uint16_t *uint16_ptr =
(uint16_t *)(obj_body_buf + index);
*uint16_ptr = htons(asn->asn);
*length_ptr = LENGTH_1WORD;
index += 2;
} break;
case RO_SUBOBJ_TYPE_SR: {
/* SR-ERO subobject format
*
* 0 1 2 3 0 1 2 3 4
* 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |L| Type=36 | Length | NT | Flags
* |F|S|C|M|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | SID (optional) |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* // NAI (variable, optional) //
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
struct pcep_ro_subobj_sr *sr_subobj =
(struct pcep_ro_subobj_sr *)ro_subobj;
obj_body_buf[index++] =
((sr_subobj->nai_type << 4) & 0xf0);
obj_body_buf[index++] =
((sr_subobj->flag_f ? OBJECT_SUBOBJ_SR_FLAG_F
: 0x00)
| (sr_subobj->flag_s ? OBJECT_SUBOBJ_SR_FLAG_S
: 0x00)
| (sr_subobj->flag_c ? OBJECT_SUBOBJ_SR_FLAG_C
: 0x00)
| (sr_subobj->flag_m ? OBJECT_SUBOBJ_SR_FLAG_M
: 0x00));
uint32_ptr = (uint32_t *)(obj_body_buf + index);
/* Start with LENGTH_1WORD for the SubObj HDR + NT +
* Flags */
uint8_t sr_base_length = LENGTH_1WORD;
/* If the sid_absent flag is true, then dont convert the
* sid */
if (sr_subobj->flag_s == false) {
uint32_ptr[0] = htonl(sr_subobj->sid);
index += LENGTH_1WORD;
uint32_ptr = (uint32_t *)(obj_body_buf + index);
sr_base_length += LENGTH_1WORD;
}
/* The lengths below need to include:
* - sr_base_length: set above to include SR SubObj Hdr
* and the SID if present
* - Number of bytes written to the NAI
* The index will only be incremented below by the
* number of bytes written to the NAI, since the RO SR
* subobj header and the SID have already been written.
*/
double_linked_list_node *nai_node =
(sr_subobj->nai_list == NULL
? NULL
: sr_subobj->nai_list->head);
if (nai_node == NULL) {
if (sr_subobj->nai_type
== PCEP_SR_SUBOBJ_NAI_ABSENT) {
*length_ptr = sr_base_length;
continue;
} else {
return 0;
}
}
switch (sr_subobj->nai_type) {
case PCEP_SR_SUBOBJ_NAI_IPV4_NODE:
uint32_ptr[0] =
((struct in_addr *)nai_node->data)
->s_addr;
*length_ptr = sr_base_length + LENGTH_1WORD;
index += LENGTH_1WORD;
break;
case PCEP_SR_SUBOBJ_NAI_IPV6_NODE:
encode_ipv6((struct in6_addr *)nai_node->data,
uint32_ptr);
*length_ptr = sr_base_length + LENGTH_4WORDS;
index += LENGTH_4WORDS;
break;
case PCEP_SR_SUBOBJ_NAI_UNNUMBERED_IPV4_ADJACENCY:
uint32_ptr[0] =
((struct in_addr *)nai_node->data)
->s_addr;
nai_node = nai_node->next_node;
uint32_ptr[1] =
((struct in_addr *)nai_node->data)
->s_addr;
nai_node = nai_node->next_node;
uint32_ptr[2] =
((struct in_addr *)nai_node->data)
->s_addr;
nai_node = nai_node->next_node;
uint32_ptr[3] =
((struct in_addr *)nai_node->data)
->s_addr;
*length_ptr = sr_base_length + LENGTH_4WORDS;
index += LENGTH_4WORDS;
break;
case PCEP_SR_SUBOBJ_NAI_IPV4_ADJACENCY:
uint32_ptr[0] =
((struct in_addr *)nai_node->data)
->s_addr;
nai_node = nai_node->next_node;
uint32_ptr[1] =
((struct in_addr *)nai_node->data)
->s_addr;
*length_ptr = sr_base_length + LENGTH_2WORDS;
index += LENGTH_2WORDS;
break;
case PCEP_SR_SUBOBJ_NAI_IPV6_ADJACENCY:
encode_ipv6((struct in6_addr *)nai_node->data,
uint32_ptr);
nai_node = nai_node->next_node;
encode_ipv6((struct in6_addr *)nai_node->data,
uint32_ptr + 4);
*length_ptr = sr_base_length + LENGTH_8WORDS;
index += LENGTH_8WORDS;
break;
case PCEP_SR_SUBOBJ_NAI_LINK_LOCAL_IPV6_ADJACENCY:
encode_ipv6((struct in6_addr *)nai_node->data,
uint32_ptr);
nai_node = nai_node->next_node;
uint32_ptr[4] =
((struct in_addr *)nai_node->data)
->s_addr;
nai_node = nai_node->next_node;
encode_ipv6((struct in6_addr *)nai_node->data,
uint32_ptr + 5);
nai_node = nai_node->next_node;
uint32_ptr[9] =
((struct in_addr *)nai_node->data)
->s_addr;
*length_ptr = sr_base_length + LENGTH_10WORDS;
index += LENGTH_10WORDS;
break;
case PCEP_SR_SUBOBJ_NAI_ABSENT:
case PCEP_SR_SUBOBJ_NAI_UNKNOWN:
break;
}
} break;
case RO_SUBOBJ_UNKNOWN:
break;
}
}
return index;
}
void encode_ipv6(struct in6_addr *src_ipv6, uint32_t *dst)
{
memcpy(dst, src_ipv6, sizeof(struct in6_addr));
}
/*
* Decoding functions.
*/
void pcep_decode_object_hdr(const uint8_t *obj_buf,
struct pcep_object_header *obj_hdr)
{
memset(obj_hdr, 0, sizeof(struct pcep_object_header));
obj_hdr->object_class = obj_buf[0];
obj_hdr->object_type = (obj_buf[1] >> 4) & 0x0f;
obj_hdr->flag_p = (obj_buf[1] & OBJECT_HEADER_FLAG_P);
obj_hdr->flag_i = (obj_buf[1] & OBJECT_HEADER_FLAG_I);
uint16_t net_order_length;
memcpy(&net_order_length, obj_buf + 2, sizeof(net_order_length));
obj_hdr->encoded_object_length = ntohs(net_order_length);
obj_hdr->encoded_object = obj_buf;
}
uint16_t pcep_object_get_length(enum pcep_object_classes object_class,
enum pcep_object_types object_type)
{
uint8_t object_length = pcep_object_class_lengths[object_class];
if (object_length == 0) {
if (object_class == PCEP_OBJ_CLASS_ENDPOINTS) {
if (object_type == PCEP_OBJ_TYPE_ENDPOINT_IPV4) {
return 12;
} else if (object_type == PCEP_OBJ_TYPE_ENDPOINT_IPV6) {
return 36;
}
}
return 0;
}
return object_length;
}
uint16_t pcep_object_get_length_by_hdr(struct pcep_object_header *object_hdr)
{
return (pcep_object_get_length(object_hdr->object_class,
object_hdr->object_type));
}
bool pcep_object_has_tlvs(struct pcep_object_header *object_hdr)
{
uint8_t object_length = pcep_object_get_length_by_hdr(object_hdr);
if (object_length == 0) {
return false;
}
return (object_hdr->encoded_object_length - object_length) > 0;
}
struct pcep_object_header *pcep_decode_object(const uint8_t *obj_buf)
{
struct pcep_object_header object_hdr;
/* Only initializes and decodes the Object Header: class, type, flags,
* and length */
pcep_decode_object_hdr(obj_buf, &object_hdr);
if (object_hdr.object_class >= MAX_OBJECT_ENCODER_INDEX) {
pcep_log(LOG_INFO,
"%s: Cannot decode unknown Object class [%d]",
__func__, object_hdr.object_class);
return NULL;
}