static struct protocol protos[] =
{
{ LLDPD_MODE_LLDP, 1, "LLDP", 'l', lldp_send, lldp_decode, NULL,
- LLDP_MULTICAST_ADDR },
+ LLDP_ADDR_NEAREST_BRIDGE,
+ LLDP_ADDR_NEAREST_NONTPMR_BRIDGE,
+ LLDP_ADDR_NEAREST_CUSTOMER_BRIDGE },
#ifdef ENABLE_CDP
{ LLDPD_MODE_CDPV1, 0, "CDPv1", 'c', cdpv1_send, cdp_decode, cdpv1_guess,
CDP_MULTICAST_ADDR },
}
struct lldpd_hardware *
-lldpd_get_hardware(struct lldpd *cfg, char *name, int index, struct lldpd_ops *ops)
+lldpd_get_hardware(struct lldpd *cfg, char *name, int index)
{
struct lldpd_hardware *hardware;
TAILQ_FOREACH(hardware, &cfg->g_hardware, h_entries) {
if ((strcmp(hardware->h_ifname, name) == 0) &&
- (hardware->h_ifindex == index) &&
- ((!ops) || (ops == hardware->h_ops)))
+ (hardware->h_ifindex == index))
break;
}
return hardware;
}
+/**
+ * Allocate the default local port. This port will be cloned each time we need a
+ * new local port.
+ */
+static void
+lldpd_alloc_default_local_port(struct lldpd *cfg)
+{
+ struct lldpd_port *port;
+
+ if ((port = (struct lldpd_port *)
+ calloc(1, sizeof(struct lldpd_port))) == NULL)
+ fatal("main", NULL);
+
+#ifdef ENABLE_DOT1
+ TAILQ_INIT(&port->p_vlans);
+ TAILQ_INIT(&port->p_ppvids);
+ TAILQ_INIT(&port->p_pids);
+#endif
+#ifdef ENABLE_CUSTOM
+ TAILQ_INIT(&port->p_custom_list);
+#endif
+ cfg->g_default_local_port = port;
+}
+
+/**
+ * Clone a given port. The destination needs to be already allocated.
+ */
+static int
+lldpd_clone_port(struct lldpd_port *destination, struct lldpd_port *source)
+{
+
+ u_int8_t *output = NULL;
+ ssize_t output_len;
+ struct lldpd_port *cloned = NULL;
+ output_len = lldpd_port_serialize(source, (void**)&output);
+ if (output_len == -1 ||
+ lldpd_port_unserialize(output, output_len, &cloned) <= 0) {
+ log_warnx("alloc", "unable to clone default port");
+ free(output);
+ return -1;
+ }
+ memcpy(destination, cloned, sizeof(struct lldpd_port));
+ free(cloned);
+ free(output);
+#ifdef ENABLE_DOT1
+ marshal_repair_tailq(lldpd_vlan, &destination->p_vlans, v_entries);
+ marshal_repair_tailq(lldpd_ppvid, &destination->p_ppvids, p_entries);
+ marshal_repair_tailq(lldpd_pi, &destination->p_pids, p_entries);
+#endif
+#ifdef ENABLE_CUSTOM
+ marshal_repair_tailq(lldpd_custom, &destination->p_custom_list, next);
+#endif
+ return 0;
+}
+
struct lldpd_hardware *
lldpd_alloc_hardware(struct lldpd *cfg, char *name, int index)
{
calloc(1, sizeof(struct lldpd_hardware))) == NULL)
return NULL;
+ /* Clone default local port */
+ if (lldpd_clone_port(&hardware->h_lport, cfg->g_default_local_port) == -1) {
+ log_warnx("alloc", "unable to clone default port");
+ free(hardware);
+ return NULL;
+ }
+
hardware->h_cfg = cfg;
strlcpy(hardware->h_ifname, name, sizeof(hardware->h_ifname));
hardware->h_ifindex = index;
hardware->h_lport.p_med_cap_enabled |= LLDP_MED_CAP_IV;
}
#endif
-#ifdef ENABLE_DOT1
- TAILQ_INIT(&hardware->h_lport.p_vlans);
- TAILQ_INIT(&hardware->h_lport.p_ppvids);
- TAILQ_INIT(&hardware->h_lport.p_pids);
-#endif
-#ifdef ENABLE_CUSTOM
- TAILQ_INIT(&hardware->h_lport.p_custom_list);
-#endif
levent_hardware_init(hardware);
return hardware;
return NULL;
}
mgmt->m_family = family;
- assert(addrsize <= LLDPD_MGMT_MAXADDRSIZE);
memcpy(&mgmt->m_addr, addrptr, addrsize);
mgmt->m_addrsize = addrsize;
mgmt->m_iface = iface;
}
neighbors--;
if (neighbors == 0)
- setproctitle("no neighbor");
+ setproctitle("no neighbor.");
else if (neighbors == 1 && neighbor && *neighbor != '\0')
- setproctitle("connected to %s", neighbor);
+ setproctitle("connected to %s.", neighbor);
else
- setproctitle("%d neighbor%s", neighbors,
+ setproctitle("%d neighbor%s.", neighbors,
(neighbors > 1)?"s":"");
#endif
lldpd_display_neighbors(cfg);
* change. To do this, we zero out fields that are not
* significant, marshal the port, then restore. */
struct lldpd_port *port = &hardware->h_lport;
+ /* Take the current flags into account to detect a change. */
+ port->_p_hardware_flags = hardware->h_flags;
u_int8_t *output = NULL;
ssize_t output_len;
char save[LLDPD_PORT_START_MARKER];
}
}
+static void
+lldpd_all_chassis_cleanup(struct lldpd *cfg)
+{
+ struct lldpd_chassis *chassis, *chassis_next;
+ log_debug("localchassis", "cleanup all chassis");
+
+ for (chassis = TAILQ_FIRST(&cfg->g_chassis); chassis;
+ chassis = chassis_next) {
+ chassis_next = TAILQ_NEXT(chassis, c_entries);
+ if (chassis->c_refcount == 0) {
+ TAILQ_REMOVE(&cfg->g_chassis, chassis, c_entries);
+ lldpd_chassis_cleanup(chassis, 1);
+ }
+ }
+}
+
void
lldpd_cleanup(struct lldpd *cfg)
{
struct lldpd_hardware *hardware, *hardware_next;
- struct lldpd_chassis *chassis, *chassis_next;
log_debug("localchassis", "cleanup all ports");
TAILQ_REMOVE(&cfg->g_hardware, hardware, h_entries);
lldpd_remote_cleanup(hardware, notify_clients_deletion, 1);
lldpd_hardware_cleanup(cfg, hardware);
- } else
- lldpd_remote_cleanup(hardware, notify_clients_deletion, 0);
- }
-
- log_debug("localchassis", "cleanup all chassis");
-
- for (chassis = TAILQ_FIRST(&cfg->g_chassis); chassis;
- chassis = chassis_next) {
- chassis_next = TAILQ_NEXT(chassis, c_entries);
- if (chassis->c_refcount == 0) {
- TAILQ_REMOVE(&cfg->g_chassis, chassis, c_entries);
- lldpd_chassis_cleanup(chassis, 1);
+ } else {
+ lldpd_remote_cleanup(hardware, notify_clients_deletion,
+ !(hardware->h_flags & IFF_RUNNING));
}
}
- lldpd_count_neighbors(cfg);
levent_schedule_cleanup(cfg);
+ lldpd_all_chassis_cleanup(cfg);
+ lldpd_count_neighbors(cfg);
}
/* Update chassis `ochassis' with values from `chassis'. The later one is not
if (!cfg->g_protocols[i].enabled)
continue;
if (cfg->g_protocols[i].guess == NULL) {
- if (memcmp(frame, cfg->g_protocols[i].mac, ETHER_ADDR_LEN) == 0) {
+ if (memcmp(frame, cfg->g_protocols[i].mac1, ETHER_ADDR_LEN) == 0 ||
+ memcmp(frame, cfg->g_protocols[i].mac2, ETHER_ADDR_LEN) == 0 ||
+ memcmp(frame, cfg->g_protocols[i].mac3, ETHER_ADDR_LEN) == 0) {
log_debug("decode", "guessed protocol is %s (from MAC address)",
cfg->g_protocols[i].name);
return cfg->g_protocols[i].mode;
log_debug("decode", "decode a received frame on %s",
hardware->h_ifname);
- if (s < sizeof(struct ether_header) + 4)
+ if (s < sizeof(struct ether_header) + 4) {
/* Too short, just discard it */
+ hardware->h_rx_discarded_cnt++;
return;
+ }
/* Decapsulate VLAN frames */
struct ether_header eheader;
s, hardware, &chassis, &port) == -1) {
log_debug("decode", "function for %s protocol did not decode this frame",
cfg->g_protocols[i].name);
+ hardware->h_rx_discarded_cnt++;
return;
}
chassis->c_protocol = port->p_protocol =
free(buffer);
return;
}
+ if (hardware->h_lport.p_disable_rx) {
+ log_debug("receive", "RX disabled, ignore the frame on %s",
+ hardware->h_ifname);
+ free(buffer);
+ return;
+ }
if (cfg->g_config.c_paused) {
log_debug("receive", "paused, ignore the frame on %s",
hardware->h_ifname);
{
struct lldpd *cfg = hardware->h_cfg;
if (cfg->g_config.c_receiveonly || cfg->g_config.c_paused) return;
+ if (hardware->h_lport.p_disable_tx) return;
if ((hardware->h_flags & IFF_RUNNING) == 0)
return;
int i, sent;
if (cfg->g_config.c_receiveonly || cfg->g_config.c_paused) return;
+ if (hardware->h_lport.p_disable_tx) return;
if ((hardware->h_flags & IFF_RUNNING) == 0)
return;
fatal("localchassis", "failed to set minimal system description");
}
}
+ if (cfg->g_config.c_platform == NULL)
+ cfg->g_config.c_platform = strdup(un.sysname);
/* Check routing */
if (lldpd_routing_enabled(cfg)) {
lldpd_remote_cleanup(hardware, NULL, 1);
lldpd_hardware_cleanup(cfg, hardware);
}
+ interfaces_cleanup(cfg);
+ lldpd_port_cleanup(cfg->g_default_local_port, 1);
+ lldpd_all_chassis_cleanup(cfg);
+ free(cfg->g_default_local_port);
+ free(cfg->g_config.c_platform);
+ levent_shutdown(cfg);
}
/**
* @return PID of running lldpcli or -1 if error.
*/
static pid_t
-lldpd_configure(int debug, const char *path, const char *ctlname)
+lldpd_configure(int use_syslog, int debug, const char *path, const char *ctlname)
{
pid_t lldpcli = vfork();
int devnull;
- char sdebug[debug + 3];
- memset(sdebug, 'd', debug + 3);
- sdebug[debug + 2] = '\0';
- sdebug[0] = '-'; sdebug[1] = 's';
+ char sdebug[debug + 4];
+ if (use_syslog)
+ strlcpy(sdebug, "-s", 3);
+ else {
+ /* debug = 0 -> -sd */
+ /* debug = 1 -> -sdd */
+ /* debug = 2 -> -sddd */
+ memset(sdebug, 'd', sizeof(sdebug));
+ sdebug[debug + 3] = '\0';
+ sdebug[0] = '-'; sdebug[1] = 's';
+ }
log_debug("main", "invoke %s %s", path, sdebug);
switch (lldpcli) {
execl(path, "lldpcli", sdebug,
"-u", ctlname,
- "-c", SYSCONFDIR "/lldpd.conf",
- "-c", SYSCONFDIR "/lldpd.d",
+ "-C", SYSCONFDIR "/lldpd.conf",
+ "-C", SYSCONFDIR "/lldpd.d",
"resume",
(char *)NULL);
log_warn("main", "unable to execute %s", path);
}
#endif
+#ifdef HOST_OS_LINUX
+static void
+version_convert(const char *sversion, unsigned iversion[], size_t n)
+{
+ const char *p = sversion;
+ char *end;
+ for (size_t i = 0; i < n; i++) {
+ iversion[i] = strtol(p, &end, 10);
+ if (*end != '.') break;
+ p = end + 1;
+ }
+}
+
+static void
+version_check(void)
+{
+ struct utsname uts;
+ if (uname(&uts) == -1) return;
+ unsigned version_min[3] = {};
+ unsigned version_cur[3] = {};
+ version_convert(uts.release, version_cur, 3);
+ version_convert(MIN_LINUX_KERNEL_VERSION, version_min, 3);
+ if (version_min[0] > version_cur[0] ||
+ (version_min[0] == version_cur[0] && version_min[1] > version_cur[1]) ||
+ (version_min[0] == version_cur[0] && version_min[1] == version_cur[1] &&
+ version_min[2] > version_cur[2])) {
+ log_warnx("lldpd", "minimal kernel version required is %s, got %s",
+ MIN_LINUX_KERNEL_VERSION, uts.release);
+ log_warnx("lldpd", "lldpd may be unable to detect bonds and bridges correctly");
+#ifndef ENABLE_OLDIES
+ log_warnx("lldpd", "consider recompiling with --enable-oldies option");
+#endif
+ }
+}
+#else
+static void version_check(void) {}
+#endif
+
int
lldpd_main(int argc, char *argv[], char *envp[])
{
struct lldpd *cfg;
struct lldpd_chassis *lchassis;
- int ch, debug = 0;
+ int ch, debug = 0, use_syslog = 1, daemonize = 1;
+ const char *errstr;
#ifdef USE_SNMP
int snmp = 0;
const char *agentx = NULL; /* AgentX socket */
* unless there is a very good reason. Most command-line options will
* get deprecated at some point. */
char *popt, opts[] =
- "H:vhkrdD:xX:m:u:4:6:I:C:p:M:P:S:iL:@ ";
+ "H:vhkrdD:p:xX:m:u:4:6:I:C:p:M:P:S:iL:@ ";
int i, found, advertise_version = 1;
#ifdef ENABLE_LLDPMED
int lldpmed = 0, noinventory = 0;
char *platform_override = NULL;
char *lsb_release = NULL;
const char *lldpcli = LLDPCLI_PATH;
+ const char *pidfile = LLDPD_PID_FILE;
int smart = 15;
- int receiveonly = 0;
+ int receiveonly = 0, version = 0;
int ctl;
#ifdef ENABLE_PRIVSEP
usage();
break;
case 'v':
- fprintf(stdout, "%s\n", PACKAGE_VERSION);
- exit(0);
+ version++;
break;
case 'd':
- debug++;
+ if (daemonize)
+ daemonize = 0;
+ else if (use_syslog)
+ use_syslog = 0;
+ else
+ debug++;
break;
case 'D':
log_accept(optarg);
break;
+ case 'p':
+ pidfile = optarg;
+ break;
case 'r':
receiveonly = 1;
break;
break;
#ifdef ENABLE_LLDPMED
case 'M':
- lldpmed = atoi(optarg);
- if ((lldpmed < 1) || (lldpmed > 4)) {
+ lldpmed = strtonum(optarg, 1, 4, &errstr);
+ if (errstr) {
fprintf(stderr, "-M requires an argument between 1 and 4\n");
usage();
}
platform_override = strdup(optarg);
break;
case 'H':
- smart = atoi(optarg);
+ smart = strtonum(optarg, 0, sizeof(filters)/sizeof(filters[0]),
+ &errstr);
+ if (errstr) {
+ fprintf(stderr, "-H requires an int between 0 and %zu\n",
+ sizeof(filters)/sizeof(filters[0]));
+ usage();
+ }
break;
default:
found = 0;
}
}
+ if (version) {
+ version_display(stdout, "lldpd", version > 1);
+ exit(0);
+ }
+
if (ctlname == NULL) ctlname = LLDPD_CTL_SOCKET;
/* Set correct smart mode */
}
smart = filters[i].b;
- log_init(debug, __progname);
+ log_init(use_syslog, debug, __progname);
tzset(); /* Get timezone info before chroot */
-
- log_debug("main", "lldpd starting...");
+ if (use_syslog && daemonize) {
+ /* So, we use syslog and we daemonize (or we are started by
+ * upstart/systemd). No need to continue writing to stdout. */
+ int fd;
+ if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
+ dup2(fd, STDIN_FILENO);
+ dup2(fd, STDOUT_FILENO);
+ dup2(fd, STDERR_FILENO);
+ if (fd > 2) close(fd);
+ }
+ }
+ log_debug("main", "lldpd " PACKAGE_VERSION " starting...");
+ version_check();
/* Grab uid and gid to use for priv sep */
#ifdef ENABLE_PRIVSEP
if ((user = getpwnam(PRIVSEP_USER)) == NULL)
- fatal("main", "no " PRIVSEP_USER " user for privilege separation");
+ fatalx("main", "no " PRIVSEP_USER " user for privilege separation, please create it");
uid = user->pw_uid;
if ((group = getgrnam(PRIVSEP_GROUP)) == NULL)
- fatal("main", "no " PRIVSEP_GROUP " group for privilege separation");
+ fatalx("main", "no " PRIVSEP_GROUP " group for privilege separation, please create it");
gid = group->gr_gid;
#endif
log_warn("main", "cannot determine if another daemon is already running");
fatalx("main", "giving up");
}
- log_warn("main", "unable to create control socket");
+ log_warn("main", "unable to create control socket at %s", ctlname);
fatalx("main", "giving up");
}
#ifdef ENABLE_PRIVSEP
/* Disable SIGHUP, until handlers are installed */
signal(SIGHUP, SIG_IGN);
- /* Configuration with lldpcli */
- if (lldpcli) {
- log_debug("main", "invoking lldpcli for configuration");
- if (lldpd_configure(debug, lldpcli, ctlname) == -1)
- fatal("main", "unable to spawn lldpcli");
- }
-
/* Daemonization, unless started by upstart, systemd or launchd or debug */
#ifndef HOST_OS_OSX
- if (!lldpd_started_by_upstart() && !lldpd_started_by_systemd() &&
- !debug) {
+ if (daemonize &&
+ !lldpd_started_by_upstart() && !lldpd_started_by_systemd()) {
int pid;
char *spid;
- log_debug("main", "daemonize");
- if (daemon(0, 0) != 0)
+ log_info("main", "going into background");
+ if (daemon(0, 1) != 0)
fatal("main", "failed to detach daemon");
- if ((pid = open(LLDPD_PID_FILE,
+ if ((pid = open(pidfile,
O_TRUNC | O_CREAT | O_WRONLY, 0666)) == -1)
- fatal("main", "unable to open pid file " LLDPD_PID_FILE);
+ fatal("main", "unable to open pid file " LLDPD_PID_FILE
+ " (or the specified one)");
if (asprintf(&spid, "%d\n", getpid()) == -1)
- fatal("main", "unable to create pid file " LLDPD_PID_FILE);
+ fatal("main", "unable to create pid file " LLDPD_PID_FILE
+ " (or the specified one)");
if (write(pid, spid, strlen(spid)) == -1)
- fatal("main", "unable to write pid file " LLDPD_PID_FILE);
+ fatal("main", "unable to write pid file " LLDPD_PID_FILE
+ " (or the specified one)");
free(spid);
close(pid);
}
#endif
+ /* Configuration with lldpcli */
+ if (lldpcli) {
+ log_debug("main", "invoking lldpcli for configuration");
+ if (lldpd_configure(use_syslog, debug, lldpcli, ctlname) == -1)
+ fatal("main", "unable to spawn lldpcli");
+ }
+
/* Try to read system information from /etc/os-release if possible.
Fall back to lsb_release for compatibility. */
log_debug("main", "get OS/LSB release information");
calloc(1, sizeof(struct lldpd))) == NULL)
fatal("main", NULL);
+ lldpd_alloc_default_local_port(cfg);
cfg->g_ctlname = ctlname;
cfg->g_ctl = ctl;
cfg->g_config.c_mgmt_pattern = mgmtp;
cfg->g_config.c_receiveonly = receiveonly;
cfg->g_config.c_tx_interval = LLDPD_TX_INTERVAL;
cfg->g_config.c_tx_hold = LLDPD_TX_HOLD;
+ cfg->g_config.c_ttl = cfg->g_config.c_tx_interval * cfg->g_config.c_tx_hold;
cfg->g_config.c_max_neighbors = LLDPD_MAX_NEIGHBORS;
#ifdef ENABLE_LLDPMED
cfg->g_config.c_enable_fast_start = enable_fast_start;
cfg->g_config.c_noinventory = 1;
#endif
- /* Set TTL */
- lchassis->c_ttl = cfg->g_config.c_tx_interval * cfg->g_config.c_tx_hold;
-
log_debug("main", "initialize protocols");
cfg->g_protocols = protos;
for (i=0; protos[i].mode != 0; i++) {
/* Main loop */
log_debug("main", "start main loop");
levent_loop(cfg);
+ lchassis->c_refcount--;
lldpd_exit(cfg);
free(cfg);