]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/daemon/lldpd.h
redhat: ensure /etc/sysconfig/lldpd is correctly populated
[thirdparty/lldpd.git] / src / daemon / lldpd.h
CommitLineData
4b292b55
VB
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_H
19#define _LLDPD_H
20
21#if HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#ifdef HAVE_VALGRIND_VALGRIND_H
26# include <valgrind/valgrind.h>
27#else
28# define RUNNING_ON_VALGRIND 0
29#endif
30
4b292b55
VB
31#include <stdlib.h>
32#include <stddef.h>
33#include <string.h>
34#include <sys/queue.h>
e12c2365 35#include <sys/types.h>
690b944c 36#include <netinet/if_ether.h>
4b292b55 37#include <netinet/in.h>
4b292b55
VB
38#include <sys/un.h>
39
4c98fe72 40#ifdef HOST_OS_LINUX
56414594
VB
41# if defined(__clang__)
42# pragma clang diagnostic push
43# pragma clang diagnostic ignored "-Wdocumentation"
44# endif
4c98fe72 45# include <linux/ethtool.h>
56414594
VB
46# if defined(__clang__)
47# pragma clang diagnostic pop
48# endif
4c98fe72
VB
49#endif
50
45bf0bd0
VB
51#if HAVE_VFORK_H
52# include <vfork.h>
53#endif
54#if HAVE_WORKING_FORK
55# define vfork fork
56#endif
57
4b292b55
VB
58#include "lldp-tlv.h"
59#if defined (ENABLE_CDP) || defined (ENABLE_FDP)
9e2db64f 60# include "protocols/cdp.h"
4b292b55
VB
61#endif
62#ifdef ENABLE_SONMP
9e2db64f 63# include "protocols/sonmp.h"
4b292b55
VB
64#endif
65#ifdef ENABLE_EDP
9e2db64f 66# include "protocols/edp.h"
4b292b55
VB
67#endif
68
4c98fe72
VB
69
70
4b292b55
VB
71#include "../compat/compat.h"
72#include "../marshal.h"
73#include "../log.h"
74#include "../ctl.h"
75#include "../lldpd-structs.h"
76
77/* We don't want to import event2/event.h. We only need those as
78 opaque structs. */
79struct event;
80struct event_base;
81
82#define SYSFS_CLASS_NET "/sys/class/net/"
83#define SYSFS_CLASS_DMI "/sys/class/dmi/id/"
8843f168 84#define LLDPD_TX_INTERVAL 30
c10302a3 85#define LLDPD_TX_HOLD 4
86#define LLDPD_TTL LLDPD_TX_INTERVAL * LLDPD_TX_HOLD
4b292b55 87#define LLDPD_TX_MSGDELAY 1
42589660 88#define LLDPD_MAX_NEIGHBORS 4
b9de0ca6 89#define LLDPD_FAST_TX_INTERVAL 1
90#define LLDPD_FAST_INIT 4
4b292b55
VB
91
92#define USING_AGENTX_SUBAGENT_MODULE 1
93
94#define PROTO_SEND_SIG struct lldpd *, struct lldpd_hardware *
95#define PROTO_DECODE_SIG struct lldpd *, char *, int, struct lldpd_hardware *, struct lldpd_chassis **, struct lldpd_port **
96#define PROTO_GUESS_SIG char *, int
97
3d596bbe
VB
98#define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))
99
4b292b55
VB
100struct protocol {
101 int mode; /* > 0 mode identifier (unique per protocol) */
102 int enabled; /* Is this protocol enabled? */
103 char *name; /* Name of protocol */
104 char arg; /* Argument to enable this protocol */
105 int(*send)(PROTO_SEND_SIG); /* How to send a frame */
106 int(*decode)(PROTO_DECODE_SIG); /* How to decode a frame */
107 int(*guess)(PROTO_GUESS_SIG); /* Can be NULL, use MAC address in this case */
a98ed042
VB
108 u_int8_t mac1[ETHER_ADDR_LEN]; /* Destination MAC address used by this protocol */
109 u_int8_t mac2[ETHER_ADDR_LEN]; /* Destination MAC address used by this protocol */
110 u_int8_t mac3[ETHER_ADDR_LEN]; /* Destination MAC address used by this protocol */
4b292b55
VB
111};
112
4b292b55
VB
113#define SMART_HIDDEN(port) (port->p_hidden_in)
114
13181ede 115struct lldpd;
4b292b55 116
4b292b55
VB
117/* lldpd.c */
118struct lldpd_hardware *lldpd_get_hardware(struct lldpd *,
32945d6a 119 char *, int);
e12c2365 120struct lldpd_hardware *lldpd_alloc_hardware(struct lldpd *, char *, int);
4b292b55
VB
121void lldpd_hardware_cleanup(struct lldpd*, struct lldpd_hardware *);
122struct lldpd_mgmt *lldpd_alloc_mgmt(int family, void *addr, size_t addrsize, u_int32_t iface);
123void lldpd_recv(struct lldpd *, struct lldpd_hardware *, int);
579bedd5 124void lldpd_send(struct lldpd_hardware *);
4b292b55 125void lldpd_loop(struct lldpd *);
1e0d651f 126int lldpd_main(int, char **, char **);
0484f180 127void lldpd_update_localports(struct lldpd *);
3333d2a8 128void lldpd_cleanup(struct lldpd *);
0484f180 129
579bedd5
VB
130/* frame.c */
131u_int16_t frame_checksum(const u_int8_t *, int, int);
4b292b55
VB
132
133/* event.c */
134void levent_loop(struct lldpd *);
f144d837 135void levent_shutdown(struct lldpd *);
4b292b55
VB
136void levent_hardware_init(struct lldpd_hardware *);
137void levent_hardware_add_fd(struct lldpd_hardware *, int);
138void levent_hardware_release(struct lldpd_hardware *);
4e90a9e0 139void levent_ctl_notify(char *, int, struct lldpd_port *);
47287a61 140void levent_send_now(struct lldpd *);
e681c859 141void levent_update_now(struct lldpd *);
aa313f2a 142int levent_iface_subscribe(struct lldpd *, int);
579bedd5 143void levent_schedule_pdu(struct lldpd_hardware *);
3333d2a8 144void levent_schedule_cleanup(struct lldpd *);
bec75f84 145int levent_make_socket_nonblocking(int);
327b1d62 146int levent_make_socket_blocking(int);
4b292b55
VB
147
148/* lldp.c */
e770b720 149int lldp_send_shutdown(PROTO_SEND_SIG);
4b292b55
VB
150int lldp_send(PROTO_SEND_SIG);
151int lldp_decode(PROTO_DECODE_SIG);
152
153/* cdp.c */
154#ifdef ENABLE_CDP
155int cdpv1_send(PROTO_SEND_SIG);
156int cdpv2_send(PROTO_SEND_SIG);
157int cdpv1_guess(PROTO_GUESS_SIG);
158int cdpv2_guess(PROTO_GUESS_SIG);
159#endif
160#if defined (ENABLE_CDP) || defined (ENABLE_FDP)
161int cdp_decode(PROTO_DECODE_SIG);
162#endif
163#ifdef ENABLE_FDP
164int fdp_send(PROTO_SEND_SIG);
165#endif
166
167#ifdef ENABLE_SONMP
168/* sonmp.c */
169int sonmp_send(PROTO_SEND_SIG);
170int sonmp_decode(PROTO_DECODE_SIG);
171#endif
172
173#ifdef ENABLE_EDP
174/* edp.c */
175int edp_send(PROTO_SEND_SIG);
176int edp_decode(PROTO_DECODE_SIG);
177#endif
178
4b292b55
VB
179/* dmi.c */
180#ifdef ENABLE_LLDPMED
4b292b55
VB
181char *dmi_hw(void);
182char *dmi_fw(void);
183char *dmi_sn(void);
184char *dmi_manuf(void);
185char *dmi_model(void);
186char *dmi_asset(void);
187#endif
4b292b55 188
25de85a4 189#ifdef USE_SNMP
4b292b55
VB
190/* agent.c */
191void agent_shutdown(void);
83d4b776 192void agent_init(struct lldpd *, const char *);
25de85a4
VB
193void agent_notify(struct lldpd_hardware *, int, struct lldpd_port *);
194#endif
4b292b55 195
71a7dbb3 196#ifdef ENABLE_PRIVSEP
4b292b55
VB
197/* agent_priv.c */
198void agent_priv_register_domain(void);
71a7dbb3 199#endif
4b292b55
VB
200
201/* client.c */
e0478a46
VB
202int
203client_handle_client(struct lldpd *cfg,
204 ssize_t(*send)(void *, int, void *, size_t),
205 void *,
4e90a9e0
VB
206 enum hmsg_type type, void *buffer, size_t n,
207 int*);
4b292b55
VB
208
209/* priv.c */
6fd393a2 210void priv_init(const char*, int, uid_t, gid_t);
71a7dbb3 211void priv_wait(void);
0262adbb 212void priv_ctl_cleanup(const char *ctlname);
1fa7d39f 213char *priv_gethostname(void);
e12c2365 214#ifdef HOST_OS_LINUX
38cbcf2b
VB
215#define ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32 (SCHAR_MAX)
216#define ETHTOOL_DECLARE_LINK_MODE_MASK(name) \
217 uint32_t name[ETHTOOL_LINK_MODE_MASK_MAX_KERNEL_NU32]
218
219struct ethtool_link_usettings {
220 struct ethtool_link_settings base;
221 struct {
222 ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
223 ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
224 ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
225 } link_modes;
226};
70c9cb05 227int priv_open(char*);
4ea0565e 228void asroot_open(void);
38cbcf2b 229int priv_ethtool(char*, struct ethtool_link_usettings*);
4ea0565e 230void asroot_ethtool(void);
e12c2365 231#endif
e735a319 232int priv_iface_init(int, char *);
4ea0565e 233int asroot_iface_init_os(int, char *, int *);
a98ed042 234int priv_iface_multicast(const char *, const u_int8_t *, int);
47820fc4
VB
235int priv_iface_description(const char *, const char *);
236int asroot_iface_description_os(const char *, const char *);
f84199dd
VB
237int priv_iface_promisc(const char*);
238int asroot_iface_promisc_os(const char *);
4b292b55
VB
239int priv_snmp_socket(struct sockaddr_un *);
240
065732ca 241enum priv_cmd {
4ea0565e
VB
242 PRIV_PING,
243 PRIV_DELETE_CTL_SOCKET,
244 PRIV_GET_HOSTNAME,
245 PRIV_OPEN,
246 PRIV_ETHTOOL,
247 PRIV_IFACE_INIT,
248 PRIV_IFACE_MULTICAST,
47820fc4 249 PRIV_IFACE_DESCRIPTION,
f84199dd 250 PRIV_IFACE_PROMISC,
4ea0565e 251 PRIV_SNMP_SOCKET,
065732ca 252};
4ea0565e 253
00e40dba 254/* priv-seccomp.c */
71a7dbb3 255#if defined USE_SECCOMP && defined ENABLE_PRIVSEP
00e40dba
VB
256int priv_seccomp_init(int, int);
257#endif
4ea0565e
VB
258
259/* privsep_io.c */
d2a289bb
VB
260enum priv_context {
261 PRIV_PRIVILEGED,
262 PRIV_UNPRIVILEGED
263};
264int may_read(enum priv_context, void *, size_t);
265void must_read(enum priv_context, void *, size_t);
266void must_write(enum priv_context, const void *, size_t);
267void priv_privileged_fd(int);
268void priv_unprivileged_fd(int);
327b1d62 269int priv_fd(enum priv_context);
d2a289bb
VB
270int receive_fd(enum priv_context);
271void send_fd(enum priv_context, int);
4b292b55 272
e12c2365 273/* interfaces-*.c */
e12c2365 274
adbb6e54
VB
275/* BPF filter to get revelant information from interfaces */
276/* LLDP: "ether proto 0x88cc and ether dst 01:80:c2:00:00:0e" */
277/* FDP: "ether dst 01:e0:52:cc:cc:cc" */
278/* CDP: "ether dst 01:00:0c:cc:cc:cc" */
279/* SONMP: "ether dst 01:00:81:00:01:00" */
280/* EDP: "ether dst 00:e0:2b:00:00:00" */
281/* For optimization purpose, we first check if the first bit of the
282 first byte is 1. if not, this can only be an EDP packet:
283
284 tcpdump -dd "(ether[0] & 1 = 1 and
a98ed042
VB
285 ((ether proto 0x88cc and (ether dst 01:80:c2:00:00:0e or
286 ether dst 01:80:c2:00:00:03 or
287 ether dst 01:80:c2:00:00:00)) or
adbb6e54
VB
288 (ether dst 01:e0:52:cc:cc:cc) or
289 (ether dst 01:00:0c:cc:cc:cc) or
290 (ether dst 01:00:81:00:01:00))) or
291 (ether dst 00:e0:2b:00:00:00)"
292*/
293
294#define LLDPD_FILTER_F \
295 { 0x30, 0, 0, 0x00000000 }, \
296 { 0x54, 0, 0, 0x00000001 }, \
a98ed042 297 { 0x15, 0, 16, 0x00000001 }, \
adbb6e54 298 { 0x28, 0, 0, 0x0000000c }, \
a98ed042 299 { 0x15, 0, 6, 0x000088cc }, \
adbb6e54 300 { 0x20, 0, 0, 0x00000002 }, \
a98ed042
VB
301 { 0x15, 2, 0, 0xc200000e }, \
302 { 0x15, 1, 0, 0xc2000003 }, \
303 { 0x15, 0, 2, 0xc2000000 }, \
adbb6e54
VB
304 { 0x28, 0, 0, 0x00000000 }, \
305 { 0x15, 12, 13, 0x00000180 }, \
306 { 0x20, 0, 0, 0x00000002 }, \
307 { 0x15, 0, 2, 0x52cccccc }, \
308 { 0x28, 0, 0, 0x00000000 }, \
309 { 0x15, 8, 9, 0x000001e0 }, \
310 { 0x15, 1, 0, 0x0ccccccc }, \
311 { 0x15, 0, 2, 0x81000100 }, \
312 { 0x28, 0, 0, 0x00000000 }, \
313 { 0x15, 4, 5, 0x00000100 }, \
314 { 0x20, 0, 0, 0x00000002 }, \
315 { 0x15, 0, 3, 0x2b000000 }, \
316 { 0x28, 0, 0, 0x00000000 }, \
317 { 0x15, 0, 1, 0x000000e0 }, \
a98ed042
VB
318 { 0x6, 0, 0, 0x00040000 }, \
319 { 0x6, 0, 0, 0x00000000 }
adbb6e54
VB
320
321/* This function is responsible to refresh information about interfaces. It is
322 * OS specific but should be present for each OS. It can use the functions in
323 * `interfaces.c` as helper by providing a list of OS-independent interface
324 * devices. */
325void interfaces_update(struct lldpd *);
adbb6e54
VB
326
327/* interfaces.c */
328/* An interface cannot be both physical and (bridge or bond or vlan) */
329#define IFACE_PHYSICAL_T (1 << 0) /* Physical interface */
330#define IFACE_BRIDGE_T (1 << 1) /* Bridge interface */
331#define IFACE_BOND_T (1 << 2) /* Bond interface */
332#define IFACE_VLAN_T (1 << 3) /* VLAN interface */
333#define IFACE_WIRELESS_T (1 << 4) /* Wireless interface */
334struct interfaces_device {
335 TAILQ_ENTRY(interfaces_device) next;
0fa2254b 336 int ignore; /* Ignore this interface */
e12c2365
VB
337 int index; /* Index */
338 char *name; /* Name */
339 char *alias; /* Alias */
e12c2365 340 char *address; /* MAC address */
adbb6e54
VB
341 char *driver; /* Driver (for whitelisting purpose) */
342 int flags; /* Flags (IFF_*) */
343 int mtu; /* MTU */
344 int type; /* Type (see IFACE_*_T) */
345 int vlanid; /* If a VLAN, what is the VLAN ID? */
346 struct interfaces_device *lower; /* Lower interface (for a VLAN for example) */
347 struct interfaces_device *upper; /* Upper interface (for a bridge or a bond) */
348
349 /* The following are OS specific. Should be static (no free function) */
350#ifdef HOST_OS_LINUX
351 int lower_idx; /* Index to lower interface */
352 int upper_idx; /* Index to upper interface */
adbb6e54 353#endif
e12c2365 354};
adbb6e54
VB
355struct interfaces_address {
356 TAILQ_ENTRY(interfaces_address) next;
357 int index; /* Index */
358 int flags; /* Flags */
e12c2365 359 struct sockaddr_storage address; /* Address */
adbb6e54
VB
360
361 /* The following are OS specific. */
362 /* Nothing yet. */
e12c2365 363};
adbb6e54
VB
364TAILQ_HEAD(interfaces_device_list, interfaces_device);
365TAILQ_HEAD(interfaces_address_list, interfaces_address);
366void interfaces_free_device(struct interfaces_device *);
367void interfaces_free_address(struct interfaces_address *);
368void interfaces_free_devices(struct interfaces_device_list *);
369void interfaces_free_addresses(struct interfaces_address_list *);
370struct interfaces_device* interfaces_indextointerface(
371 struct interfaces_device_list *,
372 int);
373struct interfaces_device* interfaces_nametointerface(
374 struct interfaces_device_list *,
375 const char *);
376
f84199dd
VB
377void interfaces_helper_promisc(struct lldpd *,
378 struct lldpd_hardware *);
adbb6e54
VB
379void interfaces_helper_whitelist(struct lldpd *,
380 struct interfaces_device_list *);
381void interfaces_helper_chassis(struct lldpd *,
382 struct interfaces_device_list *);
bdfe4193
VB
383void interfaces_helper_add_hardware(struct lldpd *,
384 struct lldpd_hardware *);
adbb6e54 385void interfaces_helper_physical(struct lldpd *,
88bc404f 386 struct interfaces_device_list *,
22e8cd65 387 struct lldpd_ops *,
88bc404f 388 int(*init)(struct lldpd *, struct lldpd_hardware *));
8fbd3195
ST
389void interfaces_helper_port_name_desc(struct lldpd *,
390 struct lldpd_hardware *,
adbb6e54
VB
391 struct interfaces_device *);
392void interfaces_helper_mgmt(struct lldpd *,
393 struct interfaces_address_list *);
394#ifdef ENABLE_DOT1
395void interfaces_helper_vlan(struct lldpd *,
396 struct interfaces_device_list *);
397#endif
5347914e
VB
398int interfaces_send_helper(struct lldpd *,
399 struct lldpd_hardware *, char *, size_t);
adbb6e54
VB
400
401void interfaces_setup_multicast(struct lldpd *, const char *, int);
c3e340b6 402int interfaces_routing_enabled(struct lldpd *);
13181ede 403void interfaces_cleanup(struct lldpd *);
adbb6e54
VB
404
405#ifdef HOST_OS_LINUX
406/* netlink.c */
13181ede
VB
407struct interfaces_device_list *netlink_get_interfaces(struct lldpd *);
408struct interfaces_address_list *netlink_get_addresses(struct lldpd *);
409void netlink_cleanup(struct lldpd *);
410struct lldpd_netlink;
e12c2365
VB
411#endif
412
c3e340b6
VB
413#ifndef HOST_OS_LINUX
414int ifbpf_phys_init(struct lldpd *, struct lldpd_hardware *);
415#endif
416
d5e69431
VB
417/* pattern.c */
418int pattern_match(char *, char *, int);
419
13181ede
VB
420struct lldpd {
421 int g_sock;
422 struct event_base *g_base;
423#ifdef USE_SNMP
424#endif
425
426 struct lldpd_config g_config;
427
428 struct protocol *g_protocols;
429 int g_lastrid;
430 struct event *g_main_loop;
431 struct event *g_cleanup_timer;
432#ifdef USE_SNMP
433 int g_snmp;
434 struct event *g_snmp_timeout;
435 void *g_snmp_fds;
436 const char *g_snmp_agentx;
437#endif /* USE_SNMP */
438
439 /* Unix socket handling */
440 const char *g_ctlname;
441 int g_ctl;
442 struct event *g_iface_event; /* Triggered when there is an interface change */
443 struct event *g_iface_timer_event; /* Triggered one second after last interface change */
444 void(*g_iface_cb)(struct lldpd *); /* Called when there is an interface change */
445
446 char *g_lsb_release;
447
448#ifdef HOST_OS_LINUX
449 struct lldpd_netlink *g_netlink;
450#endif
451
9da663f7 452 struct lldpd_port *g_default_local_port;
13181ede
VB
453#define LOCAL_CHASSIS(cfg) ((struct lldpd_chassis *)(TAILQ_FIRST(&cfg->g_chassis)))
454 TAILQ_HEAD(, lldpd_chassis) g_chassis;
455 TAILQ_HEAD(, lldpd_hardware) g_hardware;
456};
457
4b292b55 458#endif /* _LLDPD_H */