]> git.ipfire.org Git - thirdparty/lldpd.git/blob - src/lldpd-structs.h
lldpd: make custom TLV code optional
[thirdparty/lldpd.git] / src / lldpd-structs.h
1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #ifndef _LLDPD_STRUCTS_H
19 #define _LLDPD_STRUCTS_H
20
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/socket.h>
27
28 /* This is not very convenient, but we need net/if.h for IFNAMSIZ and others but
29 * we may also need linux/if.h in some modules. And they conflict each others.
30 */
31 #ifdef HOST_OS_LINUX
32 # include <linux/if.h>
33 #else
34 # include <net/if.h>
35 #endif
36
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
39 #include <sys/queue.h>
40
41 #include "compat/compat.h"
42 #include "marshal.h"
43 #include "lldp-const.h"
44
45 #ifdef ENABLE_DOT1
46 struct lldpd_ppvid {
47 TAILQ_ENTRY(lldpd_ppvid) p_entries;
48 u_int8_t p_cap_status;
49 u_int16_t p_ppvid;
50 };
51 MARSHAL_BEGIN(lldpd_ppvid)
52 MARSHAL_TQE(lldpd_ppvid, p_entries)
53 MARSHAL_END(lldpd_ppvid);
54
55 struct lldpd_vlan {
56 TAILQ_ENTRY(lldpd_vlan) v_entries;
57 char *v_name;
58 u_int16_t v_vid;
59 };
60 MARSHAL_BEGIN(lldpd_vlan)
61 MARSHAL_TQE(lldpd_vlan, v_entries)
62 MARSHAL_STR(lldpd_vlan, v_name)
63 MARSHAL_END(lldpd_vlan);
64
65 struct lldpd_pi {
66 TAILQ_ENTRY(lldpd_pi) p_entries;
67 char *p_pi;
68 int p_pi_len;
69 };
70 MARSHAL_BEGIN(lldpd_pi)
71 MARSHAL_TQE(lldpd_pi, p_entries)
72 MARSHAL_FSTR(lldpd_pi, p_pi, p_pi_len)
73 MARSHAL_END(lldpd_pi);
74 #endif
75
76 #ifdef ENABLE_LLDPMED
77 struct lldpd_med_policy {
78 u_int8_t index; /* Not used. */
79 u_int8_t type;
80 u_int8_t unknown;
81 u_int8_t tagged;
82 u_int16_t vid;
83 u_int8_t priority;
84 u_int8_t dscp;
85 };
86 MARSHAL(lldpd_med_policy);
87
88 struct lldpd_med_loc {
89 u_int8_t index; /* Not used. */
90 u_int8_t format;
91 char *data;
92 int data_len;
93 };
94 MARSHAL_BEGIN(lldpd_med_loc)
95 MARSHAL_FSTR(lldpd_med_loc, data, data_len)
96 MARSHAL_END(lldpd_med_loc);
97
98 struct lldpd_med_power {
99 u_int8_t devicetype; /* PD or PSE */
100 u_int8_t source;
101 u_int8_t priority;
102 u_int16_t val;
103 };
104 MARSHAL(lldpd_med_power);
105 #endif
106
107 #ifdef ENABLE_DOT3
108 struct lldpd_dot3_macphy {
109 u_int8_t autoneg_support;
110 u_int8_t autoneg_enabled;
111 u_int16_t autoneg_advertised;
112 u_int16_t mau_type;
113 };
114
115 struct lldpd_dot3_power {
116 u_int8_t devicetype;
117 u_int8_t supported;
118 u_int8_t enabled;
119 u_int8_t paircontrol;
120 u_int8_t pairs;
121 u_int8_t class;
122 u_int8_t powertype; /* If set to LLDP_DOT3_POWER_8023AT_OFF,
123 following fields have no meaning */
124 u_int8_t source;
125 u_int8_t priority;
126 u_int16_t requested;
127 u_int16_t allocated;
128 };
129 MARSHAL(lldpd_dot3_power);
130 #endif
131
132 enum {
133 LLDPD_AF_UNSPEC = 0,
134 LLDPD_AF_IPV4,
135 LLDPD_AF_IPV6,
136 LLDPD_AF_LAST
137 };
138
139 inline static int
140 lldpd_af(int af)
141 {
142 switch (af) {
143 case LLDPD_AF_IPV4: return AF_INET;
144 case LLDPD_AF_IPV6: return AF_INET6;
145 case LLDPD_AF_LAST: return AF_MAX;
146 default: return AF_UNSPEC;
147 }
148 }
149
150 #define LLDPD_MGMT_MAXADDRSIZE 16 /* sizeof(struct in6_addr) */
151 struct lldpd_mgmt {
152 TAILQ_ENTRY(lldpd_mgmt) m_entries;
153 int m_family;
154 union {
155 struct in_addr inet;
156 struct in6_addr inet6;
157 u_int8_t octets[LLDPD_MGMT_MAXADDRSIZE]; /* network byte order! */
158 } m_addr;
159 size_t m_addrsize;
160 u_int32_t m_iface;
161 };
162 MARSHAL_BEGIN(lldpd_mgmt)
163 MARSHAL_TQE(lldpd_mgmt, m_entries)
164 MARSHAL_END(lldpd_mgmt);
165
166 struct lldpd_chassis {
167 TAILQ_ENTRY(lldpd_chassis) c_entries;
168 u_int16_t c_refcount; /* Reference count by ports */
169 u_int16_t c_index; /* Monotonic index */
170 u_int8_t c_protocol; /* Protocol used to get this chassis */
171 u_int8_t c_id_subtype;
172 char *c_id;
173 int c_id_len;
174 char *c_name;
175 char *c_descr;
176
177 u_int16_t c_cap_available;
178 u_int16_t c_cap_enabled;
179
180 u_int16_t c_ttl;
181
182 TAILQ_HEAD(, lldpd_mgmt) c_mgmt;
183
184 #ifdef ENABLE_LLDPMED
185 u_int16_t c_med_cap_available;
186 u_int8_t c_med_type;
187 char *c_med_hw;
188 char *c_med_fw;
189 char *c_med_sw;
190 char *c_med_sn;
191 char *c_med_manuf;
192 char *c_med_model;
193 char *c_med_asset;
194 #endif
195
196 };
197 /* WARNING: any change to this structure should also be reflected into
198 `lldpd_copy_chassis()` which is not using marshaling. */
199 MARSHAL_BEGIN(lldpd_chassis)
200 MARSHAL_IGNORE(lldpd_chassis, c_entries.tqe_next)
201 MARSHAL_IGNORE(lldpd_chassis, c_entries.tqe_prev)
202 MARSHAL_FSTR(lldpd_chassis, c_id, c_id_len)
203 MARSHAL_STR(lldpd_chassis, c_name)
204 MARSHAL_STR(lldpd_chassis, c_descr)
205 MARSHAL_SUBTQ(lldpd_chassis, lldpd_mgmt, c_mgmt)
206 #ifdef ENABLE_LLDPMED
207 MARSHAL_STR(lldpd_chassis, c_med_hw)
208 MARSHAL_STR(lldpd_chassis, c_med_fw)
209 MARSHAL_STR(lldpd_chassis, c_med_sw)
210 MARSHAL_STR(lldpd_chassis, c_med_sn)
211 MARSHAL_STR(lldpd_chassis, c_med_manuf)
212 MARSHAL_STR(lldpd_chassis, c_med_model)
213 MARSHAL_STR(lldpd_chassis, c_med_asset)
214 #endif
215 MARSHAL_END(lldpd_chassis);
216
217 #ifdef ENABLE_CUSTOM
218 /* Custom TLV struct as defined on page 35 of IEEE 802.1AB-2005 */
219 struct lldpd_custom {
220 TAILQ_ENTRY(lldpd_custom) next; /* Pointer to next custom TLV */
221
222 /* Organizationally Unique Identifier */
223 u_int8_t oui[LLDP_TLV_ORG_OUI_LEN];
224 /* Organizationally Defined Subtype */
225 u_int8_t subtype;
226 /* Organizationally Defined Information String; for now/simplicity static array */
227 u_int8_t oui_info[LLDP_TLV_ORG_OUI_INFO_MAXLEN];
228 /* Organizationally Defined Information String length */
229 int oui_info_len;
230 };
231 MARSHAL_BEGIN(lldpd_custom)
232 MARSHAL_TQE(lldpd_custom, next)
233 MARSHAL_END(lldpd_custom);
234 #endif
235
236 struct lldpd_port {
237 TAILQ_ENTRY(lldpd_port) p_entries;
238 struct lldpd_chassis *p_chassis; /* Attached chassis */
239 time_t p_lastchange; /* Time of last change of values */
240 time_t p_lastupdate; /* Time of last update received */
241 struct lldpd_frame *p_lastframe; /* Frame received during last update */
242 u_int8_t p_protocol; /* Protocol used to get this port */
243 u_int8_t p_hidden_in:1; /* Considered as hidden for reception */
244 u_int8_t p_hidden_out:2; /* Considered as hidden for emission */
245 /* Important: all fields that should be ignored to check if a port has
246 * been changed should be before this mark. */
247 #define LLDPD_PORT_START_MARKER (offsetof(struct lldpd_port, p_id_subtype))
248 u_int8_t p_id_subtype;
249 char *p_id;
250 int p_id_len;
251 char *p_descr;
252 u_int16_t p_mfs;
253
254 #ifdef ENABLE_DOT3
255 /* Dot3 stuff */
256 u_int32_t p_aggregid;
257 struct lldpd_dot3_macphy p_macphy;
258 struct lldpd_dot3_power p_power;
259 #endif
260
261 #ifdef ENABLE_LLDPMED
262 u_int16_t p_med_cap_enabled;
263 struct lldpd_med_policy p_med_policy[LLDP_MED_APPTYPE_LAST];
264 struct lldpd_med_loc p_med_location[LLDP_MED_LOCFORMAT_LAST];
265 struct lldpd_med_power p_med_power;
266 #endif
267
268 #ifdef ENABLE_DOT1
269 u_int16_t p_pvid;
270 TAILQ_HEAD(, lldpd_vlan) p_vlans;
271 TAILQ_HEAD(, lldpd_ppvid) p_ppvids;
272 TAILQ_HEAD(, lldpd_pi) p_pids;
273 #endif
274 #ifdef ENABLE_CUSTOM
275 TAILQ_HEAD(, lldpd_custom) p_custom_list;
276 #endif
277 };
278 MARSHAL_BEGIN(lldpd_port)
279 MARSHAL_TQE(lldpd_port, p_entries)
280 MARSHAL_POINTER(lldpd_port, lldpd_chassis, p_chassis)
281 MARSHAL_IGNORE(lldpd_port, p_lastframe)
282 MARSHAL_FSTR(lldpd_port, p_id, p_id_len)
283 MARSHAL_STR(lldpd_port, p_descr)
284 #ifdef ENABLE_LLDPMED
285 MARSHAL_SUBSTRUCT(lldpd_port, lldpd_med_loc, p_med_location[0])
286 MARSHAL_SUBSTRUCT(lldpd_port, lldpd_med_loc, p_med_location[1])
287 MARSHAL_SUBSTRUCT(lldpd_port, lldpd_med_loc, p_med_location[2])
288 #endif
289 #ifdef ENABLE_DOT1
290 MARSHAL_SUBTQ(lldpd_port, lldpd_vlan, p_vlans)
291 MARSHAL_SUBTQ(lldpd_port, lldpd_ppvid, p_ppvids)
292 MARSHAL_SUBTQ(lldpd_port, lldpd_pi, p_pids)
293 #endif
294 #ifdef ENABLE_CUSTOM
295 MARSHAL_SUBTQ(lldpd_port, lldpd_custom, p_custom_list)
296 #endif
297 MARSHAL_END(lldpd_port);
298
299 /* Used to modify some port related settings */
300 struct lldpd_port_set {
301 char *ifname;
302 char *local_id;
303 char *local_descr;
304 #ifdef ENABLE_LLDPMED
305 struct lldpd_med_policy *med_policy;
306 struct lldpd_med_loc *med_location;
307 struct lldpd_med_power *med_power;
308 #endif
309 #ifdef ENABLE_DOT3
310 struct lldpd_dot3_power *dot3_power;
311 #endif
312 #ifdef ENABLE_CUSTOM
313 struct lldpd_custom *custom;
314 int custom_list_clear;
315 #endif
316 };
317 MARSHAL_BEGIN(lldpd_port_set)
318 MARSHAL_STR(lldpd_port_set, ifname)
319 MARSHAL_STR(lldpd_port_set, local_id)
320 MARSHAL_STR(lldpd_port_set, local_descr)
321 #ifdef ENABLE_LLDPMED
322 MARSHAL_POINTER(lldpd_port_set, lldpd_med_policy, med_policy)
323 MARSHAL_POINTER(lldpd_port_set, lldpd_med_loc, med_location)
324 MARSHAL_POINTER(lldpd_port_set, lldpd_med_power, med_power)
325 #endif
326 #ifdef ENABLE_DOT3
327 MARSHAL_POINTER(lldpd_port_set, lldpd_dot3_power, dot3_power)
328 #endif
329 #ifdef ENABLE_CUSTOM
330 MARSHAL_POINTER(lldpd_port_set, lldpd_custom, custom)
331 #endif
332 MARSHAL_END(lldpd_port_set);
333
334 /* Smart mode / Hide mode */
335 #define SMART_INCOMING_FILTER (1<<0) /* Incoming filtering enabled */
336 #define SMART_INCOMING_ONE_PROTO (1<<1) /* On reception, keep only one proto */
337 #define SMART_INCOMING_ONE_NEIGH (1<<2) /* On reception, keep only one neighbor */
338 #define SMART_OUTGOING_FILTER (1<<3) /* Outgoing filtering enabled */
339 #define SMART_OUTGOING_ONE_PROTO (1<<4) /* On emission, keep only one proto */
340 #define SMART_OUTGOING_ONE_NEIGH (1<<5) /* On emission, consider only one neighbor */
341 #define SMART_INCOMING (SMART_INCOMING_FILTER | \
342 SMART_INCOMING_ONE_PROTO | \
343 SMART_INCOMING_ONE_NEIGH)
344 #define SMART_OUTGOING (SMART_OUTGOING_FILTER | \
345 SMART_OUTGOING_ONE_PROTO | \
346 SMART_OUTGOING_ONE_NEIGH)
347
348 struct lldpd_config {
349 int c_paused; /* lldpd is paused */
350 int c_tx_interval; /* Transmit interval */
351 int c_smart; /* Bitmask for smart configuration (see SMART_*) */
352 int c_receiveonly; /* Receive only mode */
353 int c_max_neighbors; /* Maximum number of neighbors (per protocol) */
354
355 char *c_mgmt_pattern; /* Pattern to match a management address */
356 char *c_cid_pattern; /* Pattern to match interfaces to use for chassis ID */
357 char *c_iface_pattern; /* Pattern to match interfaces to use */
358
359 char *c_platform; /* Override platform description (for CDP) */
360 char *c_description; /* Override chassis description */
361 char *c_hostname; /* Override system name */
362 int c_advertise_version; /* Should the precise version be advertised? */
363 int c_set_ifdescr; /* Set interface description */
364 int c_promisc; /* Interfaces should be in promiscuous mode */
365 int c_cap_advertise; /* Chassis capabilities advertisement */
366 int c_mgmt_advertise; /* Management addresses advertisement */
367
368 #ifdef ENABLE_LLDPMED
369 int c_noinventory; /* Don't send inventory with LLDP-MED */
370 int c_enable_fast_start; /* enable fast start */
371 int c_tx_fast_init; /* Num of lldpd lldppdu's for fast start */
372 int c_tx_fast_interval; /* Time intr between sends during fast start */
373 #endif
374 int c_tx_hold; /* Transmit hold */
375 int c_bond_slave_src_mac_type; /* Src mac type in lldp frames over bond
376 slaves */
377 int c_lldp_portid_type; /* The PortID type */
378 };
379 MARSHAL_BEGIN(lldpd_config)
380 MARSHAL_STR(lldpd_config, c_mgmt_pattern)
381 MARSHAL_STR(lldpd_config, c_cid_pattern)
382 MARSHAL_STR(lldpd_config, c_iface_pattern)
383 MARSHAL_STR(lldpd_config, c_hostname)
384 MARSHAL_STR(lldpd_config, c_platform)
385 MARSHAL_STR(lldpd_config, c_description)
386 MARSHAL_END(lldpd_config);
387
388 struct lldpd_frame {
389 int size;
390 unsigned char frame[1];
391 };
392
393 struct lldpd_hardware;
394 struct lldpd;
395 struct lldpd_ops {
396 int(*send)(struct lldpd *,
397 struct lldpd_hardware*,
398 char *, size_t); /* Function to send a frame */
399 int(*recv)(struct lldpd *,
400 struct lldpd_hardware*,
401 int, char *, size_t); /* Function to receive a frame */
402 int(*cleanup)(struct lldpd *, struct lldpd_hardware *); /* Cleanup function. */
403 };
404
405 /* An interface is uniquely identified by h_ifindex, h_ifname and h_ops. This
406 * means if an interface becomes enslaved, it will be considered as a new
407 * interface. The same applies for renaming and we include the index in case of
408 * renaming to an existing interface. */
409 struct lldpd_hardware {
410 TAILQ_ENTRY(lldpd_hardware) h_entries;
411
412 struct lldpd *h_cfg; /* Pointer to main configuration */
413 void *h_recv; /* FD for reception */
414 int h_sendfd; /* FD for sending, only used by h_ops */
415 int h_mangle; /* 1 if we have to mangle the MAC address */
416 struct lldpd_ops *h_ops; /* Hardware-dependent functions */
417 void *h_data; /* Hardware-dependent data */
418 void *h_timer; /* Timer for this port */
419
420 int h_mtu;
421 int h_flags; /* Packets will be sent only
422 if IFF_RUNNING. Will be
423 removed if this is left
424 to 0. */
425 int h_ifindex; /* Interface index, used by SNMP */
426 char h_ifname[IFNAMSIZ]; /* Should be unique */
427 u_int8_t h_lladdr[ETHER_ADDR_LEN];
428
429 u_int64_t h_tx_cnt;
430 u_int64_t h_rx_cnt;
431 u_int64_t h_rx_discarded_cnt;
432 u_int64_t h_rx_unrecognized_cnt;
433 u_int64_t h_ageout_cnt;
434 u_int64_t h_insert_cnt;
435 u_int64_t h_delete_cnt;
436 u_int64_t h_drop_cnt;
437
438 /* Previous values of different stuff. */
439 /* Backup of the previous local port. Used to check if there was a
440 * change to send an immediate update. All those are not marshalled to
441 * the client. */
442 void *h_lport_previous;
443 ssize_t h_lport_previous_len;
444 /* Backup of the previous chassis ID. Used to check if there was a
445 * change and send an LLDP shutdown. */
446 u_int8_t h_lchassis_previous_id_subtype;
447 char *h_lchassis_previous_id;
448 int h_lchassis_previous_id_len;
449 /* Backup of the previous port ID. Used to check if there was a change
450 * and send an LLDP shutdown. */
451 u_int8_t h_lport_previous_id_subtype;
452 char *h_lport_previous_id;
453 int h_lport_previous_id_len;
454
455 struct lldpd_port h_lport; /* Port attached to this hardware port */
456 TAILQ_HEAD(, lldpd_port) h_rports; /* Remote ports */
457
458 #ifdef ENABLE_LLDPMED
459 int h_tx_fast; /* current tx fast start count */
460 #endif
461 };
462 MARSHAL_BEGIN(lldpd_hardware)
463 MARSHAL_IGNORE(lldpd_hardware, h_entries.tqe_next)
464 MARSHAL_IGNORE(lldpd_hardware, h_entries.tqe_prev)
465 MARSHAL_IGNORE(lldpd_hardware, h_ops)
466 MARSHAL_IGNORE(lldpd_hardware, h_data)
467 MARSHAL_IGNORE(lldpd_hardware, h_cfg)
468 MARSHAL_IGNORE(lldpd_hardware, h_lport_previous)
469 MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_len)
470 MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id_subtype)
471 MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id)
472 MARSHAL_IGNORE(lldpd_hardware, h_lchassis_previous_id_len)
473 MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id_subtype)
474 MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id)
475 MARSHAL_IGNORE(lldpd_hardware, h_lport_previous_id_len)
476 MARSHAL_SUBSTRUCT(lldpd_hardware, lldpd_port, h_lport)
477 MARSHAL_SUBTQ(lldpd_hardware, lldpd_port, h_rports)
478 MARSHAL_END(lldpd_hardware);
479
480 struct lldpd_interface {
481 TAILQ_ENTRY(lldpd_interface) next;
482 char *name;
483 };
484 MARSHAL_BEGIN(lldpd_interface)
485 MARSHAL_TQE(lldpd_interface, next)
486 MARSHAL_STR(lldpd_interface, name)
487 MARSHAL_END(lldpd_interface);
488 TAILQ_HEAD(lldpd_interface_list, lldpd_interface);
489 MARSHAL_TQ(lldpd_interface_list, lldpd_interface);
490
491 struct lldpd_neighbor_change {
492 char *ifname;
493 #define NEIGHBOR_CHANGE_DELETED -1
494 #define NEIGHBOR_CHANGE_ADDED 1
495 #define NEIGHBOR_CHANGE_UPDATED 0
496 int state;
497 struct lldpd_port *neighbor;
498 };
499 MARSHAL_BEGIN(lldpd_neighbor_change)
500 MARSHAL_STR(lldpd_neighbor_change, ifname)
501 MARSHAL_POINTER(lldpd_neighbor_change, lldpd_port, neighbor)
502 MARSHAL_END(lldpd_neighbor_change);
503
504 /* Cleanup functions */
505 void lldpd_chassis_mgmt_cleanup(struct lldpd_chassis *);
506 void lldpd_chassis_cleanup(struct lldpd_chassis *, int);
507 void lldpd_remote_cleanup(struct lldpd_hardware *,
508 void(*expire)(struct lldpd_hardware *, struct lldpd_port *),
509 int);
510 void lldpd_port_cleanup(struct lldpd_port *, int);
511 void lldpd_config_cleanup(struct lldpd_config *);
512 #ifdef ENABLE_DOT1
513 void lldpd_ppvid_cleanup(struct lldpd_port *);
514 void lldpd_vlan_cleanup(struct lldpd_port *);
515 void lldpd_pi_cleanup(struct lldpd_port *);
516 #endif
517 #ifdef ENABLE_CUSTOM
518 void lldpd_custom_list_cleanup(struct lldpd_port *);
519 #endif
520
521 #endif