forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xfrm_state_test.go
248 lines (218 loc) · 5.39 KB
/
xfrm_state_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package netlink
import (
"bytes"
"encoding/hex"
"net"
"testing"
)
func TestXfrmStateAddGetDel(t *testing.T) {
for _, s := range []*XfrmState{getBaseState(), getAeadState()} {
testXfrmStateAddGetDel(t, s)
}
}
func testXfrmStateAddGetDel(t *testing.T, state *XfrmState) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
if err := XfrmStateAdd(state); err != nil {
t.Fatal(err)
}
states, err := XfrmStateList(FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(states) != 1 {
t.Fatal("State not added properly")
}
if !compareStates(state, &states[0]) {
t.Fatalf("unexpected states returned")
}
// Get specific state
sa, err := XfrmStateGet(state)
if err != nil {
t.Fatal(err)
}
if !compareStates(state, sa) {
t.Fatalf("unexpected state returned")
}
if err = XfrmStateDel(state); err != nil {
t.Fatal(err)
}
states, err = XfrmStateList(FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(states) != 0 {
t.Fatal("State not removed properly")
}
if _, err := XfrmStateGet(state); err == nil {
t.Fatalf("Unexpected success")
}
}
func TestXfrmStateFlush(t *testing.T) {
setUpNetlinkTest(t)()
state1 := getBaseState()
state2 := getBaseState()
state2.Src = net.ParseIP("127.1.0.1")
state2.Dst = net.ParseIP("127.1.0.2")
state2.Proto = XFRM_PROTO_AH
state2.Mode = XFRM_MODE_TUNNEL
state2.Spi = 20
state2.Mark = nil
state2.Crypt = nil
if err := XfrmStateAdd(state1); err != nil {
t.Fatal(err)
}
if err := XfrmStateAdd(state2); err != nil {
t.Fatal(err)
}
// flushing proto for which no state is present should return silently
if err := XfrmStateFlush(XFRM_PROTO_COMP); err != nil {
t.Fatal(err)
}
if err := XfrmStateFlush(XFRM_PROTO_AH); err != nil {
t.Fatal(err)
}
if _, err := XfrmStateGet(state2); err == nil {
t.Fatalf("Unexpected success")
}
if err := XfrmStateAdd(state2); err != nil {
t.Fatal(err)
}
if err := XfrmStateFlush(0); err != nil {
t.Fatal(err)
}
states, err := XfrmStateList(FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(states) != 0 {
t.Fatal("State not flushed properly")
}
}
func TestXfrmStateUpdateLimits(t *testing.T) {
setUpNetlinkTest(t)()
// Program state with limits
state := getBaseState()
state.Limits.TimeHard = 3600
state.Limits.TimeSoft = 60
state.Limits.PacketHard = 1000
state.Limits.PacketSoft = 50
state.Limits.ByteHard = 1000000
state.Limits.ByteSoft = 50000
state.Limits.TimeUseHard = 3000
state.Limits.TimeUseSoft = 1500
if err := XfrmStateAdd(state); err != nil {
t.Fatal(err)
}
// Verify limits
s, err := XfrmStateGet(state)
if err != nil {
t.Fatal(err)
}
if !compareLimits(state, s) {
t.Fatalf("Incorrect time hard/soft retrieved: %s", s.Print(true))
}
// Update limits
state.Limits.TimeHard = 1800
state.Limits.TimeSoft = 30
state.Limits.PacketHard = 500
state.Limits.PacketSoft = 25
state.Limits.ByteHard = 500000
state.Limits.ByteSoft = 25000
state.Limits.TimeUseHard = 2000
state.Limits.TimeUseSoft = 1000
if err := XfrmStateUpdate(state); err != nil {
t.Fatal(err)
}
// Verify new limits
s, err = XfrmStateGet(state)
if err != nil {
t.Fatal(err)
}
if s.Limits.TimeHard != 1800 || s.Limits.TimeSoft != 30 {
t.Fatalf("Incorrect time hard retrieved: (%d, %d)", s.Limits.TimeHard, s.Limits.TimeSoft)
}
}
func getBaseState() *XfrmState {
return &XfrmState{
// Force 4 byte notation for the IPv4 addresses
Src: net.ParseIP("127.0.0.1").To4(),
Dst: net.ParseIP("127.0.0.2").To4(),
Proto: XFRM_PROTO_ESP,
Mode: XFRM_MODE_TUNNEL,
Spi: 1,
Auth: &XfrmStateAlgo{
Name: "hmac(sha256)",
Key: []byte("abcdefghijklmnopqrstuvwzyzABCDEF"),
},
Crypt: &XfrmStateAlgo{
Name: "cbc(aes)",
Key: []byte("abcdefghijklmnopqrstuvwzyzABCDEF"),
},
Mark: &XfrmMark{
Value: 0x12340000,
Mask: 0xffff0000,
},
}
}
func getAeadState() *XfrmState {
// 128 key bits + 32 salt bits
k, _ := hex.DecodeString("d0562776bf0e75830ba3f7f8eb6c09b555aa1177")
return &XfrmState{
// Leave IPv4 addresses in Ipv4 in IPv6 notation
Src: net.ParseIP("192.168.1.1"),
Dst: net.ParseIP("192.168.2.2"),
Proto: XFRM_PROTO_ESP,
Mode: XFRM_MODE_TUNNEL,
Spi: 2,
Aead: &XfrmStateAlgo{
Name: "rfc4106(gcm(aes))",
Key: k,
ICVLen: 64,
},
}
}
func compareStates(a, b *XfrmState) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return a.Src.Equal(b.Src) && a.Dst.Equal(b.Dst) &&
a.Mode == b.Mode && a.Spi == b.Spi && a.Proto == b.Proto &&
compareAlgo(a.Auth, b.Auth) &&
compareAlgo(a.Crypt, b.Crypt) &&
compareAlgo(a.Aead, b.Aead) &&
compareMarks(a.Mark, b.Mark)
}
func compareLimits(a, b *XfrmState) bool {
return a.Limits.TimeHard == b.Limits.TimeHard &&
a.Limits.TimeSoft == b.Limits.TimeSoft &&
a.Limits.PacketHard == b.Limits.PacketHard &&
a.Limits.PacketSoft == b.Limits.PacketSoft &&
a.Limits.ByteHard == b.Limits.ByteHard &&
a.Limits.ByteSoft == b.Limits.ByteSoft &&
a.Limits.TimeUseHard == b.Limits.TimeUseHard &&
a.Limits.TimeUseSoft == b.Limits.TimeUseSoft
}
func compareAlgo(a, b *XfrmStateAlgo) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return a.Name == b.Name && bytes.Equal(a.Key, b.Key) &&
(a.TruncateLen == 0 || a.TruncateLen == b.TruncateLen) &&
(a.ICVLen == 0 || a.ICVLen == b.ICVLen)
}
func compareMarks(a, b *XfrmMark) bool {
if a == b {
return true
}
if a == nil || b == nil {
return false
}
return a.Value == b.Value && a.Mask == b.Mask
}