git: 03cc3489a02d - main - ndp(8): increase buffer size in rtsock mode

From: Andrey V. Elsukov <ae_at_FreeBSD.org>
Date: Mon, 19 Feb 2024 07:52:47 UTC
The branch main has been updated by ae:

URL: https://cgit.FreeBSD.org/src/commit/?id=03cc3489a02da0eba3b2737210486723d1072b21

commit 03cc3489a02da0eba3b2737210486723d1072b21
Author:     Boris Lytochkin <lytboris@gmail.com>
AuthorDate: 2024-02-19 07:44:52 +0000
Commit:     Andrey V. Elsukov <ae@FreeBSD.org>
CommitDate: 2024-02-19 07:44:52 +0000

    ndp(8): increase buffer size in rtsock mode
    
    On a router with many connected devices (~10k+) `ndp -an` can fail
    with ENOMEM because of some additional NDP records were added
    between sysctl() buffer size estimate and data fetch calls.
    
    Allocate more space based on size estimate: 1/64 (~2%) of additional
    space, but not less that 4 m_rtmsg structures.
    
    Obtained from:  Yandex LLC
    MFC after:      2 weeks
    Sponsored by:   Yandex LLC
    Differential Revision:  https://reviews.freebsd.org/D43956
---
 usr.sbin/ndp/ndp.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/usr.sbin/ndp/ndp.c b/usr.sbin/ndp/ndp.c
index 9ade2469742e..9d9ae02dc1e2 100644
--- a/usr.sbin/ndp/ndp.c
+++ b/usr.sbin/ndp/ndp.c
@@ -652,6 +652,12 @@ again:;
 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
 		xo_err(1, "sysctl(PF_ROUTE estimate)");
 	if (needed > 0) {
+		/*
+		 * Add ~2% additional space in case some records
+		 * will appear between sysctl() calls.
+		 * Round it up so it can fit 4 additional messages at least.
+		 */
+		needed += ((needed >> 6) | (sizeof(m_rtmsg) * 4));
 		if ((buf = malloc(needed)) == NULL)
 			xo_err(1, "malloc");
 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)