From 796d4ea903f3094afdc7c3f9702505c9f4a9bb38 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 17 Jan 2022 19:36:17 +0100 Subject: [PATCH] Only set XFRMA_IF_ID if not 0 A value of 0 for XFRMA_IF_ID is not valid and will be rejected as of Linux kernel commit torvalds/linux@68ac0f3810e7 ("xfrm: state and policy should fail if XFRMA_IF_ID 0"), which will return EINVAL when setting XFRMA_IF_ID = 0. Thus, only set the XFRMA_IF_ID attribute in case XfrmState.Ifid or XfrmPolicy.Ifid are != 0. Signed-off-by: Tobias Klauser --- xfrm_policy_linux.go | 12 ++++++++---- xfrm_state_linux.go | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/xfrm_policy_linux.go b/xfrm_policy_linux.go index 694bd74..3584968 100644 --- a/xfrm_policy_linux.go +++ b/xfrm_policy_linux.go @@ -93,8 +93,10 @@ func (h *Handle) xfrmPolicyAddOrUpdate(policy *XfrmPolicy, nlProto int) error { req.AddData(out) } - ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(policy.Ifid))) - req.AddData(ifId) + if policy.Ifid != 0 { + ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(policy.Ifid))) + req.AddData(ifId) + } _, err := req.Execute(unix.NETLINK_XFRM, 0) return err @@ -189,8 +191,10 @@ func (h *Handle) xfrmPolicyGetOrDelete(policy *XfrmPolicy, nlProto int) (*XfrmPo req.AddData(out) } - ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(policy.Ifid))) - req.AddData(ifId) + if policy.Ifid != 0 { + ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(policy.Ifid))) + req.AddData(ifId) + } resType := nl.XFRM_MSG_NEWPOLICY if nlProto == nl.XFRM_MSG_DELPOLICY { diff --git a/xfrm_state_linux.go b/xfrm_state_linux.go index 3b37b87..61a2d2d 100644 --- a/xfrm_state_linux.go +++ b/xfrm_state_linux.go @@ -167,8 +167,10 @@ func (h *Handle) xfrmStateAddOrUpdate(state *XfrmState, nlProto int) error { } } - ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(state.Ifid))) - req.AddData(ifId) + if state.Ifid != 0 { + ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(state.Ifid))) + req.AddData(ifId) + } _, err := req.Execute(unix.NETLINK_XFRM, 0) return err @@ -281,8 +283,10 @@ func (h *Handle) xfrmStateGetOrDelete(state *XfrmState, nlProto int) (*XfrmState req.AddData(out) } - ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(state.Ifid))) - req.AddData(ifId) + if state.Ifid != 0 { + ifId := nl.NewRtAttr(nl.XFRMA_IF_ID, nl.Uint32Attr(uint32(state.Ifid))) + req.AddData(ifId) + } resType := nl.XFRM_MSG_NEWSA if nlProto == nl.XFRM_MSG_DELSA {