1 /** @file tlv.h
2  *
3  * OpenLLDP TLV Header
4  *
5  * See LICENSE file for more info.
6  *
7  * File: lldp_tlv.h
8  *
9  * Authors: Terry Simons (terry.simons@gmail.com)
10  *
11  *******************************************************************/
12 
13 #ifndef LLDP_TLV_H
14 #define LLDP_TLV_H
15 
16 #include "lldp_port.h"
17 
18 /* TLV Types from section 9.4.1 of IEEE 802.1AB */
19 #define END_OF_LLDPDU_TLV       0    /* MANDATORY */
20 #define CHASSIS_ID_TLV          1    /* MANDATORY */
21 #define PORT_ID_TLV             2    /* MANDATORY */
22 #define TIME_TO_LIVE_TLV        3    /* MANDATORY */
23 #define PORT_DESCRIPTION_TLV    4    /* OPTIONAL  */
24 #define SYSTEM_NAME_TLV         5    /* OPTIONAL  */
25 #define SYSTEM_DESCRIPTION_TLV  6    /* OPTIONAL  */
26 #define SYSTEM_CAPABILITIES_TLV 7    /* OPTIONAL  */
27 #define MANAGEMENT_ADDRESS_TLV  8    /* OPTIONAL  */
28 /* 9 - 126 are reserved */
29 #define ORG_SPECIFIC_TLV        127  /* OPTIONAL */
30 
31 /* TLV Subtypes from section 9 of IEEE 802.1AB */
32 
33 /* Chassis ID TLV Subtypes */
34 /* 0 is reserved */
35 #define CHASSIS_ID_CHASSIS_COMPONENT 1
36 #define CHASSIS_ID_INTERFACE_ALIAS   2
37 #define CHASSIS_ID_PORT_COMPONENT    3
38 #define CHASSIS_ID_MAC_ADDRESS       4
39 #define CHASSIS_ID_NETWORK_ADDRESS   5
40 #define CHASSIS_ID_INTERFACE_NAME    6
41 #define CHASSIS_ID_LOCALLY_ASSIGNED  7
42 /* 8-255 are reserved */
43 /* End Chassis ID TLV Subtypes */
44 
45 /* Port ID TLV Subtypes */
46 /* 0 is reserved */
47 #define PORT_ID_INTERFACE_ALIAS  1
48 #define PORT_ID_PORT_COMPONENT   2
49 #define PORT_ID_MAC_ADDRESS      3
50 #define PORT_ID_NETWORK_ADDRESS  4
51 #define PORT_ID_INTERFACE_NAME   5
52 #define PORT_ID_AGENT_CIRCUIT_ID 6
53 #define PORT_ID_LOCALLY_ASSIGNED 7
54 /* 8-255 are reserved */
55 /* End Port ID TLV Subtypes */
56 
57 /* System Capabilities TLV Subtypes */
58 #define SYSTEM_CAPABILITY_OTHER     1
59 #define SYSTEM_CAPABILITY_REPEATER  2
60 #define SYSTEM_CAPABILITY_BRIDGE    4
61 #define SYSTEM_CAPABILITY_WLAN      8
62 #define SYSTEM_CAPABILITY_ROUTER    16
63 #define SYSTEM_CAPABILITY_TELEPHONE 32
64 #define SYSTEM_CAPABILITY_DOCSIS    64
65 #define SYSTEM_CAPABILITY_STATION   128
66 /* 8 - 15 reserved */
67 /* End System Capabilities TLV Subtypes */
68 
69 /* Location Data Format Type Values */
70 #define LCI_COORDINATE	1
71 #define LCI_CIVIC	2
72 #define LCI_ELIN	3
73 /* 4 - 255 reserved for future expansion */
74 /* End Location Data Format Type Values */
75 
76 /* End TLV Subtypes from section 9 of IEEE 802.1AB */
77 
78 /* IANA Family Number Assignments */
79 /* http://www.iana.org/assignments/address-family-numbers */
80 #define IANA_RESERVED_LOW     0
81 #define IANA_IP               1
82 #define IANA_IP6              2
83 #define IANA_NSAP             3
84 #define IANA_HDLC             4
85 #define IANA_BBN_1822         5
86 #define IANA_802              6
87 #define IANA_E_163            7
88 #define IANA_E_164_ATM        8
89 #define IANA_F_69             9
90 #define IANA_X_121           10
91 #define IANA_IPX             11
92 #define IANA_APPLETALK       12
93 #define IANA_DECNET_IV       13
94 #define IANA_BANYAN_VINES    14
95 #define IANA_E_164_NSAP      15
96 #define IANA_DNS             16
97 #define IANA_DISTINGUISHED   17
98 #define IANA_AS_NUMBER       18
99 #define IANA_XTP_IPV4        19
100 #define IANA_XTP_IPV6        20
101 #define IANA_XTP_XTP         21
102 #define IANA_FIBRE_PORT_NAME 22
103 #define IANA_FIBRE_NODE_NAME 23
104 #define IANA_GWID            24
105 #define IANA_AFI_L2VPN       25
106 // Everything from 26 to 65534 is Unassigned
107 #define IANA_RESERVED_HIGH   65535
108 /* End IANA Family Number Assignments */
109 
110 struct lldp_tlv_validation_errors {
111     uint64_t errors;
112     uint8_t chassis_id_tlv_count;
113     uint8_t port_id_tlv_count;
114     uint8_t ttl_tlv_count;
115     uint8_t port_description_tlv_count;
116     uint8_t system_name_tlv_count;
117     uint8_t system_description_tlv_count;
118     uint8_t system_capabilities_tlv_count;
119     uint8_t management_address_tlv_count;
120 };
121 
122 struct lldp_test_case {
123     // Pointer to a tlv template
124     struct lldp_tlv_template *test_case;
125 
126     // Pointer to the next test case
127     struct lldp_test_cases *next;
128 };
129 
130 struct lldp_tlv *create_end_of_lldpdu_tlv(struct lldp_port *lldp_port);
131 uint8_t validate_end_of_lldpdu_tlv(struct lldp_tlv *tlv);
132 
133 struct lldp_tlv *create_chassis_id_tlv(struct lldp_port *lldp_port);
134 uint8_t validate_chassis_id_tlv(struct lldp_tlv *tlv);
135 
136 struct lldp_tlv *create_port_id_tlv(struct lldp_port *lldp_port);
137 uint8_t validate_port_id_tlv(struct lldp_tlv *tlv);
138 
139 struct lldp_tlv *create_ttl_tlv(struct lldp_port *lldp_port);
140 uint8_t validate_ttl_tlv(struct lldp_tlv *tlv);
141 
142 struct lldp_tlv *create_port_description_tlv(struct lldp_port *lldp_port);
143 uint8_t validate_port_description_tlv(struct lldp_tlv *tlv);
144 
145 struct lldp_tlv *create_system_name_tlv(struct lldp_port *lldp_port);
146 uint8_t validate_system_name_tlv(struct lldp_tlv *tlv);
147 
148 struct lldp_tlv *create_system_description_tlv(struct lldp_port *lldp_port);
149 uint8_t validate_system_description_tlv(struct lldp_tlv *tlv);
150 
151 struct lldp_tlv *create_system_capabilities_tlv(struct lldp_port *lldp_port);
152 uint8_t validate_system_capabilities_tlv(struct lldp_tlv *tlv);
153 
154 struct lldp_tlv *create_management_address_tlv(struct lldp_port *lldp_port);
155 uint8_t validate_management_address_tlv(struct lldp_tlv *tlv);
156 
157 struct lldp_tlv *create_lldpmed_location_identification_tlv(struct lldp_port *lldp_port);
158 struct lldp_tlv *validate_lldpmed_location_identification_tlv(struct lldp_tlv *tlv);  // NB todo
159 
160 // Should probably allow this create function to specify the OUI in question?
161 struct lldp_tlv *create_organizationally_specific_tlv(struct lldp_port *lldp_port, uint8_t *oui);
162 uint8_t validate_organizationally_specific_tlv(struct lldp_tlv *tlv);
163 
164 extern uint8_t (*validate_tlv[])(struct lldp_tlv *tlv);
165 
166 #define LLDP_BEGIN_RESERVED_TLV 9
167 #define LLDP_END_RESERVED_TLV 126
168 
169 #define XVALIDTLV     0
170 #define XEINVALIDTLV -1
171 
172 char *decode_organizationally_specific_tlv(struct lldp_tlv *tlv);
173 
174 char *decode_network_address(uint8_t *network_address);
175 char *decode_tlv_subtype(struct lldp_tlv *tlv);
176 void decode_oui_tlv(struct lldp_tlv *tlv);
177 char *decode_tlv_system_capabilities( uint16_t system_capabilities, uint16_t enabled_capabilities);
178 char *decode_management_address(struct lldp_tlv *tlv);
179 char *decode_ipv4_address(uint8_t *ipv4_address);
180 char *capability_name(uint16_t capability);
181 char *tlv_typetoname(uint8_t tlv_type);
182 uint8_t tlvcpy(struct lldp_tlv *dst, struct lldp_tlv *src);
183 struct lldp_tlv *tlvpop(uint8_t *buffer);
184 uint8_t tlvpush(uint8_t *buffer, struct lldp_tlv *tlv);
185 
186 uint8_t lldp_cache_tlv(struct lldp_tlv *tlv);
187 
188 uint8_t tlvInitializeLLDP();
189 void tlvCleanupLLDP();
190 
191 uint8_t initializeTLVFunctionValidators();
192 
193 char *tlv_typetoname(uint8_t tlv_type);
194 char *tlv_info_string_to_cstr(struct lldp_tlv *tlv);
195 
196 
197 struct lldp_tlv *initialize_tlv();
198 
199 //structure for locatation configuration data needed for LLDP-MED location identification TLV
200 struct lci_s {
201   int location_data_format;
202   char *coordinate_based_lci;
203   int civic_what;
204   char *civic_countrycode;
205   char *civic_ca[33];
206   char *elin;
207   char *config_file;
208 };
209 
210 
211 
212 
213 #endif /* LLDP_TLV_H */
214