Skip to content

Commit

Permalink
netlink: xfrm, add optional field to XfrmPolicyTmpl
Browse files Browse the repository at this point in the history
Add optional field in XfrmPolicyTmpl to template code so users can
configure template optional values.

Tested via:

    $ go test -exec sudo . -run XfrmPolicyWithOptional
    ok      github.com/vishvananda/netlink  0.009s

Co-authored-by: Joe Stringer <joe@cilium.io>
Signed-off-by: Joe Stringer <joe@cilium.io>
Signed-off-by: John Fastabend <john.fastabend@gmail.com>
  • Loading branch information
2 people authored and aboch committed Mar 24, 2021
1 parent c21bda4 commit 66fce01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
13 changes: 7 additions & 6 deletions xfrm_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ func (a PolicyAction) String() string {
// policy. These rules are matched with XfrmState to determine encryption
// and authentication algorithms.
type XfrmPolicyTmpl struct {
Dst net.IP
Src net.IP
Proto Proto
Mode Mode
Spi int
Reqid int
Dst net.IP
Src net.IP
Proto Proto
Mode Mode
Spi int
Reqid int
Optional int
}

func (t XfrmPolicyTmpl) String() string {
Expand Down
2 changes: 2 additions & 0 deletions xfrm_policy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (h *Handle) xfrmPolicyAddOrUpdate(policy *XfrmPolicy, nlProto int) error {
userTmpl.XfrmId.Spi = nl.Swap32(uint32(tmpl.Spi))
userTmpl.Mode = uint8(tmpl.Mode)
userTmpl.Reqid = uint32(tmpl.Reqid)
userTmpl.Optional = uint8(tmpl.Optional)
userTmpl.Aalgos = ^uint32(0)
userTmpl.Ealgos = ^uint32(0)
userTmpl.Calgos = ^uint32(0)
Expand Down Expand Up @@ -247,6 +248,7 @@ func parseXfrmPolicy(m []byte, family int) (*XfrmPolicy, error) {
resTmpl.Mode = Mode(tmpl.Mode)
resTmpl.Spi = int(nl.Swap32(tmpl.XfrmId.Spi))
resTmpl.Reqid = int(tmpl.Reqid)
resTmpl.Optional = int(tmpl.Optional)
policy.Tmpls = append(policy.Tmpls, resTmpl)
}
case nl.XFRMA_MARK:
Expand Down
28 changes: 27 additions & 1 deletion xfrm_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@ func TestXfrmPolicyWithIfid(t *testing.T) {
}
}

func TestXfrmPolicyWithOptional(t *testing.T) {
minKernelRequired(t, 4, 19)
defer setUpNetlinkTest(t)()

pol := getPolicy()
pol.Tmpls[0].Optional = 1

if err := XfrmPolicyAdd(pol); err != nil {
t.Fatal(err)
}
policies, err := XfrmPolicyList(FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(policies) != 1 {
t.Fatalf("unexpected number of policies: %d", len(policies))
}
if !comparePolicies(pol, &policies[0]) {
t.Fatalf("unexpected policy returned.\nExpected: %v.\nGot %v", pol, policies[0])
}
if err = XfrmPolicyDel(&policies[0]); err != nil {
t.Fatal(err)
}
}

func comparePolicies(a, b *XfrmPolicy) bool {
if a == b {
return true
Expand All @@ -212,7 +237,8 @@ func compareTemplates(a, b []XfrmPolicyTmpl) bool {
for i, ta := range a {
tb := b[i]
if !ta.Dst.Equal(tb.Dst) || !ta.Src.Equal(tb.Src) || ta.Spi != tb.Spi ||
ta.Mode != tb.Mode || ta.Reqid != tb.Reqid || ta.Proto != tb.Proto {
ta.Mode != tb.Mode || ta.Reqid != tb.Reqid || ta.Proto != tb.Proto ||
ta.Optional != tb.Optional {
return false
}
}
Expand Down

0 comments on commit 66fce01

Please sign in to comment.