From ba85f9f40055b610aac3834f14e78e0d1f406107 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 2 Jul 2011 20:05:21 +0200 Subject: [PATCH] Add a way to specify which interfaces lldpd should listen to. --- CHANGELOG | 1 + man/lldpd.8 | 6 ++++++ src/interfaces.c | 35 +++++++++++++++++++++++++++++++++++ src/lldpd.c | 8 +++++++- src/lldpd.h | 2 ++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index b62ae9dd..be39c176 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ lldpd (0.5.4) * Features: + Get OS information from /etc/os-release if available. Patch from Michael Tremer. + + Add a flag to specify which interfaces lldpd should listen to. lldpd (0.5.3) * Fixes: diff --git a/man/lldpd.8 b/man/lldpd.8 index 6d5fcbce..0f97c57c 100644 --- a/man/lldpd.8 +++ b/man/lldpd.8 @@ -25,6 +25,7 @@ .Op Fl S Ar description .Op Fl X Ar socket .Op Fl m Ar management +.Op Fl I Ar interfaces .Op Fl M Ar class .Op Fl H Ar hide .Sh DESCRIPTION @@ -116,6 +117,11 @@ Specify the management address of this system. only sends one management address. It will use the first one that it finds or the one that you specify with this option. This option can use wildcards. +.It Fl I Ar interfaces +Specify which interface to listen to. Without this option, +.Nm +will listen on all available interfaces. This option can use +wildcards. Several interfaces can be specified separated by commas. .It Fl M Ar class Enable emission of LLDP-MED frame. The class should be one of the following value: diff --git a/src/interfaces.c b/src/interfaces.c index 512c7c20..8ecc7f6e 100644 --- a/src/interfaces.c +++ b/src/interfaces.c @@ -735,6 +735,41 @@ lldpd_ifh_eth(struct lldpd *cfg, struct ifaddrs *ifap) } } +void +lldpd_ifh_whitelist(struct lldpd *cfg, struct ifaddrs *ifap) +{ + struct ifaddrs *ifa; + char *interfaces = NULL; + char *pattern; + + if (!cfg->g_interfaces) + return; + if ((interfaces = strdup(cfg->g_interfaces)) == NULL) { + LLOG_WARNX("unable to allocate memory"); + return; + } + + for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_flags == 0) continue; /* Already handled by someone else */ + strcpy(interfaces, cfg->g_interfaces); /* Restore our list of interfaces */ + pattern = strtok(interfaces, ","); + while (pattern != NULL) { + if (fnmatch(pattern, ifa->ifa_name, 0) == 0) { + /* This interface is whitelisted */ + break; + } + pattern = strtok(NULL, ","); + } + if (pattern == NULL) { + /* This interface was not found. We flag it. */ + LLOG_DEBUG("blacklist %s", ifa->ifa_name); + ifa->ifa_flags = 0; + } + } + + free(interfaces); +} + static int iface_bond_init(struct lldpd *cfg, struct lldpd_hardware *hardware) { diff --git a/src/lldpd.c b/src/lldpd.c index 8253d7e0..e51a6940 100644 --- a/src/lldpd.c +++ b/src/lldpd.c @@ -980,6 +980,7 @@ lldpd_update_localports(struct lldpd *cfg) struct ifaddrs *ifap; struct lldpd_hardware *hardware; lldpd_ifhandlers ifhs[] = { + lldpd_ifh_whitelist, /* Is the interface whitelisted? */ lldpd_ifh_bond, /* Handle bond */ lldpd_ifh_eth, /* Handle classic ethernet interfaces */ #ifdef ENABLE_DOT1 @@ -1108,8 +1109,9 @@ lldpd_main(int argc, char *argv[]) char *agentx = NULL; /* AgentX socket */ #endif char *mgmtp = NULL; + char *interfaces = NULL; char *popt, opts[] = - "H:hkrdxX:m:p:M:S:i@ "; + "H:hkrdxX:m:I:p:M:S:i@ "; int i, found, advertise_version = 1; #ifdef ENABLE_LLDPMED int lldpmed = 0, noinventory = 0; @@ -1142,6 +1144,9 @@ lldpd_main(int argc, char *argv[]) case 'm': mgmtp = optarg; break; + case 'I': + interfaces = optarg; + break; case 'k': advertise_version = 0; break; @@ -1244,6 +1249,7 @@ lldpd_main(int argc, char *argv[]) fatal(NULL); cfg->g_mgmt_pattern = mgmtp; + cfg->g_interfaces = interfaces; cfg->g_smart = smart; cfg->g_receiveonly = receiveonly; diff --git a/src/lldpd.h b/src/lldpd.h index 9543d73e..b11df407 100644 --- a/src/lldpd.h +++ b/src/lldpd.h @@ -345,6 +345,7 @@ struct lldpd { TAILQ_HEAD(, lldpd_callback) g_callbacks; char *g_mgmt_pattern; + char *g_interfaces; char *g_descr_override; char *g_lsb_release; @@ -447,6 +448,7 @@ int ctl_msg_pack_structure(char *, void *, unsigned int, struct hmsg *, void ** int ctl_msg_unpack_structure(char *, void *, unsigned int, struct hmsg *, void **); /* interfaces.c */ +void lldpd_ifh_whitelist(struct lldpd *, struct ifaddrs *); void lldpd_ifh_bond(struct lldpd *, struct ifaddrs *); void lldpd_ifh_eth(struct lldpd *, struct ifaddrs *); #ifdef ENABLE_DOT1 -- 2.39.5