/
zones.go
258 lines (210 loc) · 5.38 KB
/
zones.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
/*
* Copyright (c) 2022 NetLOX 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 loxinet
import (
"errors"
"sync"
tk "github.com/loxilb-io/loxilib"
)
// This file implements self-contained network security zones
// Currently we can have upto MAX_ZONES such zones
// error codes for zone
const (
ZoneBaseErr = iota - 6000
ZoneExistsErr
ZoneNotExistErr
ZoneNumberErr
)
// constant to define maximum number of zones
const (
MaximumZones = 256
)
// Zone - zone info
type Zone struct {
Name string
ZoneNum int
Ports *PortsH
Vlans *VlansH
L2 *L2H
Nh *NeighH
Rt *RtH
L3 *L3H
Rules *RuleH
Sess *SessH
Pols *PolH
Mirrs *MirrH
Mtx sync.RWMutex
}
// ZoneH - Zone context handler
type ZoneH struct {
ZoneMap map[string]*Zone
ZoneBrs map[string]*Zone
ZonePorts map[string]*Zone
ZoneMark *tk.Counter
}
// ZoneInit - routine to initialize zone context handler
func ZoneInit() *ZoneH {
var zn = new(ZoneH)
zn.ZoneMap = make(map[string]*Zone)
zn.ZoneBrs = make(map[string]*Zone)
zn.ZonePorts = make(map[string]*Zone)
zn.ZoneMark = tk.NewCounter(1, MaximumZones)
return zn
}
// ZoneAdd - routine to add a zone
func (z *ZoneH) ZoneAdd(name string) (int, error) {
var err error
zone := z.ZoneMap[name]
if zone != nil {
return ZoneExistsErr, errors.New("existing zone")
}
zone = new(Zone)
zNum, err := z.ZoneMark.GetCounter()
if err != nil {
return ZoneNumberErr, errors.New("zone number err")
}
zone.ZoneNum = int(zNum)
zone.Name = name
zone.Ports = PortInit()
zone.Vlans = VlanInit(zone)
zone.L2 = L2Init(zone)
zone.Nh = NeighInit(zone)
zone.Rt = RtInit(zone)
zone.L3 = L3Init(zone)
zone.Rules = RulesInit(zone)
zone.Sess = SessInit(zone)
zone.Pols = PolInit(zone)
zone.Mirrs = MirrInit(zone)
z.ZoneMap[name] = zone
return 0, nil
}
// Zonefind - routine to find a zone
func (z *ZoneH) Zonefind(name string) (*Zone, int) {
zone := z.ZoneMap[name]
if zone == nil {
return nil, -1
}
return zone, zone.ZoneNum
}
// ZoneDelete - routine to delete a zone
func (z *ZoneH) ZoneDelete(name string) (int, error) {
zone := z.ZoneMap[name]
if zone == nil {
return ZoneNotExistErr, errors.New("no such zone")
}
zone.Rules.RuleDestructAll()
zone.Mirrs.MirrDestructAll()
zone.Pols.PolDestructAll()
zone.Rt.RtDestructAll()
zone.Nh.NeighDestructAll()
zone.L2.L2DestructAll()
zone.Vlans.VlanDestructAll()
zone.Ports.PortDestructAll()
delete(z.ZoneMap, name)
return 0, nil
}
// ZoneBrAdd - Routine to add a bridge in a zone
func (z *ZoneH) ZoneBrAdd(name string, zns string) (int, error) {
zone := z.ZoneBrs[name]
if zone != nil {
if zone.Name == zns {
return 0, nil
}
return ZoneExistsErr, errors.New("zone exists")
}
zone, _ = z.Zonefind(zns)
if zone == nil {
return ZoneNotExistErr, errors.New("zone is not set")
}
z.ZoneBrs[name] = zone
return 0, nil
}
// ZoneBrDelete - routine to delete a bridge from the zone
func (z *ZoneH) ZoneBrDelete(name string) (int, error) {
zone := z.ZoneBrs[name]
if zone == nil {
return ZoneNotExistErr, errors.New("zone is not set")
}
delete(z.ZoneBrs, name)
return 0, nil
}
// ZonePortIsValid - routine to check if the port belongs to a zone
func (z *ZoneH) ZonePortIsValid(name string, zns string) (int, error) {
zone := z.ZonePorts[name]
if zone == nil {
return 0, nil
}
if zone.Name == zns {
return 0, nil
}
return ZoneExistsErr, errors.New("zone exists")
}
// GetPortZone - routine to identify the zone of a port
func (z *ZoneH) GetPortZone(port string) *Zone {
return z.ZonePorts[port]
}
// ZonePortAdd - routine to add a port to a zone
func (z *ZoneH) ZonePortAdd(name string, zns string) (int, error) {
zone := z.ZonePorts[name]
if zone != nil {
if zone.Name == zns {
return 0, nil
}
return ZoneExistsErr, errors.New("zone exists")
}
zone, _ = z.Zonefind(zns)
if zone == nil {
return ZoneNotExistErr, errors.New("zone is not set")
}
z.ZonePorts[name] = zone
return 0, nil
}
// ZonePortDelete - routine to delete a port from a zone
func (z *ZoneH) ZonePortDelete(name string) (int, error) {
zone := z.ZonePorts[name]
if zone == nil {
return ZoneNotExistErr, errors.New("zone is not set")
}
delete(z.ZonePorts, name)
return 0, nil
}
// ZoneTicker - This ticker routine takes care of all house-keeping operations
// for all instances of security zones. This is called from loxiNetTicker
func (z *ZoneH) ZoneTicker() {
for _, zone := range z.ZoneMap {
mh.mtx.Lock()
zone.L2.FdbsTicker()
zone.Nh.NeighsTicker()
mh.mtx.Unlock()
/* NOTE - No need to hold lock for an extended period */
mh.mtx.RLock()
zone.Rules.RulesTicker()
mh.mtx.RUnlock()
//mh.mtx.RLock()
//zone.Vlans.VlansTicker()
//mh.mtx.RUnlock()
//mh.mtx.RLock()
//zone.Rt.RoutesTicker()
//mh.mtx.RUnlock()
mh.mtx.RLock()
zone.Sess.SessionTicker()
mh.mtx.RUnlock()
mh.mtx.RLock()
zone.Pols.PolTicker()
zone.Mirrs.MirrTicker()
mh.mtx.RUnlock()
}
}