Skip to content

Commit 72a8cd7

Browse files
kyrtapzaboch
authored andcommitted
fix: set ifindex in RTM_GETADDR to avoid dumping all addresses
When a link is provided, AddrList was doing a full NLM_F_DUMP of all addresses and filtering in the client side. On systems with many interfaces this is wasteful and prone to NLM_F_DUMP_INTR errors. Set ifa_index in the request message so the kernel can do the filtering. Kernel-side filtering requires NETLINK_GET_STRICT_CHK to be enabled on the handle (Handle.SetStrictCheck). Without it, the kernel ignores the index field and the existing userspace filter serves as fallback. Signed-off-by: Patryk Diak <pdiak@redhat.com>
1 parent ddba687 commit 72a8cd7

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

addr_linux.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,20 @@ func AddrList(link Link, family int) ([]Addr, error) {
203203
func (h *Handle) AddrList(link Link, family int) ([]Addr, error) {
204204
req := h.newNetlinkRequest(unix.RTM_GETADDR, unix.NLM_F_DUMP)
205205
msg := nl.NewIfAddrmsg(family)
206-
req.AddData(msg)
207-
208-
msgs, executeErr := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWADDR)
209-
if executeErr != nil && !errors.Is(executeErr, ErrDumpInterrupted) {
210-
return nil, executeErr
211-
}
212206

213207
indexFilter := 0
214208
if link != nil {
215209
base := link.Attrs()
216210
h.ensureIndex(base)
217211
indexFilter = base.Index
212+
msg.Index = uint32(indexFilter)
213+
}
214+
215+
req.AddData(msg)
216+
217+
msgs, executeErr := req.Execute(unix.NETLINK_ROUTE, unix.RTM_NEWADDR)
218+
if executeErr != nil && !errors.Is(executeErr, ErrDumpInterrupted) {
219+
return nil, executeErr
218220
}
219221

220222
var res []Addr

0 commit comments

Comments
 (0)