]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
Change the way Chassis ID is assigned.
authorVincent Bernat <bernat@luffy.cx>
Tue, 24 Jan 2012 09:59:39 +0000 (10:59 +0100)
committerVincent Bernat <bernat@luffy.cx>
Tue, 24 Jan 2012 10:01:19 +0000 (11:01 +0100)
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
src/lldpd.c
src/lldpd.h

index 93849a072be8caff5eb4ae97687c7d66f7b739a1..2a55d38c19299de64acfe1ea8f9fc696454fcd3a 100644 (file)
@@ -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,
index cecc8a83fd81f31c1d85fa311d94f9ba7d2be9fa..6d2a3a29791cb8e18d33c775945097e873a682e1 100644 (file)
@@ -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;
index 74c14d98c45de3e1a19873ff8970c19a2e50f124..8bebd3a27d8f956c8cf837eb5c9a67c965ad9a17 100644 (file)
@@ -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