Blob Blame History Raw
From 46d50fe9ca30e8377995fea2b7d59239b46a0cfa Mon Sep 17 00:00:00 2001
Message-Id: <46d50fe9ca30e8377995fea2b7d59239b46a0cfa@dist-git>
From: Laine Stump <laine@laine.org>
Date: Mon, 14 Jan 2019 11:35:04 -0500
Subject: [PATCH] util: use nlmsg_find_attr() instead of an open-coded loop

This is about the same number of code lines, but is simpler, and more
consistent with what will be added to check another attribute in a
coming patch.

As a side effect, it

Resolves: https://bugzilla.redhat.com/1583131

Signed-off-by: Laine Stump <laine@laine.org>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
(cherry picked from commit 7282f455aaaa8bf2f73236aa9ec6b65b33c0fcdb)

Conflicts: src/util/virnetdevip.c -  more blowback from the addition of VIR_AUTOPTR
                             upstream.
Signed-off-by: Laine Stump <laine@laine.org>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/util/virnetdevip.c | 52 ++++++++++++++++++------------------------
 1 file changed, 22 insertions(+), 30 deletions(-)

diff --git a/src/util/virnetdevip.c b/src/util/virnetdevip.c
index 937ebcdbdb..d56f4f05f3 100644
--- a/src/util/virnetdevip.c
+++ b/src/util/virnetdevip.c
@@ -564,50 +564,42 @@ virNetDevIPCheckIPv6ForwardingCallback(struct nlmsghdr *resp,
 {
     struct rtmsg *rtmsg = NLMSG_DATA(resp);
     int accept_ra = -1;
-    struct rtattr *rta;
+    struct rtattr *rta_attr;
     char *ifname = NULL;
+    int ifindex = -1;
     struct virNetDevIPCheckIPv6ForwardingData *data = opaque;
     int ret = 0;
-    int len = RTM_PAYLOAD(resp);
-    int oif = -1;
 
     /* Ignore messages other than route ones */
     if (resp->nlmsg_type != RTM_NEWROUTE)
         return ret;
 
-    /* Extract a device ID attribute */
-    VIR_WARNINGS_NO_CAST_ALIGN
-    for (rta = RTM_RTA(rtmsg); RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
-        VIR_WARNINGS_RESET
-        if (rta->rta_type == RTA_OIF) {
-            oif = *(int *)RTA_DATA(rta);
-
-            /* Should never happen: netlink message would be broken */
-            if (ifname) {
-                char *ifname2 = virNetDevGetName(oif);
-                VIR_WARN("Single route has unexpected 2nd interface "
-                         "- '%s' and '%s'", ifname, ifname2);
-                VIR_FREE(ifname2);
-                break;
-            }
-
-            if (!(ifname = virNetDevGetName(oif)))
-                goto error;
-        }
-    }
-
     /* No need to do anything else for non RA routes */
     if (rtmsg->rtm_protocol != RTPROT_RA)
         goto cleanup;
 
-    data->hasRARoutes = true;
+    rta_attr = (struct rtattr *)nlmsg_find_attr(resp, sizeof(struct rtmsg), RTA_OIF);
+    if (rta_attr) {
+        /* This is a single path route, with interface used to reach
+         * nexthop in the RTA_OIF attribute.
+         */
+        ifindex = *(int *)RTA_DATA(rta_attr);
+        ifname = virNetDevGetName(ifindex);
 
-    /* Check the accept_ra value for the interface */
-    accept_ra = virNetDevIPGetAcceptRA(ifname);
-    VIR_DEBUG("Checking route for device %s, accept_ra: %d", ifname, accept_ra);
+        if (!ifname)
+            goto error;
 
-    if (accept_ra != 2 && virNetDevIPCheckIPv6ForwardingAddIF(data, &ifname) < 0)
-        goto error;
+        accept_ra = virNetDevIPGetAcceptRA(ifname);
+
+        VIR_DEBUG("Checking route for device %s (%d), accept_ra: %d",
+                  ifname, ifindex, accept_ra);
+
+        if (accept_ra != 2 && virNetDevIPCheckIPv6ForwardingAddIF(data, &ifname) < 0)
+            goto error;
+
+        data->hasRARoutes = true;
+        goto cleanup;
+    }
 
  cleanup:
     VIR_FREE(ifname);
-- 
2.20.1