]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/lldpd.h
More privilege separation
[thirdparty/lldpd.git] / src / lldpd.h
CommitLineData
43c02e7b
VB
1/*
2 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef _LLDPD_H
18#define _LLDPD_H
19
20#if HAVE_CONFIG_H
21 #include <config.h>
22#endif
23
24#define _GNU_SOURCE 1
25#include <stdlib.h>
26#include <string.h>
27#include <sys/queue.h>
28#ifndef INCLUDE_LINUX_IF_H
29#include <net/if.h>
30#else
31#include <arpa/inet.h>
32#include <linux/if.h>
33#endif
34#include <net/ethernet.h>
35#include <netinet/in.h>
4afe659e 36#include <linux/ethtool.h>
43c02e7b
VB
37
38#include "compat.h"
39#include "lldp.h"
40#include "cdp.h"
41#include "sonmp.h"
42#include "edp.h"
43
44#define LLDPD_TTL 120
45#define LLDPD_TX_DELAY 30
46#define LLDPD_TX_MSGDELAY 1
47#define LLDPD_CTL_SOCKET "/var/run/lldpd.socket"
48#define LLDPD_PID_FILE "/var/run/lldpd.pid"
49
50#define UNIX_PATH_MAX 108
51
52#define USING_AGENTX_SUBAGENT_MODULE 1
53
54struct lldpd_vlan {
55 TAILQ_ENTRY(lldpd_vlan) v_entries;
56 char *v_name;
57 u_int16_t v_vid;
58};
59#define STRUCT_LLDPD_VLAN "Lsw"
60
61struct lldpd_chassis {
62 u_int8_t c_id_subtype;
63 char *c_id;
64 int c_id_len;
65 char *c_name;
66 char *c_descr;
67
68 u_int16_t c_cap_available;
69 u_int16_t c_cap_enabled;
70
71 u_int16_t c_ttl;
72
73 struct in_addr c_mgmt;
74 u_int32_t c_mgmt_if;
75};
76#define STRUCT_LLDPD_CHASSIS "bCsswwwll"
77
78struct lldpd_port {
79 u_int8_t p_id_subtype;
80 char *p_id;
81 int p_id_len;
82 char *p_descr;
83
84 /* Dot3 stuff */
85 u_int32_t p_aggregid;
86 u_int8_t p_autoneg_support;
87 u_int8_t p_autoneg_enabled;
88 u_int16_t p_autoneg_advertised;
89 u_int16_t p_mau_type;
90
91 TAILQ_HEAD(, lldpd_vlan) p_vlans;
92};
93#define STRUCT_LLDPD_PORT "bCslbbwwPP"
94
95struct lldpd_frame {
96 int size;
97 unsigned char frame[];
98};
99
100struct lldpd_hardware {
101 TAILQ_ENTRY(lldpd_hardware) h_entries;
102
103#define INTERFACE_OPENED(x) ((x)->h_raw != -1)
104
105 int h_raw;
106 int h_raw_real; /* For bonding */
107 int h_master; /* For bonding */
108
109#define LLDPD_MODE_ANY 0
110#define LLDPD_MODE_LLDP 1
111#define LLDPD_MODE_CDPV1 2
112#define LLDPD_MODE_CDPV2 3
113#define LLDPD_MODE_SONMP 4
114#define LLDPD_MODE_EDP 5
031118c4 115#define LLDPD_MODE_FDP 6
43c02e7b
VB
116 int h_mode;
117
118 int h_flags;
119 int h_mtu;
120 char h_ifname[IFNAMSIZ];
121 u_int8_t h_lladdr[ETHER_ADDR_LEN];
122
123 u_int64_t h_tx_cnt;
124 u_int64_t h_rx_cnt;
125 u_int64_t h_rx_discarded_cnt;
126 u_int64_t h_rx_ageout_cnt;
37387046 127 u_int64_t h_rx_unrecognized_cnt;
43c02e7b
VB
128
129 u_int8_t *h_proto_macs;
130 time_t h_start_probe;
131
132 struct lldpd_port h_lport;
133 time_t h_llastchange;
134 struct lldpd_frame *h_llastframe;
135
136 time_t h_rlastchange;
137 time_t h_rlastupdate;
138 int h_rid;
139 struct lldpd_frame *h_rlastframe;
140 struct lldpd_port *h_rport;
141 struct lldpd_chassis *h_rchassis;
142};
143
50a89ca7
VB
144/* lldpd_vif can be casted to lldpd_hardware on some cases */
145struct lldpd_vif {
146 TAILQ_ENTRY(lldpd_vif) vif_entries;
147 int vif_raw;
148 int vif_raw_real; /* Not used */
149 int vif_master; /* Not used */
150 int vif_mode; /* Not used */
151 int vif_flags;
152 int vif_mtu;
153 char vif_ifname[IFNAMSIZ];
154
155 /* No more compatibility with struct lldpd_hardware from here */
156 struct lldpd_hardware *vif_real;
157};
158
43c02e7b
VB
159struct lldpd_interface {
160 TAILQ_ENTRY(lldpd_interface) next;
161 char *name;
162};
163#define STRUCT_LLDPD_INTERFACE "Ls"
164
165struct lldpd_client {
166 TAILQ_ENTRY(lldpd_client) next;
167 int fd;
168};
169
170#define PROTO_SEND_SIG struct lldpd *, struct lldpd_chassis *, struct lldpd_hardware *
171#define PROTO_DECODE_SIG struct lldpd *, char *, int, struct lldpd_hardware *, struct lldpd_chassis **, struct lldpd_port **
172#define PROTO_GUESS_SIG char *, int
173
174struct lldpd;
175struct protocol {
176 int mode; /* > 0 mode identifier (unique per protocol) */
177 int enabled; /* Is this protocol enabled? */
178 char *name; /* Name of protocol */
179 char arg; /* Argument to enable this protocol */
180 int(*send)(PROTO_SEND_SIG); /* How to send a frame */
181 int(*decode)(PROTO_DECODE_SIG); /* How to decode a frame */
182 int(*guess)(PROTO_GUESS_SIG); /* Can be NULL, use MAC address in this case */
183 u_int8_t mac[ETH_ALEN]; /* Destination MAC address used by this protocol */
184 struct sock_filter *filter; /* BPF filter */
185 size_t filterlen; /* Size of BPF filter */
186};
187
188struct lldpd {
189 int g_sock;
190 int g_delay;
191
192 struct protocol *g_protocols;
193 int g_multi; /* Set to 1 if multiple protocols */
194 int g_probe_time;
766f32b3 195 int g_listen_vlans;
43c02e7b
VB
196
197 time_t g_lastsent;
198 int g_lastrid;
199#ifdef USE_SNMP
200 int g_snmp;
201#endif /* USE_SNMP */
202
203 /* Unix socket handling */
204 int g_ctl;
205 TAILQ_HEAD(, lldpd_client) g_clients;
206
207 char *g_mgmt_pattern;
208
209 struct lldpd_chassis g_lchassis;
210
211 TAILQ_HEAD(, lldpd_hardware) g_hardware;
50a89ca7 212 TAILQ_HEAD(, lldpd_vif) g_vif;
43c02e7b
VB
213};
214
215enum hmsg_type {
216 HMSG_NONE,
217 HMSG_GET_INTERFACES,
218 HMSG_GET_CHASSIS,
219 HMSG_GET_PORT,
220 HMSG_GET_VLANS,
221 HMSG_SHUTDOWN
222};
223
224struct hmsg_hdr {
225 enum hmsg_type type;
226 int16_t len;
227 pid_t pid;
228} __attribute__ ((__packed__));
229
230struct hmsg {
231 struct hmsg_hdr hdr;
232 void *data;
233} __attribute__ ((__packed__));
234
235#define HMSG_HEADER_SIZE sizeof(struct hmsg_hdr)
236#define MAX_HMSGSIZE 8192
237
238/* lldpd.c */
239void lldpd_cleanup(struct lldpd *);
240void lldpd_vlan_cleanup(struct lldpd_port *);
241void lldpd_remote_cleanup(struct lldpd *, struct lldpd_hardware *, int);
242void lldpd_port_cleanup(struct lldpd_port *);
243void lldpd_chassis_cleanup(struct lldpd_chassis *);
244
245/* lldp.c */
246int lldp_send(PROTO_SEND_SIG);
247int lldp_decode(PROTO_DECODE_SIG);
248
249/* cdp.c */
250int cdpv1_send(PROTO_SEND_SIG);
251int cdpv2_send(PROTO_SEND_SIG);
031118c4 252int fdp_send(PROTO_SEND_SIG);
43c02e7b
VB
253int cdp_decode(PROTO_DECODE_SIG);
254int cdpv1_guess(PROTO_GUESS_SIG);
255int cdpv2_guess(PROTO_GUESS_SIG);
256
257/* sonmp.c */
258int sonmp_send(PROTO_SEND_SIG);
259int sonmp_decode(PROTO_DECODE_SIG);
260
261/* edp.c */
262int edp_send(PROTO_SEND_SIG);
263int edp_decode(PROTO_DECODE_SIG);
264
265/* ctl.c */
b5562b23 266int ctl_create(char *);
43c02e7b 267int ctl_connect(char *);
b5562b23 268void ctl_cleanup(char *);
43c02e7b
VB
269int ctl_accept(struct lldpd *, int);
270int ctl_close(struct lldpd *, int);
271void ctl_msg_init(struct hmsg *, enum hmsg_type);
272int ctl_msg_send(int, struct hmsg *);
273int ctl_msg_recv(int, struct hmsg *);
274int ctl_msg_pack_list(char *, void *, unsigned int, struct hmsg *, void **);
275int ctl_msg_unpack_list(char *, void *, unsigned int, struct hmsg *, void **);
276int ctl_msg_pack_structure(char *, void *, unsigned int, struct hmsg *, void **);
277int ctl_msg_unpack_structure(char *, void *, unsigned int, struct hmsg *, void **);
278
279/* features.c */
280int iface_is_bridge(struct lldpd *, const char *);
281int iface_is_bridged(struct lldpd *, const char *);
282int iface_is_wireless(struct lldpd *, const char *);
283int iface_is_vlan(struct lldpd *, const char *);
284int iface_is_bond(struct lldpd *, const char *);
285int iface_is_bond_slave(struct lldpd *,
286 const char *, const char *);
287int iface_is_enslaved(struct lldpd *, const char *);
288
289/* log.c */
290void log_init(int);
291void log_warn(const char *, ...);
292#define LLOG_WARN(x,...) log_warn("%s: " x, __FUNCTION__, ##__VA_ARGS__)
293void log_warnx(const char *, ...);
294#define LLOG_WARNX(x,...) log_warnx("%s: " x, __FUNCTION__, ##__VA_ARGS__)
295void log_info(const char *, ...);
296#define LLOG_INFO(x,...) log_info("%s: " x, __FUNCTION__, ##__VA_ARGS__)
297void log_debug(const char *, ...);
298#define LLOG_DEBUG(x,...) log_debug("%s: " x, __FUNCTION__, ##__VA_ARGS__)
299void fatal(const char *);
300void fatalx(const char *);
301
302/* agent.c */
303void agent_shutdown();
304void agent_init(struct lldpd *, int);
305
306/* strlcpy.c */
307size_t strlcpy(char *, const char *, size_t);
308
309/* iov.c */
310void iov_dump(struct lldpd_frame **, struct iovec *, int);
311u_int16_t iov_checksum(struct iovec *, int, int);
312
a552a72e
VB
313/* client.c */
314struct client_handle {
315 enum hmsg_type type;
316 void (*handle)(struct lldpd*, struct hmsg*, struct hmsg*);
317};
318
319void client_handle_client(struct lldpd *, struct lldpd_client *,
320 char *, int);
321void client_handle_none(struct lldpd *, struct hmsg *,
322 struct hmsg *);
323void client_handle_get_interfaces(struct lldpd *, struct hmsg *,
324 struct hmsg *);
325void client_handle_get_port_related(struct lldpd *, struct hmsg *,
326 struct hmsg *);
327void client_handle_shutdown(struct lldpd *, struct hmsg *,
328 struct hmsg *);
329
b5562b23 330/* priv.c */
a7502371
VB
331void priv_init();
332void priv_fork();
333int priv_ctl_create();
334void priv_ctl_cleanup();
335char *priv_gethostbyname();
336int priv_open(char*);
337int priv_ethtool(char*, struct ethtool_cmd*);
338int priv_iface_init(struct lldpd_hardware *, int);
339int priv_iface_multicast(char *, u_int8_t *, int);
4afe659e
VB
340
341/* privsep_fdpass.c */
342int receive_fd(int);
343void send_fd(int, int);
b5562b23 344
43c02e7b 345#endif /* _LLDPD_H */