Skip to content

Commit 77df5d3

Browse files
tklauseraboch
authored andcommittedOct 24, 2023
Make xfrm linux-only
The xfrm framework is linux-only. Only implement the respective types for GOOS=linux to avoid dependencies to x/sys/unix on non-linux or non-unix platforms. Provide dummy XfrmPolicy and XfrmState types for the globally defined XfrmPolicy* and XfrmState* functions.
1 parent ccef072 commit 77df5d3

9 files changed

+244
-255
lines changed
 

‎xfrm.go ‎xfrm_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
XFRM_PROTO_ESP Proto = unix.IPPROTO_ESP
1515
XFRM_PROTO_AH Proto = unix.IPPROTO_AH
1616
XFRM_PROTO_HAO Proto = unix.IPPROTO_DSTOPTS
17-
XFRM_PROTO_COMP Proto = 0x6c // NOTE not defined on darwin
17+
XFRM_PROTO_COMP Proto = unix.IPPROTO_COMP
1818
XFRM_PROTO_IPSEC_ANY Proto = unix.IPPROTO_RAW
1919
)
2020

‎xfrm_monitor_test.go ‎xfrm_monitor_linux_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build linux
2-
// +build linux
3-
41
package netlink
52

63
import (

‎xfrm_policy.go

-97
This file was deleted.

‎xfrm_policy_linux.go

+94
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,104 @@
11
package netlink
22

33
import (
4+
"fmt"
5+
"net"
6+
47
"github.com/vishvananda/netlink/nl"
58
"golang.org/x/sys/unix"
69
)
710

11+
// Dir is an enum representing an ipsec template direction.
12+
type Dir uint8
13+
14+
const (
15+
XFRM_DIR_IN Dir = iota
16+
XFRM_DIR_OUT
17+
XFRM_DIR_FWD
18+
XFRM_SOCKET_IN
19+
XFRM_SOCKET_OUT
20+
XFRM_SOCKET_FWD
21+
)
22+
23+
func (d Dir) String() string {
24+
switch d {
25+
case XFRM_DIR_IN:
26+
return "dir in"
27+
case XFRM_DIR_OUT:
28+
return "dir out"
29+
case XFRM_DIR_FWD:
30+
return "dir fwd"
31+
case XFRM_SOCKET_IN:
32+
return "socket in"
33+
case XFRM_SOCKET_OUT:
34+
return "socket out"
35+
case XFRM_SOCKET_FWD:
36+
return "socket fwd"
37+
}
38+
return fmt.Sprintf("socket %d", d-XFRM_SOCKET_IN)
39+
}
40+
41+
// PolicyAction is an enum representing an ipsec policy action.
42+
type PolicyAction uint8
43+
44+
const (
45+
XFRM_POLICY_ALLOW PolicyAction = 0
46+
XFRM_POLICY_BLOCK PolicyAction = 1
47+
)
48+
49+
func (a PolicyAction) String() string {
50+
switch a {
51+
case XFRM_POLICY_ALLOW:
52+
return "allow"
53+
case XFRM_POLICY_BLOCK:
54+
return "block"
55+
default:
56+
return fmt.Sprintf("action %d", a)
57+
}
58+
}
59+
60+
// XfrmPolicyTmpl encapsulates a rule for the base addresses of an ipsec
61+
// policy. These rules are matched with XfrmState to determine encryption
62+
// and authentication algorithms.
63+
type XfrmPolicyTmpl struct {
64+
Dst net.IP
65+
Src net.IP
66+
Proto Proto
67+
Mode Mode
68+
Spi int
69+
Reqid int
70+
Optional int
71+
}
72+
73+
func (t XfrmPolicyTmpl) String() string {
74+
return fmt.Sprintf("{Dst: %v, Src: %v, Proto: %s, Mode: %s, Spi: 0x%x, Reqid: 0x%x}",
75+
t.Dst, t.Src, t.Proto, t.Mode, t.Spi, t.Reqid)
76+
}
77+
78+
// XfrmPolicy represents an ipsec policy. It represents the overlay network
79+
// and has a list of XfrmPolicyTmpls representing the base addresses of
80+
// the policy.
81+
type XfrmPolicy struct {
82+
Dst *net.IPNet
83+
Src *net.IPNet
84+
Proto Proto
85+
DstPort int
86+
SrcPort int
87+
Dir Dir
88+
Priority int
89+
Index int
90+
Action PolicyAction
91+
Ifindex int
92+
Ifid int
93+
Mark *XfrmMark
94+
Tmpls []XfrmPolicyTmpl
95+
}
96+
97+
func (p XfrmPolicy) String() string {
98+
return fmt.Sprintf("{Dst: %v, Src: %v, Proto: %s, DstPort: %d, SrcPort: %d, Dir: %s, Priority: %d, Index: %d, Action: %s, Ifindex: %d, Ifid: %d, Mark: %s, Tmpls: %s}",
99+
p.Dst, p.Src, p.Proto, p.DstPort, p.SrcPort, p.Dir, p.Priority, p.Index, p.Action, p.Ifindex, p.Ifid, p.Mark, p.Tmpls)
100+
}
101+
8102
func selFromPolicy(sel *nl.XfrmSelector, policy *XfrmPolicy) {
9103
sel.Family = uint16(nl.FAMILY_V4)
10104
if policy.Dst != nil {

‎xfrm_policy_test.go ‎xfrm_policy_linux_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build linux
2-
// +build linux
3-
41
package netlink
52

63
import (

‎xfrm_state.go

-148
This file was deleted.

0 commit comments

Comments
 (0)