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