|
1 | 1 | package netlink
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + |
4 | 7 | "github.com/vishvananda/netlink/nl"
|
5 | 8 | "golang.org/x/sys/unix"
|
6 | 9 | )
|
7 | 10 |
|
| 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 | + |
8 | 102 | func selFromPolicy(sel *nl.XfrmSelector, policy *XfrmPolicy) {
|
9 | 103 | sel.Family = uint16(nl.FAMILY_V4)
|
10 | 104 | if policy.Dst != nil {
|
|
0 commit comments