]>
Commit | Line | Data |
---|---|---|
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" | |
4bad1937 | 40 | #if defined (ENABLE_CDP) || defined (ENABLE_FDP) |
43c02e7b | 41 | #include "cdp.h" |
4bad1937 VB |
42 | #endif |
43 | #ifdef ENABLE_SONMP | |
43c02e7b | 44 | #include "sonmp.h" |
4bad1937 VB |
45 | #endif |
46 | #ifdef ENABLE_EDP | |
43c02e7b | 47 | #include "edp.h" |
4bad1937 | 48 | #endif |
43c02e7b | 49 | |
89840df0 VB |
50 | #define SYSFS_CLASS_NET "/sys/class/net/" |
51 | #define SYSFS_CLASS_DMI "/sys/class/dmi/id/" | |
43c02e7b VB |
52 | #define LLDPD_TTL 120 |
53 | #define LLDPD_TX_DELAY 30 | |
54 | #define LLDPD_TX_MSGDELAY 1 | |
55 | #define LLDPD_CTL_SOCKET "/var/run/lldpd.socket" | |
56 | #define LLDPD_PID_FILE "/var/run/lldpd.pid" | |
57 | ||
58 | #define UNIX_PATH_MAX 108 | |
59 | ||
60 | #define USING_AGENTX_SUBAGENT_MODULE 1 | |
61 | ||
a1347cd8 | 62 | #ifdef ENABLE_DOT1 |
43c02e7b VB |
63 | struct lldpd_vlan { |
64 | TAILQ_ENTRY(lldpd_vlan) v_entries; | |
65 | char *v_name; | |
66 | u_int16_t v_vid; | |
67 | }; | |
0c8f36f3 | 68 | #define STRUCT_LLDPD_VLAN "(Lsw)" |
a1347cd8 | 69 | #endif |
43c02e7b | 70 | |
e3a44efb | 71 | #ifdef ENABLE_LLDPMED |
0c8f36f3 | 72 | #define STRUCT_LLDPD_MED_POLICY "(bbbwbb)" |
e3a44efb VB |
73 | struct lldpd_med_policy { |
74 | u_int8_t type; | |
75 | u_int8_t unknown; | |
76 | u_int8_t tagged; | |
77 | u_int16_t vid; | |
78 | u_int8_t priority; | |
79 | u_int8_t dscp; | |
e3a44efb VB |
80 | }; |
81 | ||
0c8f36f3 | 82 | #define STRUCT_LLDPD_MED_LOC "(bC)" |
e3a44efb VB |
83 | struct lldpd_med_loc { |
84 | u_int8_t format; | |
85 | char *data; | |
86 | int data_len; | |
e3a44efb VB |
87 | }; |
88 | #endif | |
89 | ||
43c02e7b VB |
90 | struct lldpd_chassis { |
91 | u_int8_t c_id_subtype; | |
92 | char *c_id; | |
93 | int c_id_len; | |
94 | char *c_name; | |
95 | char *c_descr; | |
96 | ||
97 | u_int16_t c_cap_available; | |
98 | u_int16_t c_cap_enabled; | |
99 | ||
100 | u_int16_t c_ttl; | |
101 | ||
102 | struct in_addr c_mgmt; | |
103 | u_int32_t c_mgmt_if; | |
89840df0 VB |
104 | |
105 | #ifdef ENABLE_LLDPMED | |
e3a44efb | 106 | #define STRUCT_LLDPD_CHASSIS_MED \ |
e3a44efb VB |
107 | STRUCT_LLDPD_MED_POLICY \ |
108 | STRUCT_LLDPD_MED_POLICY \ | |
109 | STRUCT_LLDPD_MED_POLICY \ | |
110 | STRUCT_LLDPD_MED_POLICY \ | |
111 | STRUCT_LLDPD_MED_POLICY \ | |
112 | STRUCT_LLDPD_MED_POLICY \ | |
113 | STRUCT_LLDPD_MED_POLICY \ | |
114 | STRUCT_LLDPD_MED_POLICY \ | |
115 | STRUCT_LLDPD_MED_LOC \ | |
116 | STRUCT_LLDPD_MED_LOC \ | |
117 | STRUCT_LLDPD_MED_LOC \ | |
0c8f36f3 | 118 | "wwbbbbwsssssss" |
e3a44efb | 119 | |
e3a44efb VB |
120 | struct lldpd_med_policy c_med_policy[LLDPMED_APPTYPE_LAST]; |
121 | struct lldpd_med_loc c_med_location[LLDPMED_LOCFORMAT_LAST]; | |
40ecae87 VB |
122 | u_int16_t c_med_cap_available; |
123 | u_int16_t c_med_cap_enabled; | |
89840df0 | 124 | u_int8_t c_med_type; |
994812b9 VB |
125 | u_int8_t c_med_pow_devicetype; /* PD or PSE */ |
126 | u_int8_t c_med_pow_source; | |
127 | u_int8_t c_med_pow_priority; | |
128 | u_int16_t c_med_pow_val; | |
89840df0 VB |
129 | char *c_med_hw; |
130 | char *c_med_fw; | |
131 | char *c_med_sw; | |
132 | char *c_med_sn; | |
133 | char *c_med_manuf; | |
134 | char *c_med_model; | |
135 | char *c_med_asset; | |
89840df0 | 136 | #else |
a1347cd8 | 137 | #define STRUCT_LLDPD_CHASSIS_MED "" |
89840df0 | 138 | #endif |
1531f7f3 VB |
139 | |
140 | }; | |
0c8f36f3 | 141 | #define STRUCT_LLDPD_CHASSIS "(bCsswwwll" STRUCT_LLDPD_CHASSIS_MED ")" |
43c02e7b VB |
142 | |
143 | struct lldpd_port { | |
144 | u_int8_t p_id_subtype; | |
145 | char *p_id; | |
146 | int p_id_len; | |
147 | char *p_descr; | |
548109b2 | 148 | u_int16_t p_mfs; |
43c02e7b | 149 | |
a1347cd8 | 150 | #ifdef ENABLE_DOT3 |
1531f7f3 | 151 | #define STRUCT_LLDPD_PORT_DOT3 "lbbww" |
43c02e7b VB |
152 | /* Dot3 stuff */ |
153 | u_int32_t p_aggregid; | |
154 | u_int8_t p_autoneg_support; | |
155 | u_int8_t p_autoneg_enabled; | |
156 | u_int16_t p_autoneg_advertised; | |
157 | u_int16_t p_mau_type; | |
a1347cd8 VB |
158 | #else |
159 | #define STRUCT_LLDPD_PORT_DOT3 "" | |
160 | #endif | |
1531f7f3 | 161 | |
a1347cd8 | 162 | #ifdef ENABLE_DOT1 |
75b3469d VB |
163 | #define STRUCT_LLDPD_PORT_DOT1 "wPP" |
164 | u_int16_t p_pvid; | |
1531f7f3 | 165 | TAILQ_HEAD(, lldpd_vlan) p_vlans; |
a1347cd8 VB |
166 | #else |
167 | #define STRUCT_LLDPD_PORT_DOT1 "" | |
168 | #endif | |
1531f7f3 VB |
169 | }; |
170 | ||
548109b2 | 171 | #define STRUCT_LLDPD_PORT "(bCsw" STRUCT_LLDPD_PORT_DOT3 STRUCT_LLDPD_PORT_DOT1 ")" |
43c02e7b VB |
172 | |
173 | struct lldpd_frame { | |
174 | int size; | |
175 | unsigned char frame[]; | |
176 | }; | |
177 | ||
178 | struct lldpd_hardware { | |
179 | TAILQ_ENTRY(lldpd_hardware) h_entries; | |
180 | ||
181 | #define INTERFACE_OPENED(x) ((x)->h_raw != -1) | |
182 | ||
183 | int h_raw; | |
184 | int h_raw_real; /* For bonding */ | |
185 | int h_master; /* For bonding */ | |
186 | ||
187 | #define LLDPD_MODE_ANY 0 | |
188 | #define LLDPD_MODE_LLDP 1 | |
189 | #define LLDPD_MODE_CDPV1 2 | |
190 | #define LLDPD_MODE_CDPV2 3 | |
191 | #define LLDPD_MODE_SONMP 4 | |
192 | #define LLDPD_MODE_EDP 5 | |
031118c4 | 193 | #define LLDPD_MODE_FDP 6 |
43c02e7b VB |
194 | int h_mode; |
195 | ||
196 | int h_flags; | |
197 | int h_mtu; | |
198 | char h_ifname[IFNAMSIZ]; | |
199 | u_int8_t h_lladdr[ETHER_ADDR_LEN]; | |
200 | ||
201 | u_int64_t h_tx_cnt; | |
202 | u_int64_t h_rx_cnt; | |
203 | u_int64_t h_rx_discarded_cnt; | |
204 | u_int64_t h_rx_ageout_cnt; | |
37387046 | 205 | u_int64_t h_rx_unrecognized_cnt; |
43c02e7b VB |
206 | |
207 | u_int8_t *h_proto_macs; | |
208 | time_t h_start_probe; | |
209 | ||
210 | struct lldpd_port h_lport; | |
211 | time_t h_llastchange; | |
212 | struct lldpd_frame *h_llastframe; | |
213 | ||
214 | time_t h_rlastchange; | |
215 | time_t h_rlastupdate; | |
216 | int h_rid; | |
217 | struct lldpd_frame *h_rlastframe; | |
218 | struct lldpd_port *h_rport; | |
219 | struct lldpd_chassis *h_rchassis; | |
220 | }; | |
221 | ||
50a89ca7 VB |
222 | /* lldpd_vif can be casted to lldpd_hardware on some cases */ |
223 | struct lldpd_vif { | |
224 | TAILQ_ENTRY(lldpd_vif) vif_entries; | |
225 | int vif_raw; | |
226 | int vif_raw_real; /* Not used */ | |
227 | int vif_master; /* Not used */ | |
228 | int vif_mode; /* Not used */ | |
229 | int vif_flags; | |
230 | int vif_mtu; | |
231 | char vif_ifname[IFNAMSIZ]; | |
232 | ||
233 | /* No more compatibility with struct lldpd_hardware from here */ | |
234 | struct lldpd_hardware *vif_real; | |
235 | }; | |
236 | ||
43c02e7b VB |
237 | struct lldpd_interface { |
238 | TAILQ_ENTRY(lldpd_interface) next; | |
239 | char *name; | |
240 | }; | |
0c8f36f3 | 241 | #define STRUCT_LLDPD_INTERFACE "(Ls)" |
43c02e7b VB |
242 | |
243 | struct lldpd_client { | |
244 | TAILQ_ENTRY(lldpd_client) next; | |
245 | int fd; | |
246 | }; | |
247 | ||
248 | #define PROTO_SEND_SIG struct lldpd *, struct lldpd_chassis *, struct lldpd_hardware * | |
249 | #define PROTO_DECODE_SIG struct lldpd *, char *, int, struct lldpd_hardware *, struct lldpd_chassis **, struct lldpd_port ** | |
250 | #define PROTO_GUESS_SIG char *, int | |
251 | ||
252 | struct lldpd; | |
253 | struct protocol { | |
254 | int mode; /* > 0 mode identifier (unique per protocol) */ | |
255 | int enabled; /* Is this protocol enabled? */ | |
256 | char *name; /* Name of protocol */ | |
257 | char arg; /* Argument to enable this protocol */ | |
258 | int(*send)(PROTO_SEND_SIG); /* How to send a frame */ | |
259 | int(*decode)(PROTO_DECODE_SIG); /* How to decode a frame */ | |
260 | int(*guess)(PROTO_GUESS_SIG); /* Can be NULL, use MAC address in this case */ | |
261 | u_int8_t mac[ETH_ALEN]; /* Destination MAC address used by this protocol */ | |
262 | struct sock_filter *filter; /* BPF filter */ | |
263 | size_t filterlen; /* Size of BPF filter */ | |
264 | }; | |
265 | ||
266 | struct lldpd { | |
267 | int g_sock; | |
268 | int g_delay; | |
269 | ||
270 | struct protocol *g_protocols; | |
271 | int g_multi; /* Set to 1 if multiple protocols */ | |
272 | int g_probe_time; | |
766f32b3 | 273 | int g_listen_vlans; |
43c02e7b VB |
274 | |
275 | time_t g_lastsent; | |
276 | int g_lastrid; | |
277 | #ifdef USE_SNMP | |
278 | int g_snmp; | |
279 | #endif /* USE_SNMP */ | |
280 | ||
281 | /* Unix socket handling */ | |
282 | int g_ctl; | |
283 | TAILQ_HEAD(, lldpd_client) g_clients; | |
284 | ||
285 | char *g_mgmt_pattern; | |
286 | ||
287 | struct lldpd_chassis g_lchassis; | |
288 | ||
289 | TAILQ_HEAD(, lldpd_hardware) g_hardware; | |
50a89ca7 | 290 | TAILQ_HEAD(, lldpd_vif) g_vif; |
43c02e7b VB |
291 | }; |
292 | ||
293 | enum hmsg_type { | |
294 | HMSG_NONE, | |
295 | HMSG_GET_INTERFACES, | |
296 | HMSG_GET_CHASSIS, | |
297 | HMSG_GET_PORT, | |
298 | HMSG_GET_VLANS, | |
299 | HMSG_SHUTDOWN | |
300 | }; | |
301 | ||
302 | struct hmsg_hdr { | |
303 | enum hmsg_type type; | |
304 | int16_t len; | |
305 | pid_t pid; | |
306 | } __attribute__ ((__packed__)); | |
307 | ||
308 | struct hmsg { | |
309 | struct hmsg_hdr hdr; | |
310 | void *data; | |
311 | } __attribute__ ((__packed__)); | |
312 | ||
313 | #define HMSG_HEADER_SIZE sizeof(struct hmsg_hdr) | |
314 | #define MAX_HMSGSIZE 8192 | |
315 | ||
316 | /* lldpd.c */ | |
317 | void lldpd_cleanup(struct lldpd *); | |
d9be8ea0 | 318 | void lldpd_hardware_cleanup(struct lldpd_hardware *); |
a1347cd8 | 319 | #ifdef ENABLE_DOT1 |
43c02e7b | 320 | void lldpd_vlan_cleanup(struct lldpd_port *); |
a1347cd8 | 321 | #endif |
43c02e7b VB |
322 | void lldpd_remote_cleanup(struct lldpd *, struct lldpd_hardware *, int); |
323 | void lldpd_port_cleanup(struct lldpd_port *); | |
324 | void lldpd_chassis_cleanup(struct lldpd_chassis *); | |
325 | ||
326 | /* lldp.c */ | |
327 | int lldp_send(PROTO_SEND_SIG); | |
328 | int lldp_decode(PROTO_DECODE_SIG); | |
329 | ||
330 | /* cdp.c */ | |
4bad1937 | 331 | #ifdef ENABLE_CDP |
43c02e7b VB |
332 | int cdpv1_send(PROTO_SEND_SIG); |
333 | int cdpv2_send(PROTO_SEND_SIG); | |
43c02e7b VB |
334 | int cdpv1_guess(PROTO_GUESS_SIG); |
335 | int cdpv2_guess(PROTO_GUESS_SIG); | |
4bad1937 VB |
336 | #endif |
337 | #if defined (ENABLE_CDP) || defined (ENABLE_FDP) | |
338 | int cdp_decode(PROTO_DECODE_SIG); | |
339 | #endif | |
340 | #ifdef ENABLE_FDP | |
341 | int fdp_send(PROTO_SEND_SIG); | |
342 | #endif | |
43c02e7b | 343 | |
4bad1937 | 344 | #ifdef ENABLE_SONMP |
43c02e7b VB |
345 | /* sonmp.c */ |
346 | int sonmp_send(PROTO_SEND_SIG); | |
347 | int sonmp_decode(PROTO_DECODE_SIG); | |
4bad1937 | 348 | #endif |
43c02e7b | 349 | |
4bad1937 | 350 | #ifdef ENABLE_EDP |
43c02e7b VB |
351 | /* edp.c */ |
352 | int edp_send(PROTO_SEND_SIG); | |
353 | int edp_decode(PROTO_DECODE_SIG); | |
4bad1937 | 354 | #endif |
43c02e7b VB |
355 | |
356 | /* ctl.c */ | |
b5562b23 | 357 | int ctl_create(char *); |
43c02e7b | 358 | int ctl_connect(char *); |
b5562b23 | 359 | void ctl_cleanup(char *); |
43c02e7b VB |
360 | int ctl_accept(struct lldpd *, int); |
361 | int ctl_close(struct lldpd *, int); | |
362 | void ctl_msg_init(struct hmsg *, enum hmsg_type); | |
363 | int ctl_msg_send(int, struct hmsg *); | |
364 | int ctl_msg_recv(int, struct hmsg *); | |
365 | int ctl_msg_pack_list(char *, void *, unsigned int, struct hmsg *, void **); | |
366 | int ctl_msg_unpack_list(char *, void *, unsigned int, struct hmsg *, void **); | |
367 | int ctl_msg_pack_structure(char *, void *, unsigned int, struct hmsg *, void **); | |
368 | int ctl_msg_unpack_structure(char *, void *, unsigned int, struct hmsg *, void **); | |
369 | ||
370 | /* features.c */ | |
371 | int iface_is_bridge(struct lldpd *, const char *); | |
43c02e7b VB |
372 | int iface_is_wireless(struct lldpd *, const char *); |
373 | int iface_is_vlan(struct lldpd *, const char *); | |
374 | int iface_is_bond(struct lldpd *, const char *); | |
375 | int iface_is_bond_slave(struct lldpd *, | |
beeaefa3 | 376 | const char *, const char *, int *); |
43c02e7b | 377 | int iface_is_enslaved(struct lldpd *, const char *); |
beeaefa3 | 378 | int iface_is_slave_active(struct lldpd *, int, const char *); |
1d291522 | 379 | void iface_get_permanent_mac(struct lldpd *, struct lldpd_hardware *); |
89840df0 VB |
380 | #ifdef ENABLE_LLDPMED |
381 | char *dmi_hw(); | |
382 | char *dmi_fw(); | |
383 | char *dmi_sn(); | |
384 | char *dmi_manuf(); | |
385 | char *dmi_model(); | |
386 | char *dmi_asset(); | |
387 | #endif | |
43c02e7b VB |
388 | |
389 | /* log.c */ | |
390 | void log_init(int); | |
391 | void log_warn(const char *, ...); | |
392 | #define LLOG_WARN(x,...) log_warn("%s: " x, __FUNCTION__, ##__VA_ARGS__) | |
393 | void log_warnx(const char *, ...); | |
394 | #define LLOG_WARNX(x,...) log_warnx("%s: " x, __FUNCTION__, ##__VA_ARGS__) | |
395 | void log_info(const char *, ...); | |
396 | #define LLOG_INFO(x,...) log_info("%s: " x, __FUNCTION__, ##__VA_ARGS__) | |
397 | void log_debug(const char *, ...); | |
398 | #define LLOG_DEBUG(x,...) log_debug("%s: " x, __FUNCTION__, ##__VA_ARGS__) | |
399 | void fatal(const char *); | |
400 | void fatalx(const char *); | |
401 | ||
402 | /* agent.c */ | |
403 | void agent_shutdown(); | |
404 | void agent_init(struct lldpd *, int); | |
405 | ||
d72a05d4 VB |
406 | /* agent_priv.c */ |
407 | void agent_priv_register_domain(); | |
408 | ||
43c02e7b VB |
409 | /* strlcpy.c */ |
410 | size_t strlcpy(char *, const char *, size_t); | |
411 | ||
412 | /* iov.c */ | |
413 | void iov_dump(struct lldpd_frame **, struct iovec *, int); | |
414 | u_int16_t iov_checksum(struct iovec *, int, int); | |
415 | ||
a552a72e VB |
416 | /* client.c */ |
417 | struct client_handle { | |
418 | enum hmsg_type type; | |
419 | void (*handle)(struct lldpd*, struct hmsg*, struct hmsg*); | |
420 | }; | |
421 | ||
422 | void client_handle_client(struct lldpd *, struct lldpd_client *, | |
423 | char *, int); | |
424 | void client_handle_none(struct lldpd *, struct hmsg *, | |
425 | struct hmsg *); | |
426 | void client_handle_get_interfaces(struct lldpd *, struct hmsg *, | |
427 | struct hmsg *); | |
428 | void client_handle_get_port_related(struct lldpd *, struct hmsg *, | |
429 | struct hmsg *); | |
430 | void client_handle_shutdown(struct lldpd *, struct hmsg *, | |
431 | struct hmsg *); | |
432 | ||
b5562b23 | 433 | /* priv.c */ |
a2993d83 | 434 | void priv_init(char*); |
a7502371 VB |
435 | int priv_ctl_create(); |
436 | void priv_ctl_cleanup(); | |
437 | char *priv_gethostbyname(); | |
438 | int priv_open(char*); | |
439 | int priv_ethtool(char*, struct ethtool_cmd*); | |
440 | int priv_iface_init(struct lldpd_hardware *, int); | |
441 | int priv_iface_multicast(char *, u_int8_t *, int); | |
d72a05d4 | 442 | int priv_snmp_socket(struct sockaddr_un *); |
4afe659e VB |
443 | |
444 | /* privsep_fdpass.c */ | |
445 | int receive_fd(int); | |
446 | void send_fd(int, int); | |
b5562b23 | 447 | |
43c02e7b | 448 | #endif /* _LLDPD_H */ |