-
Notifications
You must be signed in to change notification settings - Fork 51
/
ipsets.go
378 lines (301 loc) · 9.47 KB
/
ipsets.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package ipsetmanager
import (
"encoding/base64"
"io"
"net"
"sync"
ipsetpackage "github.com/aporeto-inc/go-ipset/ipset"
"github.com/spaolacci/murmur3"
provider "go.aporeto.io/trireme-lib/controller/pkg/aclprovider"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
)
const (
//IPv6DefaultIP is the default ip of v6
IPv6DefaultIP = "::/0"
//IPv4DefaultIP is the default ip for v4
IPv4DefaultIP = "0.0.0.0/0"
//IPsetV4 version for ipv4
IPsetV4 = iota
//IPsetV6 version for ipv6
IPsetV6
)
//ACLManager interface is used by supervisor. This interface provides the supervisor to
//create ipsets corresponding to service ID.
type ACLManager interface {
AddToIPset(set provider.Ipset, data string) error
DelFromIPset(set provider.Ipset, data string) error
RegisterExternalNets(contextID string, extnets policy.IPRuleList) error
DestroyUnusedIPsets()
RemoveExternalNets(contextID string)
GetIPsets(extnets policy.IPRuleList, ipver int) []string
UpdateIPsets([]string, string)
}
type ipsetInfo struct {
contextIDs map[string]bool
name string
addresses map[string]bool
}
type handler struct {
serviceIDtoIPset map[string]*ipsetInfo
contextIDtoServiceIDs map[string]map[string]bool
ipset provider.IpsetProvider
ipsetPrefix string
ipFilter func(net.IP) bool
ipsetParams *ipsetpackage.Params
toDestroy []string
}
type managerType struct {
ipv4Handler *handler
ipv6Handler *handler
sync.RWMutex
}
const (
ipv4String = "v4-"
ipv6String = "v6-"
)
//CreateIPsetManager creates the handle with Interface ACLManager
func CreateIPsetManager(ipsetv4 provider.IpsetProvider, ipsetv6 provider.IpsetProvider) ACLManager {
return &managerType{
ipv4Handler: &handler{
serviceIDtoIPset: map[string]*ipsetInfo{},
contextIDtoServiceIDs: map[string]map[string]bool{},
ipset: ipsetv4,
ipsetPrefix: ipv4String,
ipFilter: func(ip net.IP) bool {
return (ip.To4() != nil)
},
ipsetParams: &ipsetpackage.Params{},
},
ipv6Handler: &handler{
serviceIDtoIPset: map[string]*ipsetInfo{},
contextIDtoServiceIDs: map[string]map[string]bool{},
ipset: ipsetv6,
ipsetPrefix: ipv6String,
ipFilter: func(ip net.IP) bool {
return (ip.To4() == nil)
},
ipsetParams: &ipsetpackage.Params{HashFamily: "inet6"},
},
}
}
func hashServiceID(serviceID string) string {
hash := murmur3.New64()
if _, err := io.WriteString(hash, serviceID); err != nil {
return ""
}
return base64.URLEncoding.EncodeToString(hash.Sum(nil))
}
// AddToIPset is called with the ipset provider and the ip address to be added
func (m *managerType) AddToIPset(set provider.Ipset, data string) error {
// ipset can not program this rule
if data == IPv4DefaultIP {
if err := m.AddToIPset(set, "0.0.0.0/1"); err != nil {
return err
}
return m.AddToIPset(set, "128.0.0.0/1")
}
// ipset can not program this rule
if data == IPv6DefaultIP {
if err := m.AddToIPset(set, "::/1"); err != nil {
return err
}
return m.AddToIPset(set, "8000::/1")
}
return set.Add(data, 0)
}
// DelFromIPset is called with the ipset set provider and the ip to be removed from ipset
func (m *managerType) DelFromIPset(set provider.Ipset, data string) error {
if data == IPv4DefaultIP {
if err := m.DelFromIPset(set, "0.0.0.0/1"); err != nil {
return err
}
return m.DelFromIPset(set, "128.0.0.0/1")
}
if data == IPv6DefaultIP {
if err := m.DelFromIPset(set, "::/1"); err != nil {
return err
}
return m.DelFromIPset(set, "8000::/1")
}
return set.Del(data)
}
func (m *managerType) synchronizeIPsinIpset(ipHandler *handler, ipsetInfo *ipsetInfo, addresses []string) {
newips := map[string]bool{}
ipsetHandler := ipHandler.ipset.GetIpset(ipsetInfo.name)
for _, address := range addresses {
netIP := net.ParseIP(address)
if netIP == nil {
netIP, _, _ = net.ParseCIDR(address)
}
if !ipHandler.ipFilter(netIP) {
continue
}
newips[address] = true
if _, ok := ipsetInfo.addresses[address]; !ok {
if err := m.AddToIPset(ipsetHandler, address); err != nil {
zap.L().Error("Error adding IPs to ipset", zap.String("ipset", ipsetInfo.name), zap.String("address", address))
}
}
delete(ipsetInfo.addresses, address)
}
// Remove the old entries
for address, val := range ipsetInfo.addresses {
if val {
if err := m.DelFromIPset(ipsetHandler, address); err != nil {
zap.L().Error("Error removing IPs from ipset", zap.String("ipset", ipsetInfo.name), zap.String("address", address))
}
}
}
ipsetInfo.addresses = newips
}
func createIPset(ipHandler *handler, serviceID string) (*ipsetInfo, error) {
ipsetName := "TRI-" + ipHandler.ipsetPrefix + "ext-" + hashServiceID(serviceID)
_, err := ipHandler.ipset.NewIpset(ipsetName, "hash:net", ipHandler.ipsetParams)
if err != nil {
return nil, err
}
ipset := &ipsetInfo{contextIDs: map[string]bool{}, name: ipsetName, addresses: map[string]bool{}}
ipHandler.serviceIDtoIPset[serviceID] = ipset
return ipset, nil
}
func deleteServiceID(ipHandler *handler, serviceID string) {
ipsetInfo := ipHandler.serviceIDtoIPset[serviceID]
ipHandler.toDestroy = append(ipHandler.toDestroy, ipsetInfo.name)
delete(ipHandler.serviceIDtoIPset, serviceID)
}
func reduceReferenceFromServiceID(ipHandler *handler, contextID string, serviceID string) {
var ipset *ipsetInfo
if ipset = ipHandler.serviceIDtoIPset[serviceID]; ipset == nil {
zap.L().Error("Could not find ipset corresponding to serviceID", zap.String("serviceID", serviceID))
return
}
delete(ipset.contextIDs, contextID)
// there are no references from any pu. safe to destroy now
if len(ipset.contextIDs) == 0 {
deleteServiceID(ipHandler, serviceID)
}
}
// RegisterExternalNets registers the contextID and the corresponding serviceIDs
func (m *managerType) RegisterExternalNets(contextID string, extnets policy.IPRuleList) error {
m.Lock()
defer m.Unlock()
processExtnets := func(ipHandler *handler) error {
for _, extnet := range extnets {
var ipset *ipsetInfo
serviceID := extnet.Policy.ServiceID
if ipset = ipHandler.serviceIDtoIPset[serviceID]; ipset == nil {
var err error
if ipset, err = createIPset(ipHandler, serviceID); err != nil {
return err
}
}
m.synchronizeIPsinIpset(ipHandler, ipset, extnet.Addresses)
// have a backreference from serviceID to contextID
ipset.contextIDs[contextID] = true
}
return nil
}
processOlderExtnets := func(ipHandler *handler) {
newExtnets := map[string]bool{}
for _, extnet := range extnets {
serviceID := extnet.Policy.ServiceID
newExtnets[serviceID] = true
m, ok := ipHandler.contextIDtoServiceIDs[contextID]
if ok && m[serviceID] {
delete(m, serviceID)
}
}
for serviceID := range ipHandler.contextIDtoServiceIDs[contextID] {
reduceReferenceFromServiceID(ipHandler, contextID, serviceID)
}
ipHandler.contextIDtoServiceIDs[contextID] = newExtnets
}
if err := processExtnets(m.ipv4Handler); err != nil {
return err
}
if err := processExtnets(m.ipv6Handler); err != nil {
return err
}
processOlderExtnets(m.ipv4Handler)
processOlderExtnets(m.ipv6Handler)
return nil
}
// DestroyUnusedIPsets destroys the unused ipsets.
func (m *managerType) DestroyUnusedIPsets() {
m.Lock()
defer m.Unlock()
destroy := func(ipHandler *handler) {
for _, ipsetName := range ipHandler.toDestroy {
ipsetHandler := ipHandler.ipset.GetIpset(ipsetName)
if err := ipsetHandler.Destroy(); err != nil {
zap.L().Warn("Failed to destroy ipset", zap.String("ipset", ipsetName), zap.Error(err))
}
}
ipHandler.toDestroy = nil
}
destroy(m.ipv4Handler)
destroy(m.ipv6Handler)
}
// RemoveExternalNets is called when the contextID is being unsupervised such that all the external nets can be deleted.
func (m *managerType) RemoveExternalNets(contextID string) {
m.Lock()
process := func(ipHandler *handler) {
m, ok := ipHandler.contextIDtoServiceIDs[contextID]
if ok {
for serviceID := range m {
reduceReferenceFromServiceID(ipHandler, contextID, serviceID)
}
}
delete(ipHandler.contextIDtoServiceIDs, contextID)
}
process(m.ipv4Handler)
process(m.ipv6Handler)
m.Unlock()
m.DestroyUnusedIPsets()
}
// GetIPsets returns the ipset names corresponding to the serviceIDs.
func (m *managerType) GetIPsets(extnets policy.IPRuleList, ipver int) []string {
m.Lock()
defer m.Unlock()
var ipHandler *handler
if ipver == IPsetV4 {
ipHandler = m.ipv4Handler
} else {
ipHandler = m.ipv6Handler
}
var ipsets []string
for _, extnet := range extnets {
serviceID := extnet.Policy.ServiceID
ipsetInfo, ok := ipHandler.serviceIDtoIPset[serviceID]
if ok {
ipsets = append(ipsets, ipsetInfo.name)
}
}
return ipsets
}
// UpdateIPsets updates the ip addresses in the ipsets corresponding to the serviceID
func (m *managerType) UpdateIPsets(addresses []string, serviceID string) {
m.Lock()
defer m.Unlock()
process := func(ipHandler *handler) {
for _, address := range addresses {
netIP := net.ParseIP(address)
if netIP == nil {
netIP, _, _ = net.ParseCIDR(address)
}
if !ipHandler.ipFilter(netIP) {
continue
}
if ipset := ipHandler.serviceIDtoIPset[serviceID]; ipset != nil {
ipsetHandler := ipHandler.ipset.GetIpset(ipset.name)
if err := m.AddToIPset(ipsetHandler, address); err != nil {
zap.L().Error("Error adding IPs to ipset", zap.String("ipset", ipset.name), zap.String("address", address))
}
ipset.addresses[address] = true
}
}
}
process(m.ipv4Handler)
process(m.ipv6Handler)
}