Skip to content

Commit 5ecd901

Browse files
jrfastabjoestringer
authored andcommitted
netlink: xfrm, add optional field to XfrmPolicyTmpl
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 <[email protected]> Signed-off-by: Joe Stringer <[email protected]> Signed-off-by: John Fastabend <[email protected]>
1 parent dc14dc4 commit 5ecd901

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

xfrm_policy.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ func (a PolicyAction) String() string {
5858
// policy. These rules are matched with XfrmState to determine encryption
5959
// and authentication algorithms.
6060
type XfrmPolicyTmpl struct {
61-
Dst net.IP
62-
Src net.IP
63-
Proto Proto
64-
Mode Mode
65-
Spi int
66-
Reqid int
61+
Dst net.IP
62+
Src net.IP
63+
Proto Proto
64+
Mode Mode
65+
Spi int
66+
Reqid int
67+
Optional int
6768
}
6869

6970
func (t XfrmPolicyTmpl) String() string {

xfrm_policy_linux.go

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (h *Handle) xfrmPolicyAddOrUpdate(policy *XfrmPolicy, nlProto int) error {
7979
userTmpl.XfrmId.Spi = nl.Swap32(uint32(tmpl.Spi))
8080
userTmpl.Mode = uint8(tmpl.Mode)
8181
userTmpl.Reqid = uint32(tmpl.Reqid)
82+
userTmpl.Optional = uint8(tmpl.Optional)
8283
userTmpl.Aalgos = ^uint32(0)
8384
userTmpl.Ealgos = ^uint32(0)
8485
userTmpl.Calgos = ^uint32(0)
@@ -247,6 +248,7 @@ func parseXfrmPolicy(m []byte, family int) (*XfrmPolicy, error) {
247248
resTmpl.Mode = Mode(tmpl.Mode)
248249
resTmpl.Spi = int(nl.Swap32(tmpl.XfrmId.Spi))
249250
resTmpl.Reqid = int(tmpl.Reqid)
251+
resTmpl.Optional = int(tmpl.Optional)
250252
policy.Tmpls = append(policy.Tmpls, resTmpl)
251253
}
252254
case nl.XFRMA_MARK:

xfrm_policy_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,31 @@ func TestXfrmPolicyWithIfid(t *testing.T) {
190190
}
191191
}
192192

193+
func TestXfrmPolicyWithOptional(t *testing.T) {
194+
minKernelRequired(t, 4, 19)
195+
defer setUpNetlinkTest(t)()
196+
197+
pol := getPolicy()
198+
pol.Tmpls[0].Optional = 1
199+
200+
if err := XfrmPolicyAdd(pol); err != nil {
201+
t.Fatal(err)
202+
}
203+
policies, err := XfrmPolicyList(FAMILY_ALL)
204+
if err != nil {
205+
t.Fatal(err)
206+
}
207+
if len(policies) != 1 {
208+
t.Fatalf("unexpected number of policies: %d", len(policies))
209+
}
210+
if !comparePolicies(pol, &policies[0]) {
211+
t.Fatalf("unexpected policy returned.\nExpected: %v.\nGot %v", pol, policies[0])
212+
}
213+
if err = XfrmPolicyDel(&policies[0]); err != nil {
214+
t.Fatal(err)
215+
}
216+
}
217+
193218
func comparePolicies(a, b *XfrmPolicy) bool {
194219
if a == b {
195220
return true
@@ -212,7 +237,8 @@ func compareTemplates(a, b []XfrmPolicyTmpl) bool {
212237
for i, ta := range a {
213238
tb := b[i]
214239
if !ta.Dst.Equal(tb.Dst) || !ta.Src.Equal(tb.Src) || ta.Spi != tb.Spi ||
215-
ta.Mode != tb.Mode || ta.Reqid != tb.Reqid || ta.Proto != tb.Proto {
240+
ta.Mode != tb.Mode || ta.Reqid != tb.Reqid || ta.Proto != tb.Proto ||
241+
ta.Optional != tb.Optional {
216242
return false
217243
}
218244
}

0 commit comments

Comments
 (0)