From 03cc3489a02da0eba3b2737210486723d1072b21 Mon Sep 17 00:00:00 2001 From: Boris Lytochkin Date: Mon, 19 Feb 2024 10:44:52 +0300 Subject: [PATCH] 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)