From b4ac808343b7b1cece806f2aa6c0bc30a83ac147 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Tue, 24 Jan 2012 10:59:39 +0100 Subject: [PATCH] Change the way Chassis ID is assigned. By default, chassis ID is assigned the chassis name. Once an appropriate interface is discovered, the chassis ID is changed to the MAC address of this interface. In most configurations, the chassis ID is always set to the MAC address of the first interface, like this was before this patch. --- src/interfaces.c | 36 ++++++++++++++++++++++++++++++++++++ src/lldpd.c | 19 +++++++++---------- src/lldpd.h | 1 + 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/interfaces.c b/src/interfaces.c index 93849a07..2a55d38c 100644 --- a/src/interfaces.c +++ b/src/interfaces.c @@ -1076,6 +1076,42 @@ lldpd_ifh_mgmt(struct lldpd *cfg, struct ifaddrs *ifap) } } +/* Fill out chassis ID if not already done. This handler is special + because we will only handle interfaces that are already handled. */ +void +lldpd_ifh_chassis(struct lldpd *cfg, struct ifaddrs *ifap) +{ + struct ifaddrs *ifa; + struct lldpd_hardware *hardware; + + if (LOCAL_CHASSIS(cfg)->c_id != NULL && + LOCAL_CHASSIS(cfg)->c_id_subtype == LLDP_CHASSISID_SUBTYPE_LLADDR) + return; /* We already have one */ + + for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_flags) continue; /* This interface is not valid */ + + if ((hardware = lldpd_get_hardware(cfg, + ifa->ifa_name, + if_nametoindex(ifa->ifa_name), + NULL)) == NULL) + /* That's odd. Let's skip. */ + continue; + + char *name = malloc(sizeof(hardware->h_lladdr)); + if (!name) { + LLOG_WARN("Not enough memory for chassis ID"); + return; + } + free(LOCAL_CHASSIS(cfg)->c_id); + memcpy(name, hardware->h_lladdr, sizeof(hardware->h_lladdr)); + LOCAL_CHASSIS(cfg)->c_id = name; + LOCAL_CHASSIS(cfg)->c_id_len = sizeof(hardware->h_lladdr); + LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR; + return; + } +} + struct lldpd_ops eth_ops = { .send = iface_eth_send, .recv = iface_eth_recv, diff --git a/src/lldpd.c b/src/lldpd.c index cecc8a83..6d2a3a29 100644 --- a/src/lldpd.c +++ b/src/lldpd.c @@ -922,7 +922,6 @@ lldpd_update_localchassis(struct lldpd *cfg) char *hp; int f; char status; - struct lldpd_hardware *hardware; /* Set system name and description */ if (uname(&un) != 0) @@ -970,16 +969,15 @@ lldpd_update_localchassis(struct lldpd *cfg) LOCAL_CHASSIS(cfg)->c_med_sw = strdup("Unknown"); #endif - /* Set chassis ID if needed */ - if ((LOCAL_CHASSIS(cfg)->c_id == NULL) && - (hardware = TAILQ_FIRST(&cfg->g_hardware))) { - if ((LOCAL_CHASSIS(cfg)->c_id = - malloc(sizeof(hardware->h_lladdr))) == NULL) + /* Set chassis ID if needed. This is only done if chassis ID + has not been set previously (with the MAC address of an + interface for example) + */ + if (LOCAL_CHASSIS(cfg)->c_id == NULL) { + if (!(LOCAL_CHASSIS(cfg)->c_id = strdup(LOCAL_CHASSIS(cfg)->c_name))) fatal(NULL); - LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LLADDR; - LOCAL_CHASSIS(cfg)->c_id_len = sizeof(hardware->h_lladdr); - memcpy(LOCAL_CHASSIS(cfg)->c_id, - hardware->h_lladdr, sizeof(hardware->h_lladdr)); + LOCAL_CHASSIS(cfg)->c_id_len = strlen(LOCAL_CHASSIS(cfg)->c_name); + LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL; } } @@ -996,6 +994,7 @@ lldpd_update_localports(struct lldpd *cfg) lldpd_ifh_vlan, /* Handle VLAN */ #endif lldpd_ifh_mgmt, /* Handle management address (if not already handled) */ + lldpd_ifh_chassis, /* Handle chassis ID (if not already handled) */ NULL }; lldpd_ifhandlers *ifh; diff --git a/src/lldpd.h b/src/lldpd.h index 74c14d98..8bebd3a2 100644 --- a/src/lldpd.h +++ b/src/lldpd.h @@ -503,6 +503,7 @@ void lldpd_ifh_eth(struct lldpd *, struct ifaddrs *); void lldpd_ifh_vlan(struct lldpd *, struct ifaddrs *); #endif void lldpd_ifh_mgmt(struct lldpd *, struct ifaddrs *); +void lldpd_ifh_chassis(struct lldpd *, struct ifaddrs *); /* dmi.c */ #ifdef ENABLE_LLDPMED -- 2.39.5