From f44483ab60a86e667cb27f351a588b380ce241ab Mon Sep 17 00:00:00 2001 From: nickgarlis Date: Mon, 30 Jun 2025 18:06:33 +0200 Subject: [PATCH] Prevent buffer enlargement in specific scenarios We should not enlarge the socket buffers when: - We are using a test dial (there are no buffers to enlarge). - A connection has been initialized with socket options which means that the user could have specified fixed buffer sizes. --- conn.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/conn.go b/conn.go index 73eeeec..a193957 100644 --- a/conn.go +++ b/conn.go @@ -440,6 +440,22 @@ func (cc *Conn) getMessageSize() int { return total } +// canEnlargeBuffers returns true if the connection can automatically enlarge +// the write and read buffers of the netlink connection. +func (cc *Conn) canEnlargeBuffers() bool { + // If there are sock options, we assume that the user has already set the + // buffers to a fixed size. + if len(cc.sockOptions) > 0 { + return false + } + + if cc.TestDial != nil { + return false + } + + return true +} + // enlargeWriteBuffer automatically sets the write buffer of the given // connection to the accumulated message size. This is only done if the current // write buffer is smaller than the message size. @@ -453,6 +469,10 @@ func (cc *Conn) getMessageSize() int { // TODO: Update this function to mimic the behavior of nftables once our // socket library supports multiple iovec entries. func (cc *Conn) enlargeWriteBuffer(conn *netlink.Conn) error { + if !cc.canEnlargeBuffers() { + return nil + } + messageSize := cc.getMessageSize() writeBuffer, err := conn.WriteBuffer() if err != nil { @@ -481,6 +501,10 @@ func (cc *Conn) getDefaultEchoReadBuffer() int { // // See https://git.netfilter.org/nftables/tree/src/mnl.c?id=713592c6008a8c589a00d3d3d2e49709ff2de62c#n426 func (cc *Conn) enlargeReadBuffer(conn *netlink.Conn) error { + if !cc.canEnlargeBuffers() { + return nil + } + var bufferSize int // If there are any messages with the Echo flag, we initialize the buffer size