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>
38 #include <sys/select.h>
39 #include <sys/utsname.h>
46 VLOG_DEFINE_THIS_MODULE(lldpd);
48 static struct protocol protos[] =
50 { LLDPD_MODE_LLDP, 1, "LLDP", 'l', lldp_send, lldp_decode, NULL,
51 LLDP_MULTICAST_ADDR },
52 { 0, 0, "any", ' ', NULL, NULL, NULL,
53 { { { 0,0,0,0,0,0 } } } }
56 void lldpd_assign_cfg_to_protocols(struct lldpd *cfg)
58 cfg->g_protocols = protos;
61 struct lldpd_hardware *
62 lldpd_get_hardware(struct lldpd *cfg, char *name, int index,
63 struct lldpd_ops *ops)
65 struct lldpd_hardware *hw;
67 LIST_FOR_EACH (hw, h_entries, &cfg->g_hardware) {
68 if (!strcmp(hw->h_ifname, name) && hw->h_ifindex == index
69 && (!ops || ops == hw->h_ops)) {
77 struct lldpd_hardware *
78 lldpd_alloc_hardware(struct lldpd *cfg, char *name, int index)
80 struct lldpd_hardware *hw;
82 VLOG_DBG("allocate a new local hardware interface (%s)", name);
84 hw = xzalloc(sizeof *hw);
86 ovs_strlcpy(hw->h_ifname, name, sizeof hw->h_ifname);
87 hw->h_ifindex = index;
88 hw->h_lport.p_chassis = CONTAINER_OF(list_front(&cfg->g_chassis),
89 struct lldpd_chassis, list);
90 hw->h_lport.p_chassis->c_refcount++;
91 list_init(&hw->h_rports);
97 lldpd_alloc_mgmt(int family, void *addrptr, size_t addrsize, u_int32_t iface)
99 struct lldpd_mgmt *mgmt;
101 VLOG_DBG("allocate a new management address (family: %d)", family);
103 if (family <= LLDPD_AF_UNSPEC || family >= LLDPD_AF_LAST) {
104 errno = EAFNOSUPPORT;
107 if (addrsize > LLDPD_MGMT_MAXADDRSIZE) {
111 mgmt = xzalloc(sizeof *mgmt);
112 mgmt->m_family = family;
113 memcpy(&mgmt->m_addr, addrptr, addrsize);
114 mgmt->m_addrsize = addrsize;
115 mgmt->m_iface = iface;
121 lldpd_hardware_cleanup(struct lldpd *cfg, struct lldpd_hardware *hardware)
123 VLOG_DBG("cleanup hardware port %s", hardware->h_ifname);
125 lldpd_port_cleanup(&hardware->h_lport, true);
126 if (hardware->h_ops && hardware->h_ops->cleanup) {
127 hardware->h_ops->cleanup(cfg, hardware);
133 lldpd_cleanup(struct lldpd *cfg)
135 struct lldpd_hardware *hw, *hw_next;
136 struct lldpd_chassis *chassis, *chassis_next;
138 VLOG_DBG("cleanup all ports");
140 LIST_FOR_EACH_SAFE (hw, hw_next, h_entries, &cfg->g_hardware) {
142 list_remove(&hw->h_entries);
143 lldpd_remote_cleanup(hw, NULL, true);
144 lldpd_hardware_cleanup(cfg, hw);
146 lldpd_remote_cleanup(hw, NULL, false);
150 VLOG_DBG("cleanup all chassis");
152 LIST_FOR_EACH_SAFE (chassis, chassis_next, list, &cfg->g_chassis) {
153 if (chassis->c_refcount == 0) {
154 list_remove(&chassis->list);
155 lldpd_chassis_cleanup(chassis, 1);
160 /* Update chassis `ochassis' with values from `chassis'. The later one is not
161 * expected to be part of a list! It will also be wiped from memory.
164 lldpd_move_chassis(struct lldpd_chassis *ochassis,
165 struct lldpd_chassis *chassis)
167 struct lldpd_mgmt *mgmt;
168 int refcount = ochassis->c_refcount;
169 int index = ochassis->c_index;
170 struct ovs_list listcopy;
172 /* We want to keep refcount, index and list stuff from the current chassis
174 memcpy(&listcopy, &ochassis->list, sizeof listcopy);
175 lldpd_chassis_cleanup(ochassis, 0);
178 /* WARNING: this is a kludgy hack, we need in-place copy and cannot use
181 memcpy(ochassis, chassis, sizeof *ochassis);
182 list_init(&ochassis->c_mgmt);
184 /* Copy of management addresses */
185 LIST_FOR_EACH_POP (mgmt, m_entries, &chassis->c_mgmt) {
186 list_insert(&ochassis->c_mgmt, &mgmt->m_entries);
189 /* Restore saved values */
190 ochassis->c_refcount = refcount;
191 ochassis->c_index = index;
192 memcpy(&ochassis->list, &listcopy, sizeof ochassis->list);
194 /* Get rid of the new chassis */
199 lldpd_guess_type(struct lldpd *cfg, char *frame, int s)
203 if (s < ETH_ADDR_LEN) {
207 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
208 if (!cfg->g_protocols[i].enabled) {
211 if (cfg->g_protocols[i].guess == NULL) {
212 if (memcmp(frame, &cfg->g_protocols[i].mac, ETH_ADDR_LEN) == 0) {
213 VLOG_DBG("guessed protocol is %s (from MAC address)",
214 cfg->g_protocols[i].name);
215 return cfg->g_protocols[i].mode;
218 if (cfg->g_protocols[i].guess(frame, s)) {
219 VLOG_DBG("guessed protocol is %s (from detector function)",
220 cfg->g_protocols[i].name);
221 return cfg->g_protocols[i].mode;
230 lldpd_decode(struct lldpd *cfg, char *frame, int s,
231 struct lldpd_hardware *hw)
234 struct lldpd_chassis *chassis, *ochassis = NULL;
235 struct lldpd_port *port, *oport;
236 int guess = LLDPD_MODE_LLDP;
237 struct eth_header eheader;
241 VLOG_DBG("decode a received frame on %s size %d", hw->h_ifname,s);
243 if (s < sizeof(struct eth_header) + 4) {
244 /* Too short, just discard it */
248 /* Decapsulate VLAN frames */
249 memcpy(&eheader, frame, sizeof eheader);
250 if (eheader.eth_type == htons(ETH_TYPE_VLAN)) {
251 /* VLAN decapsulation means to shift 4 bytes left the frame from
252 * offset 2 * ETH_ADDR_LEN
254 memmove(frame + 2 * ETH_ADDR_LEN, frame + 2 * ETH_ADDR_LEN + 4,
255 s - 2 * ETH_ADDR_LEN);
259 LIST_FOR_EACH (oport, p_entries, &hw->h_rports) {
260 if (oport->p_lastframe &&
261 oport->p_lastframe->size == s &&
262 !memcmp(oport->p_lastframe->frame, frame, s)) {
263 /* Already received the same frame */
264 VLOG_DBG("duplicate frame, no need to decode");
265 oport->p_lastupdate = time_now();
270 guess = lldpd_guess_type(cfg, frame, s);
271 VLOG_DBG("guessed %d enabled:%d", guess, cfg->g_protocols[0].enabled);
273 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
274 if (!cfg->g_protocols[i].enabled) {
277 if (cfg->g_protocols[i].mode == guess) {
278 VLOG_DBG("using decode function for %s protocol",
279 cfg->g_protocols[i].name);
280 if (cfg->g_protocols[i].decode(cfg, frame, s, hw, &chassis, &port)
282 VLOG_DBG("function for %s protocol did not "
284 cfg->g_protocols[i].name);
287 chassis->c_protocol = port->p_protocol = cfg->g_protocols[i].mode;
290 VLOG_DBG(" %"PRIuSIZE "mode:%d enabled:%d",
291 i, cfg->g_protocols[i].mode, cfg->g_protocols[i].enabled);
293 if (cfg->g_protocols[i].mode == 0) {
294 VLOG_DBG("unable to guess frame type on %s", hw->h_ifname);
298 /* Do we already have the same MSAP somewhere? */
299 VLOG_DBG("search for the same MSAP");
301 LIST_FOR_EACH (oport, p_entries, &hw->h_rports) {
302 if (port->p_protocol == oport->p_protocol) {
304 if (port->p_id_subtype == oport->p_id_subtype &&
305 port->p_id_len == oport->p_id_len &&
306 !memcmp(port->p_id, oport->p_id, port->p_id_len) &&
307 chassis->c_id_subtype == oport->p_chassis->c_id_subtype &&
308 chassis->c_id_len == oport->p_chassis->c_id_len &&
309 !memcmp(chassis->c_id, oport->p_chassis->c_id,
310 chassis->c_id_len)) {
311 ochassis = oport->p_chassis;
312 VLOG_DBG("MSAP is already known");
323 /* Do we have room for a new MSAP? */
324 if (!oport && cfg->g_config.c_max_neighbors) {
325 if (count == (cfg->g_config.c_max_neighbors - 1)) {
326 VLOG_DBG("max neighbors %d reached for port %s, "
327 "dropping any new ones silently",
328 cfg->g_config.c_max_neighbors,
330 } else if (count > cfg->g_config.c_max_neighbors - 1) {
331 VLOG_DBG("too many neighbors for port %s, drop this new one",
333 lldpd_port_cleanup(port, true);
334 lldpd_chassis_cleanup(chassis, true);
340 /* No, but do we already know the system? */
343 VLOG_DBG("MSAP is unknown, search for the chassis");
345 LIST_FOR_EACH (ochassis, list, &cfg->g_chassis) {
346 if ((chassis->c_protocol == ochassis->c_protocol) &&
347 (chassis->c_id_subtype == ochassis->c_id_subtype) &&
348 (chassis->c_id_len == ochassis->c_id_len) &&
349 (memcmp(chassis->c_id, ochassis->c_id,
350 chassis->c_id_len) == 0)) {
362 /* The port is known, remove it before adding it back */
363 list_remove(&oport->p_entries);
364 lldpd_port_cleanup(oport, 1);
369 lldpd_move_chassis(ochassis, chassis);
372 /* Chassis not known, add it */
373 VLOG_DBG("unknown chassis, add it to the list");
374 chassis->c_index = ++cfg->g_lastrid;
375 chassis->c_refcount = 0;
376 list_push_back(&cfg->g_chassis, &chassis->list);
377 listsize = list_size(&cfg->g_chassis);
378 VLOG_DBG("%"PRIuSIZE " different systems are known", listsize);
382 port->p_lastchange = port->p_lastupdate = time_now();
383 port->p_lastframe = xmalloc(s + sizeof(struct lldpd_frame));
384 port->p_lastframe->size = s;
385 memcpy(port->p_lastframe->frame, frame, s);
386 list_insert(&hw->h_rports, &port->p_entries);
388 port->p_chassis = chassis;
389 port->p_chassis->c_refcount++;
390 /* Several cases are possible :
391 * 1. chassis is new, its refcount was 0. It is now attached
392 * to this port, its refcount is 1.
393 * 2. chassis already exists and was attached to another
394 * port, we increase its refcount accordingly.
395 * 3. chassis already exists and was attached to the same
396 * port, its refcount was decreased with
397 * lldpd_port_cleanup() and is now increased again.
399 * In all cases, if the port already existed, it has been
400 * freed with lldpd_port_cleanup() and therefore, the refcount
401 * of the chassis that was attached to it is decreased.
403 i = list_size(&hw->h_rports);
404 VLOG_DBG("%"PRIuSIZE " neighbors for %s", i, hw->h_ifname);
414 lldpd_hide_ports(struct lldpd *cfg,
415 struct lldpd_hardware *hw,
417 struct lldpd_port *port;
418 int protocols[LLDPD_MODE_MAX + 1];
424 VLOG_DBG("apply smart filter for port %s", hw->h_ifname);
426 /* Compute the number of occurrences of each protocol */
427 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
431 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
432 protocols[port->p_protocol]++;
435 /* Turn the protocols[] array into an array of
436 * enabled/disabled protocols. 1 means enabled, 0
439 min = (unsigned int) - 1;
440 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
441 if (protocols[i] && (protocols[i] < min)) {
445 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
446 if (protocols[i] == min && !found) {
447 /* If we need a tie breaker, we take the first protocol only */
448 if (cfg->g_config.c_smart & mask &
449 (SMART_OUTGOING_ONE_PROTO | SMART_INCOMING_ONE_PROTO)) {
458 /* We set the p_hidden flag to 1 if the protocol is disabled */
459 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
460 if (mask == SMART_OUTGOING) {
461 port->p_hidden_out = protocols[port->p_protocol] ? false : true;
463 port->p_hidden_in = protocols[port->p_protocol] ? false : true;
467 /* If we want only one neighbor, we take the first one */
468 if (cfg->g_config.c_smart & mask &
469 (SMART_OUTGOING_ONE_NEIGH | SMART_INCOMING_ONE_NEIGH)) {
472 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
473 if (mask == SMART_OUTGOING) {
475 port->p_hidden_out = true;
477 if (!port->p_hidden_out) {
481 if (mask == SMART_INCOMING) {
483 port->p_hidden_in = true;
485 if (!port->p_hidden_in) {
492 /* Print a debug message summarizing the operation */
493 for (i = 0; i <= LLDPD_MODE_MAX; i++) {
498 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
499 if (!((mask == SMART_OUTGOING && port->p_hidden_out) ||
500 (mask == SMART_INCOMING && port->p_hidden_in))) {
502 protocols[port->p_protocol] = 1;
508 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
509 if (cfg->g_protocols[i].enabled &&
510 protocols[cfg->g_protocols[i].mode]) {
512 strlen(cfg->g_protocols[i].name) + 3 > sizeof(buffer)) {
513 /* Unlikely, our buffer is too small */
514 memcpy(buffer + sizeof(buffer) - 4, "...", 4);
518 strncat(buffer, ", ", 2);
519 strncat(buffer, cfg->g_protocols[i].name,
520 strlen(cfg->g_protocols[i].name));
524 VLOG_DBG("%s: %s: %d visible neighbors (out of %d)",
526 (mask == SMART_OUTGOING) ? "out filter" : "in filter",
528 VLOG_DBG("%s: protocols: %s",
529 hw->h_ifname, buffer[0] ? buffer : "(none)");
532 /* Hide unwanted ports depending on smart mode set by the user */
534 lldpd_hide_all(struct lldpd *cfg)
536 struct lldpd_hardware *hw;
538 if (!cfg->g_config.c_smart) {
542 VLOG_DBG("apply smart filter results on all ports");
544 LIST_FOR_EACH (hw, h_entries, &cfg->g_hardware) {
545 if (cfg->g_config.c_smart & SMART_INCOMING_FILTER) {
546 lldpd_hide_ports(cfg, hw, SMART_INCOMING);
548 if (cfg->g_config.c_smart & SMART_OUTGOING_FILTER) {
549 lldpd_hide_ports(cfg, hw, SMART_OUTGOING);
555 lldpd_recv(struct lldpd *cfg,
556 struct lldpd_hardware *hw,
562 VLOG_DBG("receive a frame on %s", hw->h_ifname);
563 if (cfg->g_config.c_paused) {
564 VLOG_DBG("paused, ignore the frame on %s", hw->h_ifname);
568 VLOG_DBG("decode received frame on %s h_rx_cnt=%" PRIu64,
569 hw->h_ifname, hw->h_rx_cnt);
570 lldpd_decode(cfg, buffer, n, hw);
571 lldpd_hide_all(cfg); /* Immediatly hide */
575 lldpd_send(struct lldpd_hardware *hw, struct dp_packet *p)
577 struct lldpd *cfg = hw->h_cfg;
578 struct lldpd_port *port;
582 if (cfg->g_config.c_receiveonly || cfg->g_config.c_paused) {
586 if ((hw->h_flags & IFF_RUNNING) == 0) {
591 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
592 if (!cfg->g_protocols[i].enabled) {
596 /* We send only if we have at least one remote system
597 * speaking this protocol or if the protocol is forced */
598 if (cfg->g_protocols[i].enabled > 1) {
599 if ((lldp_size = cfg->g_protocols[i].send(cfg, hw, p)) != -E2BIG) {
603 VLOG_DBG("send PDU on %s failed E2BIG", hw->h_ifname);
608 LIST_FOR_EACH (port, p_entries, &hw->h_rports) {
609 /* If this remote port is disabled, we don't consider it */
610 if (port->p_hidden_out) {
613 if (port->p_protocol == cfg->g_protocols[i].mode) {
614 VLOG_DBG("send PDU on %s with protocol %s",
615 hw->h_ifname, cfg->g_protocols[i].name);
616 lldp_size = cfg->g_protocols[i].send(cfg, hw, p);
624 /* Nothing was sent for this port, let's speak the first
625 * available protocol.
627 for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
628 if (!cfg->g_protocols[i].enabled) {
631 VLOG_DBG("fallback to protocol %s for %s",
632 cfg->g_protocols[i].name, hw->h_ifname);
633 lldp_size = cfg->g_protocols[i].send(cfg, hw, p);
636 if (cfg->g_protocols[i].mode == 0) {
637 VLOG_WARN("no protocol enabled, dunno what to send");