lldp_tlv.h
Go to the documentation of this file.
1 /**
2  * @file lldp_tlv.h
3  * @brief TLV parsing and formatting
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _LLDP_TLV_H
32 #define _LLDP_TLV_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "lldp/lldp.h"
37 
38 //Size of organizationally unique identifiers
39 #define LLDP_OUI_SIZE 3
40 
41 //Maximum length of TLV information string
42 #define LLDP_MAX_TLV_INFO_LEN 511
43 //Maximum length of organizationally defined information string
44 #define LLDP_MAX_ORG_SPECIFIC_INFO_LEN 507
45 
46 //Minimum length of chassis ID
47 #define LLDP_MIN_CHASSIS_ID_LEN 1
48 //Maximum length of chassis ID
49 #define LLDP_MAX_CHASSIS_ID_LEN 255
50 
51 //Minimum length of port ID
52 #define LLDP_MIN_PORT_ID_LEN 1
53 //Maximum length of port ID
54 #define LLDP_MAX_PORT_ID_LEN 255
55 
56 //Minimum length of port description
57 #define LLDP_MIN_PORT_DESC_LEN 1
58 //Maximum length of port description
59 #define LLDP_MAX_PORT_DESC_LEN 255
60 
61 //Minimum length of system name
62 #define LLDP_MIN_SYS_NAME_LEN 1
63 //Maximum length of system name
64 #define LLDP_MAX_SYS_NAME_LEN 255
65 
66 //Minimum length of system description
67 #define LLDP_MIN_SYS_DESC_LEN 1
68 //Maximum length of system description
69 #define LLDP_MAX_SYS_DESC_LEN 255
70 
71 //Minimum length of management address
72 #define LLDP_MIN_MGMT_ADDR_LEN 1
73 //Maximum length of management address
74 #define LLDP_MAX_MGMT_ADDR_LEN 31
75 
76 //Minimum length of object identifier
77 #define LLDP_MIN_OID_LEN 0
78 //Maximum length of object identifier
79 #define LLDP_MAX_OID_LEN 128
80 
81 //C++ guard
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 
86 
87 /**
88  * @brief TLV type values
89  **/
90 
91 typedef enum
92 {
93  LLDP_TLV_TYPE_END_OF_LLDPDU = 0, ///<End Of LLDPDU
94  LLDP_TLV_TYPE_CHASSIS_ID = 1, ///<Chassis ID
95  LLDP_TLV_TYPE_PORT_ID = 2, ///<Port ID
96  LLDP_TLV_TYPE_TIME_TO_LIVE = 3, ///<Time To Live
97  LLDP_TLV_TYPE_PORT_DESC = 4, ///<Port Description
98  LLDP_TLV_TYPE_SYS_NAME = 5, ///<System Name
99  LLDP_TLV_TYPE_SYS_DESC = 6, ///<System Description
100  LLDP_TLV_TYPE_SYS_CAP = 7, ///<System Capabilities
101  LLDP_TLV_TYPE_MGMT_ADDR = 8, ///<Management Address
102  LLDP_TLV_TYPE_ORG_DEFINED = 127 ///<Organizationally Specific TLVs
104 
105 
106 /**
107  * @brief Chassis ID subtypes
108  **/
109 
110 typedef enum
111 {
113  LLDP_CHASSIS_ID_SUBTYPE_CHASSIS_COMPONENT = 1, ///<Chassis component
116  LLDP_CHASSIS_ID_SUBTYPE_MAC_ADDR = 4, ///<MAC address
117  LLDP_CHASSIS_ID_SUBTYPE_NETWORK_ADDR = 5, ///<Network address
119  LLDP_CHASSIS_ID_SUBTYPE_LOCALLY_ASSIGNED = 7 ///<Locally assigned
121 
122 
123 /**
124  * @brief Port ID subtypes
125  **/
126 
127 typedef enum
128 {
130  LLDP_PORT_ID_SUBTYPE_INTERFACE_ALIAS = 1, ///<Interface alias
131  LLDP_PORT_ID_SUBTYPE_PORT_COMPONENT = 2, ///<Port component
132  LLDP_PORT_ID_SUBTYPE_MAC_ADDR = 3, ///<MAC address
133  LLDP_PORT_ID_SUBTYPE_NETWORK_ADDR = 4, ///<Network address
134  LLDP_PORT_ID_SUBTYPE_INTERFACE_NAME = 5, ///<Interface name
135  LLDP_PORT_ID_SUBTYPE_AGENT_CIRCUIT_ID = 6, ///<Agent circuit ID
136  LLDP_PORT_ID_SUBTYPE_LOCALLY_ASSIGNED = 7 ///<Locally assigned
138 
139 
140 /**
141  * @brief System capabilities
142  **/
143 
144 typedef enum
145 {
146  LLDP_SYS_CAP_OTHER = 0x0001, ///<Other
147  LLDP_SYS_CAP_REPEATER = 0x0002, ///<Repeater
148  LLDP_SYS_CAP_BRIDGE = 0x0004, ///<Bridge
149  LLDP_SYS_CAP_WLAN_ACCESS_POINT = 0x0008, ///<WLAN Access Point
150  LLDP_SYS_CAP_ROUTER = 0x0010, ///<Router
151  LLDP_SYS_CAP_TELEPHONE = 0x0020, ///<Telephone
152  LLDP_SYS_CAP_DOCSIS_CABLE_DEVICE = 0x0040, ///<DOCSIS cable device
153  LLDP_SYS_CAP_STATION_ONLY = 0x0080 ///<Station Only
155 
156 
157 /**
158  * @brief Management address subtypes
159  **/
160 
161 typedef enum
162 {
164  LLDP_MGMT_ADDR_SUBTYPE_IPV4 = 1, ///<IPv4 address
165  LLDP_MGMT_ADDR_SUBTYPE_IPV6 = 2, ///<IPv6 address
166  LLDP_MGMT_ADDR_SUBTYPE_ALL_802 = 6 ///<MAC address
168 
169 
170 /**
171  * @brief Interface numbering subtypes
172  **/
173 
174 typedef enum
175 {
177  LLDP_IF_NUM_SUBTYPE_IF_INDEX = 2, ///<Interface index
178  LLDP_IF_NUM_SUBTYPE_SYS_PORT_NUM = 3 ///<System port number
180 
181 
182 /**
183  * @brief Organizationally unique identifiers
184  **/
185 
186 typedef enum
187 {
188  LLDP_DOT1_OUI = 0x0080C2, ///<IEEE 802.1
189  LLDP_DOT3_OUI = 0x00120F, ///<IEEE 802.3
190  LLDP_MED_OUI = 0x0012BB, ///<LLDP-MED
191  LLDP_PNO_OUI = 0x000ECF ///<PROFIBUS
193 
194 
195 /**
196  * @brief TLV structure
197  **/
198 
199 typedef struct
200 {
201  size_t pos;
202  uint8_t type;
203  size_t length;
204  uint8_t *value;
205 } LldpTlv;
206 
207 
208 //CC-RX, CodeWarrior or Win32 compiler?
209 #if defined(__CCRX__)
210  #pragma pack
211 #elif defined(__CWCC__) || defined(_WIN32)
212  #pragma pack(push, 1)
213 #endif
214 
215 
216 /**
217  * @brief TLV header
218  **/
219 
221 {
222 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
223  uint8_t type : 7; //0
224  uint8_t lengthH : 1;
225  uint8_t lengthL; //1
226  uint8_t value[]; //2
227 #else
228  uint8_t lengthH : 1; //0
229  uint8_t type : 7;
230  uint8_t lengthL; //1
231  uint8_t value[]; //2
232 #endif
234 
235 
236 /**
237  * @brief Chassis ID TLV
238  **/
239 
240 typedef __packed_struct
241 {
242  uint8_t chassisIdSubtype; //0
243  uint8_t chassisId[]; //1
245 
246 
247 /**
248  * @brief Port ID TLV
249  **/
250 
251 typedef __packed_struct
252 {
253  uint8_t portIdSubtype; //0
254  uint8_t portId[]; //1
256 
257 
258 /**
259  * @brief Time To Live TLV
260  **/
261 
262 typedef __packed_struct
263 {
264  uint16_t ttl; //0-1
266 
267 
268 /**
269  * @brief System Capabilities TLV
270  **/
271 
272 typedef __packed_struct
273 {
274  uint16_t supportedCap; //0-1
275  uint16_t enabledCap; //2-3
277 
278 
279 /**
280  * @brief Management Address TLV (part 1)
281  **/
282 
283 typedef __packed_struct
284 {
285  uint8_t mgmtAddrLen; //0
286  uint8_t mgmtAddrSubtype; //1
287  uint8_t mgmtAddr[]; //2
289 
290 
291 /**
292  * @brief Management Address TLV (part 2)
293  **/
294 
295 typedef __packed_struct
296 {
297  uint8_t ifNumSubtype; //0
298  uint32_t ifNum; //1-4
299  uint8_t oidLen; //5
300  uint8_t oid[]; //6
302 
303 
304 /**
305  * @brief Organizationally Specific TLV
306  **/
307 
308 typedef __packed_struct
309 {
310  uint8_t oui[LLDP_OUI_SIZE]; //0-2
311  uint8_t subtype; //3
312  uint8_t value[]; //4
314 
315 
316 //CC-RX, CodeWarrior or Win32 compiler?
317 #if defined(__CCRX__)
318  #pragma unpack
319 #elif defined(__CWCC__) || defined(_WIN32)
320  #pragma pack(pop)
321 #endif
322 
323 //LLDP related functions
324 error_t lldpSetTlv(LldpDataUnit *lldpdu, uint8_t type, uint_t index,
325  const uint8_t *value, size_t length, bool_t replace);
326 
327 error_t lldpGetTlv(LldpDataUnit *lldpdu, uint8_t type, uint_t index,
328  const uint8_t **value, size_t *length);
329 
332 
333 error_t lldpDeleteTlv(LldpDataUnit *lldpdu, uint8_t type, uint_t index);
334 
335 error_t lldpDecodeMgmtAddrTlv(const uint8_t *value, size_t length,
336  const LldpMgmtAddrTlv1 **mgmtAddr1, const LldpMgmtAddrTlv2 **mgmtAddr2);
337 
338 error_t lldpSetOrgDefTlv(LldpDataUnit *lldpdu, uint32_t oui, uint8_t subtype,
339  uint_t index, const uint8_t *value, size_t length, bool_t replace);
340 
341 error_t lldpGetOrgDefTlv(LldpDataUnit *lldpdu, uint32_t oui, uint8_t subtype,
342  uint_t index, const uint8_t **value, size_t *length);
343 
344 error_t lldpDeleteOrgDefTlv(LldpDataUnit *lldpdu, uint32_t oui, uint8_t subtype,
345  uint_t index);
346 
347 //C++ guard
348 #ifdef __cplusplus
349 }
350 #endif
351 
352 #endif
size_t pos
Definition: lldp_tlv.h:201
uint8_t type
Definition: lldp_tlv.h:202
@ LLDP_MGMT_ADDR_SUBTYPE_IPV4
IPv4 address.
Definition: lldp_tlv.h:164
@ LLDP_TLV_TYPE_SYS_CAP
System Capabilities.
Definition: lldp_tlv.h:100
@ LLDP_PORT_ID_SUBTYPE_PORT_COMPONENT
Port component.
Definition: lldp_tlv.h:131
int bool_t
Definition: compiler_port.h:53
uint8_t portId[]
Definition: lldp_tlv.h:254
uint16_t enabledCap
Definition: lldp_tlv.h:275
@ LLDP_CHASSIS_ID_SUBTYPE_INTERFACE_ALIAS
Interface alias.
Definition: lldp_tlv.h:114
uint8_t chassisId[]
Definition: lldp_tlv.h:243
@ LLDP_SYS_CAP_BRIDGE
Bridge.
Definition: lldp_tlv.h:148
@ LLDP_CHASSIS_ID_SUBTYPE_NETWORK_ADDR
Network address.
Definition: lldp_tlv.h:117
LldpOrgDefTlv
Definition: lldp_tlv.h:313
@ LLDP_TLV_TYPE_MGMT_ADDR
Management Address.
Definition: lldp_tlv.h:101
@ LLDP_SYS_CAP_OTHER
Other.
Definition: lldp_tlv.h:146
@ LLDP_TLV_TYPE_ORG_DEFINED
Organizationally Specific TLVs.
Definition: lldp_tlv.h:102
@ LLDP_PNO_OUI
PROFIBUS.
Definition: lldp_tlv.h:191
error_t lldpDeleteOrgDefTlv(LldpDataUnit *lldpdu, uint32_t oui, uint8_t subtype, uint_t index)
Remove an organizationally specific TLV from a LLDPDU.
Definition: lldp_tlv.c:719
LldpTlvType
TLV type values.
Definition: lldp_tlv.h:92
LldpPortIdSubtype
Port ID subtypes.
Definition: lldp_tlv.h:128
@ LLDP_IF_NUM_SUBTYPE_SYS_PORT_NUM
System port number.
Definition: lldp_tlv.h:178
typedef __packed_struct
TLV header.
Definition: lldp_tlv.h:221
uint8_t type
Definition: lldp_tlv.h:229
@ LLDP_CHASSIS_ID_SUBTYPE_INTERFACE_NAME
Interface name.
Definition: lldp_tlv.h:118
uint8_t oid[]
Definition: lldp_tlv.h:300
@ LLDP_PORT_ID_SUBTYPE_INTERFACE_NAME
Interface name.
Definition: lldp_tlv.h:134
@ LLDP_TLV_TYPE_END_OF_LLDPDU
End Of LLDPDU.
Definition: lldp_tlv.h:93
LldpOui
Organizationally unique identifiers.
Definition: lldp_tlv.h:187
LldpChassisIdTlv
Definition: lldp_tlv.h:244
uint8_t mgmtAddr[]
Definition: lldp_tlv.h:287
@ LLDP_CHASSIS_ID_SUBTYPE_MAC_ADDR
MAC address.
Definition: lldp_tlv.h:116
#define LLDP_OUI_SIZE
Definition: lldp_tlv.h:39
@ LLDP_PORT_ID_SUBTYPE_RESERVED
Reserved.
Definition: lldp_tlv.h:129
@ LLDP_SYS_CAP_REPEATER
Repeater.
Definition: lldp_tlv.h:147
uint8_t lengthL
Definition: lldp_tlv.h:230
LldpIfNumSubtype
Interface numbering subtypes.
Definition: lldp_tlv.h:175
error_t
Error codes.
Definition: error.h:43
@ LLDP_PORT_ID_SUBTYPE_LOCALLY_ASSIGNED
Locally assigned.
Definition: lldp_tlv.h:136
@ LLDP_PORT_ID_SUBTYPE_MAC_ADDR
MAC address.
Definition: lldp_tlv.h:132
error_t lldpGetFirstTlv(LldpDataUnit *lldpdu, LldpTlv *tlv)
Extract the first TLV from an LLDPDU.
Definition: lldp_tlv.c:247
@ LLDP_SYS_CAP_DOCSIS_CABLE_DEVICE
DOCSIS cable device.
Definition: lldp_tlv.h:152
@ LLDP_SYS_CAP_ROUTER
Router.
Definition: lldp_tlv.h:150
@ LLDP_CHASSIS_ID_SUBTYPE_CHASSIS_COMPONENT
Chassis component.
Definition: lldp_tlv.h:113
error_t lldpSetTlv(LldpDataUnit *lldpdu, uint8_t type, uint_t index, const uint8_t *value, size_t length, bool_t replace)
Add or replace a TLV.
Definition: lldp_tlv.c:56
@ LLDP_IF_NUM_SUBTYPE_UNKNOWN
Unknown.
Definition: lldp_tlv.h:176
error_t lldpDecodeMgmtAddrTlv(const uint8_t *value, size_t length, const LldpMgmtAddrTlv1 **mgmtAddr1, const LldpMgmtAddrTlv2 **mgmtAddr2)
Decode the contents of a Management Address TLV.
Definition: lldp_tlv.c:380
LldpPortIdTlv
Definition: lldp_tlv.h:255
@ LLDP_MED_OUI
LLDP-MED.
Definition: lldp_tlv.h:190
uint8_t length
Definition: tcp.h:368
@ LLDP_MGMT_ADDR_SUBTYPE_ALL_802
MAC address.
Definition: lldp_tlv.h:166
@ LLDP_DOT1_OUI
IEEE 802.1.
Definition: lldp_tlv.h:188
@ LLDP_PORT_ID_SUBTYPE_NETWORK_ADDR
Network address.
Definition: lldp_tlv.h:133
@ LLDP_TLV_TYPE_SYS_NAME
System Name.
Definition: lldp_tlv.h:98
@ LLDP_CHASSIS_ID_SUBTYPE_RESERVED
Reserved.
Definition: lldp_tlv.h:112
uint8_t subtype
Definition: lldp_tlv.h:311
@ LLDP_TLV_TYPE_CHASSIS_ID
Chassis ID.
Definition: lldp_tlv.h:94
@ LLDP_IF_NUM_SUBTYPE_IF_INDEX
Interface index.
Definition: lldp_tlv.h:177
error_t lldpDeleteTlv(LldpDataUnit *lldpdu, uint8_t type, uint_t index)
Remove a TLV from a LLDPDU.
Definition: lldp_tlv.c:320
@ LLDP_TLV_TYPE_SYS_DESC
System Description.
Definition: lldp_tlv.h:99
@ LLDP_SYS_CAP_TELEPHONE
Telephone.
Definition: lldp_tlv.h:151
@ LLDP_DOT3_OUI
IEEE 802.3.
Definition: lldp_tlv.h:189
@ LLDP_PORT_ID_SUBTYPE_AGENT_CIRCUIT_ID
Agent circuit ID.
Definition: lldp_tlv.h:135
size_t length
Definition: lldp_tlv.h:203
LldpTimeToLiveTlv
Definition: lldp_tlv.h:265
uint32_t ifNum
Definition: lldp_tlv.h:298
error_t lldpGetNextTlv(LldpDataUnit *lldpdu, LldpTlv *tlv)
Extract the next TLV from an LLDPDU.
Definition: lldp_tlv.c:264
uint8_t oidLen
Definition: lldp_tlv.h:299
LldpChassisIdSubtype
Chassis ID subtypes.
Definition: lldp_tlv.h:111
@ LLDP_SYS_CAP_WLAN_ACCESS_POINT
WLAN Access Point.
Definition: lldp_tlv.h:149
LldpMgmtAddrTlv2
Definition: lldp_tlv.h:301
@ LLDP_CHASSIS_ID_SUBTYPE_LOCALLY_ASSIGNED
Locally assigned.
Definition: lldp_tlv.h:119
@ LLDP_MGMT_ADDR_SUBTYPE_OTHER
Other.
Definition: lldp_tlv.h:163
error_t lldpGetOrgDefTlv(LldpDataUnit *lldpdu, uint32_t oui, uint8_t subtype, uint_t index, const uint8_t **value, size_t *length)
Search an LLDPDU for an organizationally specific TLV.
Definition: lldp_tlv.c:651
@ LLDP_SYS_CAP_STATION_ONLY
Station Only.
Definition: lldp_tlv.h:153
@ LLDP_TLV_TYPE_PORT_ID
Port ID.
Definition: lldp_tlv.h:95
uint8_t mgmtAddrSubtype
Definition: lldp_tlv.h:286
uint32_t ttl
Definition: dns_common.h:221
LLDP (Link Layer Discovery Protocol)
LldpMgmtAddrSubtype
Management address subtypes.
Definition: lldp_tlv.h:162
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
LldpTlvHeader
Definition: lldp_tlv.h:233
@ LLDP_TLV_TYPE_TIME_TO_LIVE
Time To Live.
Definition: lldp_tlv.h:96
LldpSysCapTlv
Definition: lldp_tlv.h:276
LldpMgmtAddrTlv1
Definition: lldp_tlv.h:288
TLV structure.
Definition: lldp_tlv.h:200
error_t lldpGetTlv(LldpDataUnit *lldpdu, uint8_t type, uint_t index, const uint8_t **value, size_t *length)
Search a LLDPDU for a given TLV.
Definition: lldp_tlv.c:200
@ LLDP_TLV_TYPE_PORT_DESC
Port Description.
Definition: lldp_tlv.h:97
@ LLDP_CHASSIS_ID_SUBTYPE_PORT_COMPONENT
Port component.
Definition: lldp_tlv.h:115
@ LLDP_MGMT_ADDR_SUBTYPE_IPV6
IPv6 address.
Definition: lldp_tlv.h:165
@ LLDP_PORT_ID_SUBTYPE_INTERFACE_ALIAS
Interface alias.
Definition: lldp_tlv.h:130
uint8_t value[]
Definition: lldp_tlv.h:231
LldpSysCap
System capabilities.
Definition: lldp_tlv.h:145
#define LldpDataUnit
Definition: lldp.h:36
uint8_t * value
Definition: lldp_tlv.h:204
error_t lldpSetOrgDefTlv(LldpDataUnit *lldpdu, uint32_t oui, uint8_t subtype, uint_t index, const uint8_t *value, size_t length, bool_t replace)
Add or replace an organizationally specific TLV.
Definition: lldp_tlv.c:452