-
Notifications
You must be signed in to change notification settings - Fork 36
/
ipsec.go
151 lines (134 loc) · 4.52 KB
/
ipsec.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
// Code generated by vpplink DO NOT EDIT.
// Copyright (C) 2019 Cisco Systems Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package generated
import (
"fmt"
"io"
types "github.com/calico-vpp/vpplink/api/v0"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/interface_types"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/ipsec"
"github.com/projectcalico/vpp-dataplane/v3/vpplink/generated/bindings/ipsec_types"
)
func (v *Vpp) GetIPsecTunnelProtection(tunnelInterface uint32) (protections []types.IPsecTunnelProtection, err error) {
client := ipsec.NewServiceClient(v.conn)
stream, err := client.IpsecTunnelProtectDump(v.ctx, &ipsec.IpsecTunnelProtectDump{
SwIfIndex: interface_types.InterfaceIndex(tunnelInterface),
})
if err != nil {
return nil, fmt.Errorf("error listing tunnel interface %v protections: %w", tunnelInterface, err)
}
for {
response, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("error listing tunnel interface %v protections: %w", tunnelInterface, err)
}
protections = append(protections, types.IPsecTunnelProtection{
SwIfIndex: uint32(response.Tun.SwIfIndex),
NextHop: response.Tun.Nh.ToIP(),
OutSAIndex: response.Tun.SaOut,
InSAIndices: response.Tun.SaIn,
})
}
return protections, nil
}
func (v *Vpp) addDelIpsecSA(sa *types.IPSecSA, isAdd bool) error {
client := ipsec.NewServiceClient(v.conn)
request := &ipsec.IpsecSadEntryAddDelV3{
IsAdd: isAdd,
Entry: ipsec_types.IpsecSadEntryV3{
SadID: sa.SAId,
Spi: sa.Spi,
Protocol: ipsec_types.IPSEC_API_PROTO_ESP,
CryptoAlgorithm: ipsec_types.IPSEC_API_CRYPTO_ALG_AES_CTR_128,
CryptoKey: getVPPKey(sa.CryptoKey),
Salt: sa.Salt,
IntegrityKey: getVPPKey(sa.IntegrityKey),
IntegrityAlgorithm: ipsec_types.IPSEC_API_INTEG_ALG_SHA1_96,
Flags: toVppSaFlags(sa.Flags),
UDPSrcPort: uint16(sa.SrcPort),
UDPDstPort: uint16(sa.DstPort),
},
}
if sa.Tunnel != nil {
request.Entry.Tunnel = toVppTunnel(*sa.Tunnel)
}
_, err := client.IpsecSadEntryAddDelV3(v.ctx, request)
if err != nil {
return err
}
return nil
}
func (v *Vpp) AddIpsecSA(sa *types.IPSecSA) error {
if err := v.addDelIpsecSA(sa, true); err != nil {
return fmt.Errorf("failed to add IPSec SA: %w", err)
}
return nil
}
func (v *Vpp) DelIpsecSA(sa *types.IPSecSA) error {
if err := v.addDelIpsecSA(sa, false); err != nil {
return fmt.Errorf("failed to delete IPSec SA: %w", err)
}
return nil
}
func (v *Vpp) AddIpsecSAProtect(swIfIndex, saIn, saOut uint32) error {
client := ipsec.NewServiceClient(v.conn)
_, err := client.IpsecTunnelProtectUpdate(v.ctx, &ipsec.IpsecTunnelProtectUpdate{
Tunnel: ipsec.IpsecTunnelProtect{
SwIfIndex: interface_types.InterfaceIndex(swIfIndex),
SaOut: saOut,
SaIn: []uint32{saIn},
},
})
if err != nil {
return fmt.Errorf("failed to add IPSec Tunnel Protect: %w", err)
}
return nil
}
func (v *Vpp) DelIpsecSAProtect(swIfIndex uint32) error {
client := ipsec.NewServiceClient(v.conn)
_, err := client.IpsecTunnelProtectDel(v.ctx, &ipsec.IpsecTunnelProtectDel{
SwIfIndex: interface_types.InterfaceIndex(swIfIndex),
})
if err != nil {
return fmt.Errorf("failed to delete IPSec Tunnel Protect: %w", err)
}
return nil
}
func (v *Vpp) AddIpsecInterface() (uint32, error) {
client := ipsec.NewServiceClient(v.conn)
response, err := client.IpsecItfCreate(v.ctx, &ipsec.IpsecItfCreate{
Itf: ipsec.IpsecItf{
UserInstance: ^uint32(0),
},
})
if err != nil {
return InvalidSwIfIndex, fmt.Errorf("failed to add IPSec interface: %w", err)
}
return uint32(response.SwIfIndex), nil
}
func (v *Vpp) DelIpsecInterface(swIfIndex uint32) error {
client := ipsec.NewServiceClient(v.conn)
_, err := client.IpsecItfDelete(v.ctx, &ipsec.IpsecItfDelete{
SwIfIndex: interface_types.InterfaceIndex(swIfIndex),
})
if err != nil {
return fmt.Errorf("failed to delete IPSec interface: %w", err)
}
return nil
}