1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2015 Nicira, Inc.
4 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <arpa/inet.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
31 #include <sys/types.h>
37 #include <netinet/if_ether.h>
39 #include <sys/select.h>
40 #include <sys/utsname.h>
47 VLOG_DEFINE_THIS_MODULE(lldpd);
49 static struct protocol protos[] =
51 { LLDPD_MODE_LLDP, 1, "LLDP", 'l', lldp_send, lldp_decode, NULL,
52 LLDP_MULTICAST_ADDR },
53 { 0, 0, "any", ' ', NULL, NULL, NULL,
57 void lldpd_assign_cfg_to_protocols(struct lldpd *cfg)
59 cfg->g_protocols = protos;
62 struct lldpd_hardware *
63 lldpd_get_hardware(struct lldpd *cfg, char *name, int index,
64 struct lldpd_ops *ops)
66 struct lldpd_hardware *hw;
68 LIST_FOR_EACH (hw, h_entries, &cfg->g_hardware) {
69 if (!strcmp(hw->h_ifname, name) && hw->h_ifindex == index
70 && (!ops || ops == hw->h_ops)) {
78 struct lldpd_hardware *
79 lldpd_alloc_hardware(struct lldpd *cfg, char *name, int index)
81 struct lldpd_hardware *hw;
83 VLOG_DBG("allocate a new local hardware interface (%s)", name);
85 hw = xzalloc(sizeof *hw);
87 ovs_strlcpy(hw->h_ifname, name, sizeof hw->h_ifname);
88 hw->h_ifindex = index;
89 hw->h_lport.p_chassis = CONTAINER_OF(list_front(&cfg->g_chassis),
90 struct lldpd_chassis, list);
91 hw->h_lport.p_chassis->c_refcount++;
92 list_init(&hw->h_rports);
98 lldpd_alloc_mgmt(int family, void *addrptr, size_t addrsize, u_int32_t iface)
100 struct lldpd_mgmt *mgmt;
102 VLOG_DBG("allocate a new management address (family: %d)", family);
104 if (family <= LLDPD_AF_UNSPEC || family >= LLDPD_AF_LAST) {
105 errno = EAFNOSUPPORT;
108 if (addrsize > LLDPD_MGMT_MAXADDRSIZE) {
112 mgmt = xzalloc(sizeof *mgmt);
113 mgmt->m_family = family;
114 memcpy(&mgmt->m_addr, addrptr, addrsize);
115 mgmt->m_addrsize = addrsize;
116 mgmt->m_iface = iface;
122 lldpd_hardware_cleanup(struct lldpd *cfg, struct lldpd_hardware *hardware)
124 VLOG_DBG("cleanup hardware port %s", hardware->h_ifname);
126 lldpd_port_cleanup(&hardware->h_lport, true);
127 if (hardware->h_ops && hardware->h_ops->cleanup) {
128 hardware->h_ops->cleanup(cfg, hardware);
134 lldpd_cleanup(struct lldpd *cfg)
136 struct lldpd_hardware *hw, *hw_next;
137 struct lldpd_chassis *chassis, *chassis_next;
139 VLOG_DBG("cleanup all ports");
141 LIST_FOR_EACH_SAFE (hw, hw_next, h_entries, &cfg->g_hardware) {
143 list_remove(&hw->h_entries);
144 lldpd_remote_cleanup(hw, NULL, true);
145 lldpd_hardware_cleanup(cfg, hw);
147 lldpd_remote_cleanup(hw, NULL, false);
151 VLOG_DBG("cleanup all chassis");
153 LIST_FOR_EACH_SAFE (chassis, chassis_next, list, &cfg->g_chassis) {
154 if (chassis->c_refcount == 0) {
155 list_remove(&chassis->list);
156 lldpd_chassis_cleanup(chassis, 1);
161 /* Update chassis `ochassis' with values from `chassis'. The later one is not
162 * expected to be part of a list! It will also be wiped from memory.
165 lldpd_move_chassis(struct lldpd_chassis *ochassis,
166 struct lldpd_chassis *chassis)
168 struct lldpd_mgmt *mgmt, *mgmt_next;
169 int refcount = ochassis->c_refcount;
170 int index = ochassis->c_index;
171 struct ovs_list listcopy;
173 /* We want to keep refcount, index and list stuff from the current chassis
175 memcpy(&listcopy, &ochassis->list, sizeof listcopy);
176 lldpd_chassis_cleanup(ochassis, 0);
179 /* WARNING: this is a kludgy hack, we need in-place copy and cannot use
182 memcpy(ochassis, chassis, sizeof *ochassis);
183 list_init(&ochassis->c_mgmt);
185 /* Copy of management addresses */
186 LIST_FOR_EACH_SAFE (mgmt, mgmt_next, m_entries, &chassis->c_mgmt) {
187 list_remove(&mgmt->m_entries);
188 list_insert(&ochassis->c_mgmt, &mgmt->m_entries);
191 /* Restore saved values */
192 ochassis->c_refcount = refcount;
193 ochassis->c_index = index;
194 memcpy(&ochassis->list, &listcopy, sizeof ochassis->list);
196 /* Get rid of the new chassis */
201 lldpd_guess_type(struct lldpd *cfg, char *frame, int s)
205 if (s < ETH_ADDR_LEN) {
209 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
210 if (!cfg->g_protocols[i].enabled) {
213 if (cfg->g_protocols[i].guess == NULL) {
214 if (memcmp(frame, cfg->g_protocols[i].mac, ETH_ADDR_LEN) == 0) {
215 VLOG_DBG("guessed protocol is %s (from MAC address)",
216 cfg->g_protocols[i].name);
217 return cfg->g_protocols[i].mode;
220 if (cfg->g_protocols[i].guess(frame, s)) {
221 VLOG_DBG("guessed protocol is %s (from detector function)",
222 cfg->g_protocols[i].name);
223 return cfg->g_protocols[i].mode;
232 lldpd_decode(struct lldpd *cfg, char *frame, int s,
233 struct lldpd_hardware *hw)
236 struct lldpd_chassis *chassis, *ochassis = NULL;
237 struct lldpd_port *port, *oport;
238 int guess = LLDPD_MODE_LLDP;
239 struct eth_header eheader;
243 VLOG_DBG("decode a received frame on %s size %d", hw->h_ifname,s);
245 if (s < sizeof(struct eth_header) + 4) {
246 /* Too short, just discard it */
250 /* Decapsulate VLAN frames */
251 memcpy(&eheader, frame, sizeof eheader);
252 if (eheader.eth_type == htons(ETH_TYPE_VLAN)) {
253 /* VLAN decapsulation means to shift 4 bytes left the frame from
254 * offset 2 * ETH_ADDR_LEN
256 memmove(frame + 2 * ETH_ADDR_LEN, frame + 2 * ETH_ADDR_LEN + 4,
257 s - 2 * ETH_ADDR_LEN);
261 LIST_FOR_EACH (oport, p_entries, &hw->h_rports) {
262 if (oport->p_lastframe &&
263 oport->p_lastframe->size == s &&
264 !memcmp(oport->p_lastframe->frame, frame, s)) {
265 /* Already received the same frame */
266 VLOG_DBG("duplicate frame, no need to decode");
267 oport->p_lastupdate = time_now();
272 guess = lldpd_guess_type(cfg, frame, s);
273 VLOG_DBG("guessed %d enabled:%d", guess, cfg->g_protocols[0].enabled);
275 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
276 if (!cfg->g_protocols[i].enabled) {
279 if (cfg->g_protocols[i].mode == guess) {
280 VLOG_DBG("using decode function for %s protocol",
281 cfg->g_protocols[i].name);
282 if (cfg->g_protocols[i].decode(cfg, frame, s, hw, &chassis, &port)
284 VLOG_DBG("function for %s protocol did not "
286 cfg->g_protocols[i].name);
289 chassis->c_protocol = port->p_protocol = cfg->g_protocols[i].mode;
292 VLOG_DBG(" %"PRIuSIZE "mode:%d enabled:%d",
293 i, cfg->g_protocols[i].mode, cfg->g_protocols[i].enabled);
295 if (cfg->g_protocols[i].mode == 0) {
296 VLOG_DBG("unable to guess frame type on %s", hw->h_ifname);
300 /* Do we already have the same MSAP somewhere? */
301 VLOG_DBG("search for the same MSAP");
303 LIST_FOR_EACH (oport, p_entries, &hw->h_rports) {
304 if (port->p_protocol == oport->p_protocol) {
306 if (port->p_id_subtype == oport->p_id_subtype &&
307 port->p_id_len == oport->p_id_len &&
308 !memcmp(port->p_id, oport->p_id, port->p_id_len) &&
309 chassis->c_id_subtype == oport->p_chassis->c_id_subtype &&
310 chassis->c_id_len == oport->p_chassis->c_id_len &&
311 !memcmp(chassis->c_id, oport->p_chassis->c_id,
312 chassis->c_id_len)) {
313 ochassis = oport->p_chassis;
314 VLOG_DBG("MSAP is already known");
325 /* Do we have room for a new MSAP? */
326 if (!oport && cfg->g_config.c_max_neighbors) {
327 if (count == (cfg->g_config.c_max_neighbors - 1)) {
328 VLOG_DBG("max neighbors %d reached for port %s, "
329 "dropping any new ones silently",
330 cfg->g_config.c_max_neighbors,
332 } else if (count > cfg->g_config.c_max_neighbors - 1) {
333 VLOG_DBG("too many neighbors for port %s, drop this new one",
335 lldpd_port_cleanup(port, true);
336 lldpd_chassis_cleanup(chassis, true);
342 /* No, but do we already know the system? */
345 VLOG_DBG("MSAP is unknown, search for the chassis");
347 LIST_FOR_EACH (ochassis, list, &cfg->g_chassis) {
348 if ((chassis->c_protocol == ochassis->c_protocol) &&
349 (chassis->c_id_subtype == ochassis->c_id_subtype) &&
350 (chassis->c_id_len == ochassis->c_id_len) &&
351 (memcmp(chassis->c_id, ochassis->c_id,
352 chassis->c_id_len) == 0)) {
364 /* The port is known, remove it before adding it back */
365 list_remove(&oport->p_entries);
366 lldpd_port_cleanup(oport, 1);
371 lldpd_move_chassis(ochassis, chassis);
374 /* Chassis not known, add it */
375 VLOG_DBG("unknown chassis, add it to the list");
376 chassis->c_index = ++cfg->g_lastrid;
377 chassis->c_refcount = 0;
378 list_push_back(&cfg->g_chassis, &chassis->list);
379 listsize = list_size(&cfg->g_chassis);
380 VLOG_DBG("%"PRIuSIZE " different systems are known", listsize);
384 port->p_lastchange = port->p_lastupdate = time_now();
385 port->p_lastframe = xmalloc(s + sizeof(struct lldpd_frame));
386 port->p_lastframe->size = s;
387 memcpy(port->p_lastframe->frame, frame, s);
388 list_insert(&hw->h_rports, &port->p_entries);
390 port->p_chassis = chassis;
391 port->p_chassis->c_refcount++;
392 /* Several cases are possible :
393 * 1. chassis is new, its refcount was 0. It is now attached
394 * to this port, its refcount is 1.
395 * 2. chassis already exists and was attached to another
396 * port, we increase its refcount accordingly.
397 * 3. chassis already exists and was attached to the same
398 * port, its refcount was decreased with
399 * lldpd_port_cleanup() and is now increased again.
401 * In all cases, if the port already existed, it has been
402 * freed with lldpd_port_cleanup() and therefore, the refcount
403 * of the chassis that was attached to it is decreased.
405 i = list_size(&hw->h_rports);
406 VLOG_DBG("%"PRIuSIZE " neighbors for %s", i, hw->h_ifname);
416 lldpd_hide_ports(struct lldpd *cfg,
417 struct lldpd_hardware *hw,
419 struct lldpd_port *port;
420 int protocols[LLDPD_MODE_MAX + 1];
426 VLOG_DBG("apply smart filter for port %s", hw->h_ifname);
428 /* Compute the number of occurrences of each protocol */
429 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
433 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
434 protocols[port->p_protocol]++;
437 /* Turn the protocols[] array into an array of
438 * enabled/disabled protocols. 1 means enabled, 0
441 min = (unsigned int) - 1;
442 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
443 if (protocols[i] && (protocols[i] < min)) {
447 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
448 if (protocols[i] == min && !found) {
449 /* If we need a tie breaker, we take the first protocol only */
450 if (cfg->g_config.c_smart & mask &
451 (SMART_OUTGOING_ONE_PROTO | SMART_INCOMING_ONE_PROTO)) {
460 /* We set the p_hidden flag to 1 if the protocol is disabled */
461 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
462 if (mask == SMART_OUTGOING) {
463 port->p_hidden_out = protocols[port->p_protocol] ? false : true;
465 port->p_hidden_in = protocols[port->p_protocol] ? false : true;
469 /* If we want only one neighbor, we take the first one */
470 if (cfg->g_config.c_smart & mask &
471 (SMART_OUTGOING_ONE_NEIGH | SMART_INCOMING_ONE_NEIGH)) {
474 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
475 if (mask == SMART_OUTGOING) {
477 port->p_hidden_out = true;
479 if (!port->p_hidden_out) {
483 if (mask == SMART_INCOMING) {
485 port->p_hidden_in = true;
487 if (!port->p_hidden_in) {
494 /* Print a debug message summarizing the operation */
495 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
500 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
501 if (!((mask == SMART_OUTGOING && port->p_hidden_out) ||
502 (mask == SMART_INCOMING && port->p_hidden_in))) {
504 protocols[port->p_protocol] = 1;
510 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
511 if (cfg->g_protocols[i].enabled &&
512 protocols[cfg->g_protocols[i].mode]) {
514 strlen(cfg->g_protocols[i].name) + 3 > sizeof(buffer)) {
515 /* Unlikely, our buffer is too small */
516 memcpy(buffer + sizeof(buffer) - 4, "...", 4);
520 strncat(buffer, ", ", 2);
521 strncat(buffer, cfg->g_protocols[i].name,
522 strlen(cfg->g_protocols[i].name));
526 VLOG_DBG("%s: %s: %d visible neighbors (out of %d)",
528 (mask == SMART_OUTGOING) ? "out filter" : "in filter",
530 VLOG_DBG("%s: protocols: %s",
531 hw->h_ifname, buffer[0] ? buffer : "(none)");
534 /* Hide unwanted ports depending on smart mode set by the user */
536 lldpd_hide_all(struct lldpd *cfg)
538 struct lldpd_hardware *hw;
540 if (!cfg->g_config.c_smart) {
544 VLOG_DBG("apply smart filter results on all ports");
546 LIST_FOR_EACH (hw, h_entries, &cfg->g_hardware) {
547 if (cfg->g_config.c_smart & SMART_INCOMING_FILTER) {
548 lldpd_hide_ports(cfg, hw, SMART_INCOMING);
550 if (cfg->g_config.c_smart & SMART_OUTGOING_FILTER) {
551 lldpd_hide_ports(cfg, hw, SMART_OUTGOING);
557 lldpd_recv(struct lldpd *cfg,
558 struct lldpd_hardware *hw,
564 VLOG_DBG("receive a frame on %s", hw->h_ifname);
565 if (cfg->g_config.c_paused) {
566 VLOG_DBG("paused, ignore the frame on %s", hw->h_ifname);
570 VLOG_DBG("decode received frame on %s h_rx_cnt=%" PRIu64,
571 hw->h_ifname, hw->h_rx_cnt);
572 lldpd_decode(cfg, buffer, n, hw);
573 lldpd_hide_all(cfg); /* Immediatly hide */
577 lldpd_send(struct lldpd_hardware *hw, struct dp_packet *p)
579 struct lldpd *cfg = hw->h_cfg;
580 struct lldpd_port *port;
584 if (cfg->g_config.c_receiveonly || cfg->g_config.c_paused) {
588 if ((hw->h_flags & IFF_RUNNING) == 0) {
593 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
594 if (!cfg->g_protocols[i].enabled) {
598 /* We send only if we have at least one remote system
599 * speaking this protocol or if the protocol is forced */
600 if (cfg->g_protocols[i].enabled > 1) {
601 if ((lldp_size = cfg->g_protocols[i].send(cfg, hw, p)) != -E2BIG) {
605 VLOG_DBG("send PDU on %s failed E2BIG", hw->h_ifname);
610 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
611 /* If this remote port is disabled, we don't consider it */
612 if (port->p_hidden_out) {
615 if (port->p_protocol == cfg->g_protocols[i].mode) {
616 VLOG_DBG("send PDU on %s with protocol %s",
617 hw->h_ifname, cfg->g_protocols[i].name);
618 lldp_size = cfg->g_protocols[i].send(cfg, hw, p);
626 /* Nothing was sent for this port, let's speak the first
627 * available protocol.
629 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
630 if (!cfg->g_protocols[i].enabled) {
633 VLOG_DBG("fallback to protocol %s for %s",
634 cfg->g_protocols[i].name, hw->h_ifname);
635 lldp_size = cfg->g_protocols[i].send(cfg, hw, p);
638 if (cfg->g_protocols[i].mode == 0) {
639 VLOG_WARN("no protocol enabled, dunno what to send");