forked from contiv/ofnet
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ofnetPolicy.go
499 lines (426 loc) · 13.5 KB
/
ofnetPolicy.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/***
Copyright 2014 Cisco Systems Inc. All rights reserved.
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 ofnet
import (
"errors"
"net"
"net/rpc"
"reflect"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/contiv/ofnet/ofctrl"
)
// This file has security policy rule implementation
const TCP_FLAG_ACK = 0x10
const TCP_FLAG_SYN = 0x2
// PolicyRule has info about single rule
type PolicyRule struct {
Rule *OfnetPolicyRule // rule definition
flow *ofctrl.Flow // Flow associated with the flow
}
// PolicyAgent is an instance of a policy agent
type PolicyAgent struct {
agent *OfnetAgent // Pointer back to ofnet agent that owns this
ofSwitch *ofctrl.OFSwitch // openflow switch we are talking to
dstGrpTable *ofctrl.Table // dest group lookup table
policyTable *ofctrl.Table // Policy rule lookup table
nextTable *ofctrl.Table // Next table to goto for accepted packets
Rules map[string]*PolicyRule // rules database
dstGrpFlow map[string]*ofctrl.Flow // FLow entries for dst group lookup
mutex sync.RWMutex
}
// NewPolicyMgr Creates a new policy manager
func NewPolicyAgent(agent *OfnetAgent, rpcServ *rpc.Server) *PolicyAgent {
policyAgent := new(PolicyAgent)
// initialize
policyAgent.agent = agent
policyAgent.Rules = make(map[string]*PolicyRule)
policyAgent.dstGrpFlow = make(map[string]*ofctrl.Flow)
// Register for Master add/remove events
rpcServ.Register(policyAgent)
// done
return policyAgent
}
// Handle switch connected notification
func (self *PolicyAgent) SwitchConnected(sw *ofctrl.OFSwitch) {
// Keep a reference to the switch
self.ofSwitch = sw
log.Infof("Switch connected(policyAgent).")
}
// Handle switch disconnected notification
func (self *PolicyAgent) SwitchDisconnected(sw *ofctrl.OFSwitch) {
// FIXME: ??
}
// Metadata Format
// 6 3 3 1 1 0 0
// 3 1 0 6 5 1 0
// +-------------+-+---------------+---------------+-+
// | ....U |U| SrcGrp | DstGrp |V|
// +-------------+-+---------------+---------------+-+
//
// U: Unused
// SrcGrp: Source endpoint group
// DstGrp: Destination endpoint group
// V: Received on VTEP Port. Dont flood back to VTEP ports.
//
// DstGroupMetadata returns metadata for dst group
func DstGroupMetadata(groupId int) (uint64, uint64) {
metadata := uint64(groupId) << 1
metadataMask := uint64(0xfffe)
metadata = metadata & metadataMask
return metadata, metadataMask
}
// SrcGroupMetadata returns metadata for src group
func SrcGroupMetadata(groupId int) (uint64, uint64) {
metadata := uint64(groupId) << 16
metadataMask := uint64(0x7fff0000)
metadata = metadata & metadataMask
return metadata, metadataMask
}
// ruleIsSame check if two rules are identical
func ruleIsSame(r1, r2 *OfnetPolicyRule) bool {
return reflect.DeepEqual(*r1, *r2)
}
// AddEndpoint adds an endpoint to dst group lookup
func (self *PolicyAgent) AddEndpoint(endpoint *OfnetEndpoint) error {
self.mutex.RLock()
if self.dstGrpFlow[endpoint.EndpointID] != nil {
// FIXME: handle this as Update
log.Warnf("DstGroup for endpoint %+v already exists", endpoint)
self.mutex.RUnlock()
return nil
}
self.mutex.RUnlock()
log.Infof("Adding dst group entry for endpoint: %+v", endpoint)
vrf := self.agent.getvlanVrf(endpoint.Vlan)
if vrf == nil {
log.Errorf("Error finding vrf for vlan %d", endpoint.Vlan)
return errors.New("Error finding vrf for vlan")
}
log.Infof("Recevied add endpoint for vrf %v", *vrf)
self.agent.vrfMutex.RLock()
vrfid := self.agent.vrfNameIdMap[*vrf]
self.agent.vrfMutex.RUnlock()
vrfMetadata, vrfMetadataMask := Vrfmetadata(*vrfid)
// Install the Dst group lookup flow
dstGrpFlow, err := self.dstGrpTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x0800,
IpDa: &endpoint.IpAddr,
Metadata: &vrfMetadata,
MetadataMask: &vrfMetadataMask,
})
if err != nil {
log.Errorf("Error adding dstGroup flow for %v. Err: %v", endpoint.IpAddr, err)
return err
}
// Format the metadata
metadata, metadataMask := DstGroupMetadata(endpoint.EndpointGroup)
// Set dst GroupId
err = dstGrpFlow.SetMetadata(metadata, metadataMask)
if err != nil {
log.Errorf("Error setting metadata %v for flow {%+v}. Err: %v", metadata, dstGrpFlow, err)
return err
}
// Go to policy Table
err = dstGrpFlow.Next(self.policyTable)
if err != nil {
log.Errorf("Error installing flow {%+v}. Err: %v", dstGrpFlow, err)
return err
}
// save the Flow
self.mutex.Lock()
self.dstGrpFlow[endpoint.EndpointID] = dstGrpFlow
self.mutex.Unlock()
return nil
}
// DelEndpoint deletes an endpoint from dst group lookup
func (self *PolicyAgent) DelEndpoint(endpoint *OfnetEndpoint) error {
// find the dst group flow
self.mutex.RLock()
dstGrp := self.dstGrpFlow[endpoint.EndpointID]
self.mutex.RUnlock()
if dstGrp == nil {
return errors.New("Dst Group not found")
}
// delete the Flow
err := dstGrp.Delete()
if err != nil {
log.Errorf("Error deleting dst group for endpoint: %+v. Err: %v", endpoint, err)
}
// delete the cache
self.mutex.Lock()
delete(self.dstGrpFlow, endpoint.EndpointID)
self.mutex.Unlock()
return nil
}
// AddIpv6Endpoint adds an endpoint to dst group lookup
func (self *PolicyAgent) AddIpv6Endpoint(endpoint *OfnetEndpoint) error {
if endpoint.Ipv6Addr == nil {
log.Warnf("DstGroup for IPv6 endpoint %+v without Ipv6Addr", endpoint)
return nil
}
ipv6EpId := self.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
self.mutex.RLock()
if self.dstGrpFlow[ipv6EpId] != nil {
// FIXME: handle this as Update
log.Warnf("DstGroup for IPv6 endpoint %+v already exists", endpoint)
self.mutex.RUnlock()
return nil
}
self.mutex.RUnlock()
log.Infof("Adding dst group entry for endpoint: %+v", endpoint)
vrf := self.agent.getvlanVrf(endpoint.Vlan)
if vrf == nil {
log.Errorf("Error finding vrf for vlan %d", endpoint.Vlan)
return errors.New("Error finding vrf for vlan")
}
log.Infof("Recevied add endpoint for vrf %v", *vrf)
self.agent.vrfMutex.RLock()
vrfid := self.agent.vrfNameIdMap[*vrf]
self.agent.vrfMutex.RUnlock()
vrfMetadata, vrfMetadataMask := Vrfmetadata(*vrfid)
// Install the Dst group lookup flow
dstGrpFlow, err := self.dstGrpTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MATCH_PRIORITY,
Ethertype: 0x86DD,
Ipv6Da: &endpoint.Ipv6Addr,
Metadata: &vrfMetadata,
MetadataMask: &vrfMetadataMask,
})
if err != nil {
log.Errorf("Error adding dstGroup flow for %v. Err: %v", endpoint.IpAddr, err)
return err
}
// Format the metadata
metadata, metadataMask := DstGroupMetadata(endpoint.EndpointGroup)
// Set dst GroupId
err = dstGrpFlow.SetMetadata(metadata, metadataMask)
if err != nil {
log.Errorf("Error setting metadata %v for flow {%+v}. Err: %v", metadata, dstGrpFlow, err)
return err
}
// Go to policy Table
err = dstGrpFlow.Next(self.policyTable)
if err != nil {
log.Errorf("Error installing flow {%+v}. Err: %v", dstGrpFlow, err)
return err
}
// save the Flow
self.mutex.Lock()
self.dstGrpFlow[ipv6EpId] = dstGrpFlow
self.mutex.Unlock()
return nil
}
// DelIpv6Endpoint deletes an endpoint from dst group lookup
func (self *PolicyAgent) DelIpv6Endpoint(endpoint *OfnetEndpoint) error {
// find the dst group IPv6 flow
ipv6EpId := self.agent.getEndpointIdByIpVlan(endpoint.Ipv6Addr, endpoint.Vlan)
self.mutex.RLock()
dstGrp := self.dstGrpFlow[ipv6EpId]
if dstGrp == nil {
self.mutex.RUnlock()
return errors.New("Dst Group IPv6 Flow not found")
}
self.mutex.RUnlock()
// delete the Flow
err := dstGrp.Delete()
if err != nil {
log.Errorf("Error deleting dst group for IPv6 endpoint: %+v. Err: %v", endpoint, err)
}
// delete the cache
self.mutex.RLock()
delete(self.dstGrpFlow, ipv6EpId)
self.mutex.RUnlock()
return nil
}
// AddRule adds a security rule to policy table
func (self *PolicyAgent) AddRule(rule *OfnetPolicyRule, ret *bool) error {
var ipDa *net.IP = nil
var ipDaMask *net.IP = nil
var ipSa *net.IP = nil
var ipSaMask *net.IP = nil
var md *uint64 = nil
var mdm *uint64 = nil
var flag, flagMask uint16
var flagPtr, flagMaskPtr *uint16
var err error
// make sure switch is connected
if !self.agent.IsSwitchConnected() {
self.agent.WaitForSwitchConnection()
}
// check if we already have the rule
self.mutex.RLock()
if self.Rules[rule.RuleId] != nil {
oldRule := self.Rules[rule.RuleId].Rule
if ruleIsSame(oldRule, rule) {
self.mutex.RUnlock()
return nil
} else {
self.mutex.RUnlock()
log.Errorf("Rule already exists. new rule: {%+v}, old rule: {%+v}", rule, oldRule)
return errors.New("Rule already exists")
}
}
self.mutex.RUnlock()
log.Infof("Received AddRule: %+v", rule)
// Parse dst ip
if rule.DstIpAddr != "" {
ipDa, ipDaMask, err = ParseIPAddrMaskString(rule.DstIpAddr)
if err != nil {
log.Errorf("Error parsing dst ip %s. Err: %v", rule.DstIpAddr, err)
return err
}
}
// parse src ip
if rule.SrcIpAddr != "" {
ipSa, ipSaMask, err = ParseIPAddrMaskString(rule.SrcIpAddr)
if err != nil {
log.Errorf("Error parsing src ip %s. Err: %v", rule.SrcIpAddr, err)
return err
}
}
// parse source/dst endpoint groups
if rule.SrcEndpointGroup != 0 && rule.DstEndpointGroup != 0 {
srcMetadata, srcMetadataMask := SrcGroupMetadata(rule.SrcEndpointGroup)
dstMetadata, dstMetadataMask := DstGroupMetadata(rule.DstEndpointGroup)
metadata := srcMetadata | dstMetadata
metadataMask := srcMetadataMask | dstMetadataMask
md = &metadata
mdm = &metadataMask
} else if rule.SrcEndpointGroup != 0 {
srcMetadata, srcMetadataMask := SrcGroupMetadata(rule.SrcEndpointGroup)
md = &srcMetadata
mdm = &srcMetadataMask
} else if rule.DstEndpointGroup != 0 {
dstMetadata, dstMetadataMask := DstGroupMetadata(rule.DstEndpointGroup)
md = &dstMetadata
mdm = &dstMetadataMask
}
// Setup TCP flags
if rule.IpProtocol == 6 && rule.TcpFlags != "" {
switch rule.TcpFlags {
case "syn":
flag = TCP_FLAG_SYN
flagMask = TCP_FLAG_SYN
case "syn,ack":
flag = TCP_FLAG_ACK | TCP_FLAG_SYN
flagMask = TCP_FLAG_ACK | TCP_FLAG_SYN
case "ack":
flag = TCP_FLAG_ACK
flagMask = TCP_FLAG_ACK
case "syn,!ack":
flag = TCP_FLAG_SYN
flagMask = TCP_FLAG_ACK | TCP_FLAG_SYN
case "!syn,ack":
flag = TCP_FLAG_ACK
flagMask = TCP_FLAG_ACK | TCP_FLAG_SYN
default:
log.Errorf("Unknown TCP flags: %s, in rule: %+v", rule.TcpFlags, rule)
return errors.New("Unknown TCP flag")
}
flagPtr = &flag
flagMaskPtr = &flagMask
}
// Install the rule in policy table
ruleFlow, err := self.policyTable.NewFlow(ofctrl.FlowMatch{
Priority: uint16(FLOW_POLICY_PRIORITY_OFFSET + rule.Priority),
Ethertype: 0x0800,
IpDa: ipDa,
IpDaMask: ipDaMask,
IpSa: ipSa,
IpSaMask: ipSaMask,
IpProto: rule.IpProtocol,
TcpSrcPort: rule.SrcPort,
TcpDstPort: rule.DstPort,
UdpSrcPort: rule.SrcPort,
UdpDstPort: rule.DstPort,
Metadata: md,
MetadataMask: mdm,
TcpFlags: flagPtr,
TcpFlagsMask: flagMaskPtr,
})
if err != nil {
log.Errorf("Error adding flow for rule {%v}. Err: %v", rule, err)
return err
}
// Point it to next table
if rule.Action == "allow" {
err = ruleFlow.Next(self.nextTable)
if err != nil {
log.Errorf("Error installing flow {%+v}. Err: %v", ruleFlow, err)
return err
}
} else if rule.Action == "deny" {
err = ruleFlow.Next(self.ofSwitch.DropAction())
if err != nil {
log.Errorf("Error installing flow {%+v}. Err: %v", ruleFlow, err)
return err
}
} else {
log.Errorf("Unknown action in rule {%+v}", rule)
return errors.New("Unknown action in rule")
}
// save the rule
pRule := PolicyRule{
Rule: rule,
flow: ruleFlow,
}
self.mutex.Lock()
self.Rules[rule.RuleId] = &pRule
self.mutex.Unlock()
return nil
}
// DelRule deletes a security rule from policy table
func (self *PolicyAgent) DelRule(rule *OfnetPolicyRule, ret *bool) error {
log.Infof("Received DelRule: %+v", rule)
// Gte the rule
self.mutex.Lock()
defer self.mutex.Unlock()
cache := self.Rules[rule.RuleId]
if cache == nil {
log.Errorf("Could not find rule: %+v", rule)
return errors.New("rule not found")
}
// Delete the Flow
err := cache.flow.Delete()
if err != nil {
log.Errorf("Error deleting flow: %+v. Err: %v", rule, err)
}
// Delete the rule from cache
delete(self.Rules, rule.RuleId)
return nil
}
// InitTables initializes policy table on the switch
func (self *PolicyAgent) InitTables(nextTblId uint8) error {
sw := self.ofSwitch
nextTbl := sw.GetTable(nextTblId)
if nextTbl == nil {
log.Fatalf("Error getting table id: %d", nextTblId)
}
self.nextTable = nextTbl
// Create all tables
self.dstGrpTable, _ = sw.NewTable(DST_GRP_TBL_ID)
self.policyTable, _ = sw.NewTable(POLICY_TBL_ID)
// Packets that miss dest group lookup still go to policy table
validPktFlow, _ := self.dstGrpTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
validPktFlow.Next(self.policyTable)
// Packets that didnt match any rule go to next table
vlanMissFlow, _ := self.policyTable.NewFlow(ofctrl.FlowMatch{
Priority: FLOW_MISS_PRIORITY,
})
vlanMissFlow.Next(nextTbl)
return nil
}