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