2017-02-20 17:34:32 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2016-05-09 16:19:18 +00:00
|
|
|
"bytes"
|
2014-09-01 03:27:34 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-05-12 17:52:20 +00:00
|
|
|
const zeroCIDR = "0.0.0.0/0"
|
|
|
|
|
2016-05-04 05:52:55 +00:00
|
|
|
func TestXfrmPolicyAddUpdateDel(t *testing.T) {
|
2014-09-01 03:27:34 +00:00
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
2016-05-13 23:42:24 +00:00
|
|
|
policy := getPolicy()
|
2016-05-09 23:52:35 +00:00
|
|
|
if err := XfrmPolicyAdd(policy); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
policies, err := XfrmPolicyList(FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(policies) != 1 {
|
|
|
|
t.Fatal("Policy not added properly")
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:52:35 +00:00
|
|
|
if !comparePolicies(policy, &policies[0]) {
|
|
|
|
t.Fatalf("unexpected policy returned.\nExpected: %v.\nGot %v", policy, policies[0])
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:18:51 +00:00
|
|
|
if policies[0].Ifindex != 0 {
|
|
|
|
t.Fatalf("default policy has a non-zero interface index.\nGot %d", policies[0].Ifindex)
|
|
|
|
}
|
|
|
|
|
2019-01-02 22:56:57 +00:00
|
|
|
if policies[0].Ifid != 0 {
|
|
|
|
t.Fatalf("default policy has non-zero if_id.\nGot %d", policies[0].Ifid)
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:18:51 +00:00
|
|
|
if policies[0].Action != XFRM_POLICY_ALLOW {
|
|
|
|
t.Fatalf("default policy has non-allow action.\nGot %s", policies[0].Action)
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:52:35 +00:00
|
|
|
// Look for a specific policy
|
|
|
|
sp, err := XfrmPolicyGet(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !comparePolicies(policy, sp) {
|
|
|
|
t.Fatalf("unexpected policy returned")
|
2016-05-09 16:19:18 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 05:52:55 +00:00
|
|
|
// Modify the policy
|
|
|
|
policy.Priority = 100
|
2016-05-09 23:52:35 +00:00
|
|
|
if err := XfrmPolicyUpdate(policy); err != nil {
|
2016-05-04 05:52:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-09 23:52:35 +00:00
|
|
|
sp, err = XfrmPolicyGet(policy)
|
2016-05-04 05:52:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-09 23:52:35 +00:00
|
|
|
if sp.Priority != 100 {
|
2016-05-04 05:52:55 +00:00
|
|
|
t.Fatalf("failed to modify the policy")
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:52:35 +00:00
|
|
|
if err = XfrmPolicyDel(policy); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err = XfrmPolicyList(FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(policies) != 0 {
|
|
|
|
t.Fatal("Policy not removed properly")
|
|
|
|
}
|
2016-05-12 17:52:20 +00:00
|
|
|
|
|
|
|
// Src and dst are not mandatory field. Creation should succeed
|
|
|
|
policy.Src = nil
|
|
|
|
policy.Dst = nil
|
|
|
|
if err = XfrmPolicyAdd(policy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sp, err = XfrmPolicyGet(policy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !comparePolicies(policy, sp) {
|
|
|
|
t.Fatalf("unexpected policy returned")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = XfrmPolicyDel(policy); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-05-12 20:16:26 +00:00
|
|
|
|
|
|
|
if _, err := XfrmPolicyGet(policy); err == nil {
|
|
|
|
t.Fatalf("Unexpected success")
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2016-05-09 16:19:18 +00:00
|
|
|
|
2016-05-13 23:42:24 +00:00
|
|
|
func TestXfrmPolicyFlush(t *testing.T) {
|
2017-11-28 06:59:57 +00:00
|
|
|
defer setUpNetlinkTest(t)()
|
2016-05-13 23:42:24 +00:00
|
|
|
|
|
|
|
p1 := getPolicy()
|
|
|
|
if err := XfrmPolicyAdd(p1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p1.Dir = XFRM_DIR_IN
|
|
|
|
s := p1.Src
|
|
|
|
p1.Src = p1.Dst
|
|
|
|
p1.Dst = s
|
|
|
|
if err := XfrmPolicyAdd(p1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := XfrmPolicyList(FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(policies) != 2 {
|
|
|
|
t.Fatalf("unexpected number of policies: %d", len(policies))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := XfrmPolicyFlush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err = XfrmPolicyList(FAMILY_ALL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(policies) != 0 {
|
|
|
|
t.Fatalf("unexpected number of policies: %d", len(policies))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:18:51 +00:00
|
|
|
func TestXfrmPolicyBlockWithIfindex(t *testing.T) {
|
|
|
|
defer setUpNetlinkTest(t)()
|
|
|
|
|
|
|
|
pBlock := getPolicy()
|
|
|
|
pBlock.Action = XFRM_POLICY_BLOCK
|
|
|
|
pBlock.Ifindex = 1 // loopback interface
|
|
|
|
if err := XfrmPolicyAdd(pBlock); 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(pBlock, &policies[0]) {
|
|
|
|
t.Fatalf("unexpected policy returned.\nExpected: %v.\nGot %v", pBlock, policies[0])
|
|
|
|
}
|
|
|
|
if err = XfrmPolicyDel(pBlock); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 22:56:57 +00:00
|
|
|
func TestXfrmPolicyWithIfid(t *testing.T) {
|
|
|
|
minKernelRequired(t, 4, 19)
|
|
|
|
defer setUpNetlinkTest(t)()
|
|
|
|
|
|
|
|
pol := getPolicy()
|
|
|
|
pol.Ifid = 54321
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 00:51:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 23:52:35 +00:00
|
|
|
func comparePolicies(a, b *XfrmPolicy) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Do not check Index which is assigned by kernel
|
|
|
|
return a.Dir == b.Dir && a.Priority == b.Priority &&
|
|
|
|
compareIPNet(a.Src, b.Src) && compareIPNet(a.Dst, b.Dst) &&
|
2018-08-10 15:18:51 +00:00
|
|
|
a.Action == b.Action && a.Ifindex == b.Ifindex &&
|
2016-05-09 23:52:35 +00:00
|
|
|
a.Mark.Value == b.Mark.Value && a.Mark.Mask == b.Mark.Mask &&
|
2019-01-02 22:56:57 +00:00
|
|
|
a.Ifid == b.Ifid && compareTemplates(a.Tmpls, b.Tmpls)
|
2016-05-09 23:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func compareTemplates(a, b []XfrmPolicyTmpl) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, ta := range a {
|
|
|
|
tb := b[i]
|
2016-05-25 18:10:56 +00:00
|
|
|
if !ta.Dst.Equal(tb.Dst) || !ta.Src.Equal(tb.Src) || ta.Spi != tb.Spi ||
|
2021-02-23 00:51:33 +00:00
|
|
|
ta.Mode != tb.Mode || ta.Reqid != tb.Reqid || ta.Proto != tb.Proto ||
|
|
|
|
ta.Optional != tb.Optional {
|
2016-05-09 23:52:35 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-09 16:19:18 +00:00
|
|
|
func compareIPNet(a, b *net.IPNet) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
2016-05-12 17:52:20 +00:00
|
|
|
// For unspecified src/dst parseXfrmPolicy would set the zero address cidr
|
|
|
|
if (a == nil && b.String() == zeroCIDR) || (b == nil && a.String() == zeroCIDR) {
|
|
|
|
return true
|
|
|
|
}
|
2016-05-09 16:19:18 +00:00
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.IP.Equal(b.IP) && bytes.Equal(a.Mask, b.Mask)
|
|
|
|
}
|
2016-05-13 23:42:24 +00:00
|
|
|
|
|
|
|
func getPolicy() *XfrmPolicy {
|
|
|
|
src, _ := ParseIPNet("127.1.1.1/32")
|
|
|
|
dst, _ := ParseIPNet("127.1.1.2/32")
|
|
|
|
policy := &XfrmPolicy{
|
|
|
|
Src: src,
|
|
|
|
Dst: dst,
|
|
|
|
Proto: 17,
|
|
|
|
DstPort: 1234,
|
|
|
|
SrcPort: 5678,
|
|
|
|
Dir: XFRM_DIR_OUT,
|
|
|
|
Mark: &XfrmMark{
|
|
|
|
Value: 0xabff22,
|
|
|
|
Mask: 0xffffffff,
|
|
|
|
},
|
|
|
|
Priority: 10,
|
|
|
|
}
|
|
|
|
tmpl := XfrmPolicyTmpl{
|
|
|
|
Src: net.ParseIP("127.0.0.1"),
|
|
|
|
Dst: net.ParseIP("127.0.0.2"),
|
|
|
|
Proto: XFRM_PROTO_ESP,
|
|
|
|
Mode: XFRM_MODE_TUNNEL,
|
2016-05-25 18:10:56 +00:00
|
|
|
Spi: 0xabcdef99,
|
2016-05-13 23:42:24 +00:00
|
|
|
}
|
|
|
|
policy.Tmpls = append(policy.Tmpls, tmpl)
|
|
|
|
return policy
|
|
|
|
}
|