]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
mnl: do not cache sender buffer size
authorPablo Neira Ayuso <pablo@netfilter.org>
Fri, 20 Sep 2019 15:01:47 +0000 (17:01 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sun, 22 Sep 2019 07:15:16 +0000 (09:15 +0200)
SO_SNDBUF never fails, this socket option just provides a hint to the
kernel.  SO_SNDBUFFORCE sets the buffer size to zero if the value goes
over INT_MAX. Userspace is caching the buffer hint that sends to the
kernel, so it might leave userspace out of sync if the kernel ignores
the hint. Do not make assumptions, fetch the sender buffer size from the
kernel via getsockopt().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/mnl.c

index 57ff89f50e2321229bbd63cfa7599f9cdec756e8..14fa4a7186fd3fb6e2be94471b8e7977a3f4826b 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -218,24 +218,24 @@ void mnl_err_list_free(struct mnl_err *err)
        xfree(err);
 }
 
-static int nlbuffsiz;
-
 static void mnl_set_sndbuffer(const struct mnl_socket *nl,
                              struct nftnl_batch *batch)
 {
+       socklen_t len = sizeof(int);
+       int sndnlbuffsiz = 0;
        int newbuffsiz;
 
-       if (nftnl_batch_iovec_len(batch) * BATCH_PAGE_SIZE <= nlbuffsiz)
-               return;
+       getsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_SNDBUF,
+                  &sndnlbuffsiz, &len);
 
        newbuffsiz = nftnl_batch_iovec_len(batch) * BATCH_PAGE_SIZE;
+       if (newbuffsiz <= sndnlbuffsiz)
+               return;
 
        /* Rise sender buffer length to avoid hitting -EMSGSIZE */
        if (setsockopt(mnl_socket_get_fd(nl), SOL_SOCKET, SO_SNDBUFFORCE,
                       &newbuffsiz, sizeof(socklen_t)) < 0)
                return;
-
-       nlbuffsiz = newbuffsiz;
 }
 
 static unsigned int nlsndbufsiz;