]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/daemon/lldpd.h
lldpd: cleanup phase is done in `update_local_ports()`
[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
5d6aba3e 20#define _GNU_SOURCE 1
4b292b55
VB
21
22#if HAVE_CONFIG_H
23# include <config.h>
24#endif
25
26#ifdef HAVE_VALGRIND_VALGRIND_H
27# include <valgrind/valgrind.h>
28#else
29# define RUNNING_ON_VALGRIND 0
30#endif
31
4b292b55
VB
32#include <stdlib.h>
33#include <stddef.h>
34#include <string.h>
35#include <sys/queue.h>
e12c2365 36#include <sys/types.h>
690b944c 37#include <netinet/if_ether.h>
4b292b55 38#include <netinet/in.h>
4b292b55
VB
39#include <sys/un.h>
40
41#include "lldp-tlv.h"
42#if defined (ENABLE_CDP) || defined (ENABLE_FDP)
43# include "cdp.h"
44#endif
45#ifdef ENABLE_SONMP
46# include "sonmp.h"
47#endif
48#ifdef ENABLE_EDP
49# include "edp.h"
50#endif
51
52#include "../compat/compat.h"
53#include "../marshal.h"
54#include "../log.h"
55#include "../ctl.h"
56#include "../lldpd-structs.h"
57
58/* We don't want to import event2/event.h. We only need those as
59 opaque structs. */
60struct event;
61struct event_base;
62
63#define SYSFS_CLASS_NET "/sys/class/net/"
64#define SYSFS_CLASS_DMI "/sys/class/dmi/id/"
65#define LLDPD_TTL 120
8843f168 66#define LLDPD_TX_INTERVAL 30
4b292b55
VB
67#define LLDPD_TX_MSGDELAY 1
68#define LLDPD_PID_FILE "/var/run/lldpd.pid"
69
70#define USING_AGENTX_SUBAGENT_MODULE 1
71
72#define PROTO_SEND_SIG struct lldpd *, struct lldpd_hardware *
73#define PROTO_DECODE_SIG struct lldpd *, char *, int, struct lldpd_hardware *, struct lldpd_chassis **, struct lldpd_port **
74#define PROTO_GUESS_SIG char *, int
75
76struct protocol {
77 int mode; /* > 0 mode identifier (unique per protocol) */
78 int enabled; /* Is this protocol enabled? */
79 char *name; /* Name of protocol */
80 char arg; /* Argument to enable this protocol */
81 int(*send)(PROTO_SEND_SIG); /* How to send a frame */
82 int(*decode)(PROTO_DECODE_SIG); /* How to decode a frame */
83 int(*guess)(PROTO_GUESS_SIG); /* Can be NULL, use MAC address in this case */
4e5f34c5 84 u_int8_t mac[ETHER_ADDR_LEN]; /* Destination MAC address used by this protocol */
4b292b55
VB
85};
86
4b292b55
VB
87#define SMART_HIDDEN(port) (port->p_hidden_in)
88
89struct lldpd {
90 int g_sock;
4b292b55
VB
91 struct event_base *g_base;
92#ifdef USE_SNMP
93#endif
94
8ec333bd
VB
95 struct lldpd_config g_config;
96
4b292b55
VB
97 struct protocol *g_protocols;
98 time_t g_lastsent;
99 int g_lastrid;
4b292b55
VB
100 struct event *g_main_loop;
101#ifdef USE_SNMP
102 int g_snmp;
103 struct event *g_snmp_timeout;
104 void *g_snmp_fds;
105 char *g_snmp_agentx;
106#endif /* USE_SNMP */
107
108 /* Unix socket handling */
109 int g_ctl;
110 struct event *g_ctl_event;
111
4b292b55 112 char *g_lsb_release;
4b292b55
VB
113
114#define LOCAL_CHASSIS(cfg) ((struct lldpd_chassis *)(TAILQ_FIRST(&cfg->g_chassis)))
115 TAILQ_HEAD(, lldpd_chassis) g_chassis;
116 TAILQ_HEAD(, lldpd_hardware) g_hardware;
117};
118
4b292b55
VB
119/* lldpd.c */
120struct lldpd_hardware *lldpd_get_hardware(struct lldpd *,
121 char *, int, struct lldpd_ops *);
e12c2365 122struct lldpd_hardware *lldpd_alloc_hardware(struct lldpd *, char *, int);
4b292b55
VB
123void lldpd_hardware_cleanup(struct lldpd*, struct lldpd_hardware *);
124struct lldpd_mgmt *lldpd_alloc_mgmt(int family, void *addr, size_t addrsize, u_int32_t iface);
125void lldpd_recv(struct lldpd *, struct lldpd_hardware *, int);
126void lldpd_loop(struct lldpd *);
127int lldpd_main(int, char **);
128
129/* event.c */
130void levent_loop(struct lldpd *);
131void levent_hardware_init(struct lldpd_hardware *);
132void levent_hardware_add_fd(struct lldpd_hardware *, int);
133void levent_hardware_release(struct lldpd_hardware *);
4e90a9e0 134void levent_ctl_notify(char *, int, struct lldpd_port *);
47287a61 135void levent_send_now(struct lldpd *);
4b292b55
VB
136
137/* lldp.c */
138int lldp_send(PROTO_SEND_SIG);
139int lldp_decode(PROTO_DECODE_SIG);
140
141/* cdp.c */
142#ifdef ENABLE_CDP
143int cdpv1_send(PROTO_SEND_SIG);
144int cdpv2_send(PROTO_SEND_SIG);
145int cdpv1_guess(PROTO_GUESS_SIG);
146int cdpv2_guess(PROTO_GUESS_SIG);
147#endif
148#if defined (ENABLE_CDP) || defined (ENABLE_FDP)
149int cdp_decode(PROTO_DECODE_SIG);
150#endif
151#ifdef ENABLE_FDP
152int fdp_send(PROTO_SEND_SIG);
153#endif
154
155#ifdef ENABLE_SONMP
156/* sonmp.c */
157int sonmp_send(PROTO_SEND_SIG);
158int sonmp_decode(PROTO_DECODE_SIG);
159#endif
160
161#ifdef ENABLE_EDP
162/* edp.c */
163int edp_send(PROTO_SEND_SIG);
164int edp_decode(PROTO_DECODE_SIG);
165#endif
166
4b292b55
VB
167/* dmi.c */
168#ifdef ENABLE_LLDPMED
4b292b55
VB
169char *dmi_hw(void);
170char *dmi_fw(void);
171char *dmi_sn(void);
172char *dmi_manuf(void);
173char *dmi_model(void);
174char *dmi_asset(void);
175#endif
4b292b55 176
25de85a4 177#ifdef USE_SNMP
4b292b55
VB
178/* agent.c */
179void agent_shutdown(void);
180void agent_init(struct lldpd *, char *);
25de85a4
VB
181void agent_notify(struct lldpd_hardware *, int, struct lldpd_port *);
182#endif
4b292b55
VB
183
184/* agent_priv.c */
185void agent_priv_register_domain(void);
186
187/* client.c */
e0478a46
VB
188int
189client_handle_client(struct lldpd *cfg,
190 ssize_t(*send)(void *, int, void *, size_t),
191 void *,
4e90a9e0
VB
192 enum hmsg_type type, void *buffer, size_t n,
193 int*);
4b292b55
VB
194
195/* priv.c */
196void priv_init(char*, int, uid_t, gid_t);
197void priv_ctl_cleanup(void);
198char *priv_gethostbyname(void);
e12c2365 199#ifdef HOST_OS_LINUX
70c9cb05 200int priv_open(char*);
e12c2365
VB
201int priv_ethtool(char*, void*, size_t);
202#endif
203int priv_iface_init(int);
4b292b55
VB
204int priv_iface_multicast(const char *, u_int8_t *, int);
205int priv_snmp_socket(struct sockaddr_un *);
206
207/* privsep_fdpass.c */
208int receive_fd(int);
209void send_fd(int, int);
210
e12c2365 211/* interfaces-*.c */
e12c2365 212
adbb6e54
VB
213/* BPF filter to get revelant information from interfaces */
214/* LLDP: "ether proto 0x88cc and ether dst 01:80:c2:00:00:0e" */
215/* FDP: "ether dst 01:e0:52:cc:cc:cc" */
216/* CDP: "ether dst 01:00:0c:cc:cc:cc" */
217/* SONMP: "ether dst 01:00:81:00:01:00" */
218/* EDP: "ether dst 00:e0:2b:00:00:00" */
219/* For optimization purpose, we first check if the first bit of the
220 first byte is 1. if not, this can only be an EDP packet:
221
222 tcpdump -dd "(ether[0] & 1 = 1 and
223 ((ether proto 0x88cc and ether dst 01:80:c2:00:00:0e) or
224 (ether dst 01:e0:52:cc:cc:cc) or
225 (ether dst 01:00:0c:cc:cc:cc) or
226 (ether dst 01:00:81:00:01:00))) or
227 (ether dst 00:e0:2b:00:00:00)"
228*/
229
230#define LLDPD_FILTER_F \
231 { 0x30, 0, 0, 0x00000000 }, \
232 { 0x54, 0, 0, 0x00000001 }, \
233 { 0x15, 0, 14, 0x00000001 }, \
234 { 0x28, 0, 0, 0x0000000c }, \
235 { 0x15, 0, 4, 0x000088cc }, \
236 { 0x20, 0, 0, 0x00000002 }, \
237 { 0x15, 0, 2, 0xc200000e }, \
238 { 0x28, 0, 0, 0x00000000 }, \
239 { 0x15, 12, 13, 0x00000180 }, \
240 { 0x20, 0, 0, 0x00000002 }, \
241 { 0x15, 0, 2, 0x52cccccc }, \
242 { 0x28, 0, 0, 0x00000000 }, \
243 { 0x15, 8, 9, 0x000001e0 }, \
244 { 0x15, 1, 0, 0x0ccccccc }, \
245 { 0x15, 0, 2, 0x81000100 }, \
246 { 0x28, 0, 0, 0x00000000 }, \
247 { 0x15, 4, 5, 0x00000100 }, \
248 { 0x20, 0, 0, 0x00000002 }, \
249 { 0x15, 0, 3, 0x2b000000 }, \
250 { 0x28, 0, 0, 0x00000000 }, \
251 { 0x15, 0, 1, 0x000000e0 }, \
252 { 0x6, 0, 0, 0x0000ffff }, \
253 { 0x6, 0, 0, 0x00000000 },
254
255/* This function is responsible to refresh information about interfaces. It is
256 * OS specific but should be present for each OS. It can use the functions in
257 * `interfaces.c` as helper by providing a list of OS-independent interface
258 * devices. */
259void interfaces_update(struct lldpd *);
adbb6e54
VB
260
261/* interfaces.c */
262/* An interface cannot be both physical and (bridge or bond or vlan) */
263#define IFACE_PHYSICAL_T (1 << 0) /* Physical interface */
264#define IFACE_BRIDGE_T (1 << 1) /* Bridge interface */
265#define IFACE_BOND_T (1 << 2) /* Bond interface */
266#define IFACE_VLAN_T (1 << 3) /* VLAN interface */
267#define IFACE_WIRELESS_T (1 << 4) /* Wireless interface */
268struct interfaces_device {
269 TAILQ_ENTRY(interfaces_device) next;
e12c2365
VB
270 int index; /* Index */
271 char *name; /* Name */
272 char *alias; /* Alias */
e12c2365 273 char *address; /* MAC address */
adbb6e54
VB
274 char *driver; /* Driver (for whitelisting purpose) */
275 int flags; /* Flags (IFF_*) */
276 int mtu; /* MTU */
277 int type; /* Type (see IFACE_*_T) */
278 int vlanid; /* If a VLAN, what is the VLAN ID? */
279 struct interfaces_device *lower; /* Lower interface (for a VLAN for example) */
280 struct interfaces_device *upper; /* Upper interface (for a bridge or a bond) */
281
282 /* The following are OS specific. Should be static (no free function) */
283#ifdef HOST_OS_LINUX
284 int lower_idx; /* Index to lower interface */
285 int upper_idx; /* Index to upper interface */
286 int txqueue; /* Transmit queue length */
287#endif
e12c2365 288};
adbb6e54
VB
289struct interfaces_address {
290 TAILQ_ENTRY(interfaces_address) next;
291 int index; /* Index */
292 int flags; /* Flags */
e12c2365 293 struct sockaddr_storage address; /* Address */
adbb6e54
VB
294
295 /* The following are OS specific. */
296 /* Nothing yet. */
e12c2365 297};
adbb6e54
VB
298TAILQ_HEAD(interfaces_device_list, interfaces_device);
299TAILQ_HEAD(interfaces_address_list, interfaces_address);
300void interfaces_free_device(struct interfaces_device *);
301void interfaces_free_address(struct interfaces_address *);
302void interfaces_free_devices(struct interfaces_device_list *);
303void interfaces_free_addresses(struct interfaces_address_list *);
304struct interfaces_device* interfaces_indextointerface(
305 struct interfaces_device_list *,
306 int);
307struct interfaces_device* interfaces_nametointerface(
308 struct interfaces_device_list *,
309 const char *);
310
311void interfaces_helper_whitelist(struct lldpd *,
312 struct interfaces_device_list *);
313void interfaces_helper_chassis(struct lldpd *,
314 struct interfaces_device_list *);
315void interfaces_helper_physical(struct lldpd *,
88bc404f 316 struct interfaces_device_list *,
22e8cd65 317 struct lldpd_ops *,
88bc404f 318 int(*init)(struct lldpd *, struct lldpd_hardware *));
adbb6e54
VB
319void interfaces_helper_port_name_desc(struct lldpd_hardware *,
320 struct interfaces_device *);
321void interfaces_helper_mgmt(struct lldpd *,
322 struct interfaces_address_list *);
323#ifdef ENABLE_DOT1
324void interfaces_helper_vlan(struct lldpd *,
325 struct interfaces_device_list *);
326#endif
327
328void interfaces_setup_multicast(struct lldpd *, const char *, int);
329
330#ifdef HOST_OS_LINUX
331/* netlink.c */
332struct interfaces_device_list *netlink_get_interfaces(void);
333struct interfaces_address_list *netlink_get_addresses(void);
e12c2365
VB
334#endif
335
4b292b55 336#endif /* _LLDPD_H */