-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrules_immediate.go
499 lines (382 loc) · 14.6 KB
/
rules_immediate.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
package main
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/cilium/ebpf"
)
type NewOps struct {
Action NewOpsAction
Rule Rule
}
type NewIpMapOps struct {
Action NewOpsAction
Priority uint32
AddrArr []Addr
}
type NewPortMapOps struct {
Action NewOpsAction
Priority uint32
PortArr []uint16
}
type NewProtoMapOps struct {
Action NewOpsAction
Priority uint32
Protos []ProtoMapKey
}
var (
ruleActionMapMutex sync.Mutex
newOpsBuffer = make(chan NewOps, NEW_OPS_BUFFER_SIZE)
)
func updateIPMap(wgWorkerPtr *sync.WaitGroup, newOpsBufferForIP chan NewIpMapOps, bpfMapForIP *ebpf.Map, specialCidrMapRuleArr map[SpecialCidr][]uint32, name string) {
zlog.Debug(name, "🍄 start 🍄")
for newIpMapOps := range newOpsBufferForIP {
b := time.Now()
if NEW_OPS_ACTION_ADD == newIpMapOps.Action {
for _, addr := range newIpMapOps.AddrArr {
specialCidrMapRuleArrWithUpdate := make(map[SpecialCidr][]uint32)
aSpecialCidr := addr.CidrSpecial
newCidrRuleNoArr := []uint32{newIpMapOps.Priority}
for bSpecialCidr, ruleArr := range specialCidrMapRuleArr {
compareRet := compareCIDR(&aSpecialCidr, &bSpecialCidr)
if CIDR_EQUAL == compareRet || CIDR_CONTAIN == compareRet {
// 新项 cidr 与 遍历项 cidr 相同 || 新项 cidr 比 遍历项 cidr 大
specialCidrMapRuleArr[bSpecialCidr] = append(ruleArr, newIpMapOps.Priority)
specialCidrMapRuleArrWithUpdate[bSpecialCidr] = specialCidrMapRuleArr[bSpecialCidr]
} else if CIDR_INCLUDED == compareRet {
// 新项 cidr 比 遍历项 cidr 小
newCidrRuleNoArr = append(newCidrRuleNoArr, ruleArr...)
removeDupRuleNo(&newCidrRuleNoArr)
}
// CIDR_NO_CROSS 新项 cidr 与 遍历项 cidr 无交叉; 啥也不做
}
if _, ok := specialCidrMapRuleArr[aSpecialCidr]; !ok {
specialCidrMapRuleArr[aSpecialCidr] = newCidrRuleNoArr
specialCidrMapRuleArrWithUpdate[aSpecialCidr] = newCidrRuleNoArr
}
// 下发配置
for specialCidr, ruleArr := range specialCidrMapRuleArrWithUpdate {
var value RuleBitmapArrV4
for ruleInx := 0; ruleInx < len(ruleArr); ruleInx++ {
setBitmapBit(&value, ruleArr[ruleInx])
}
key := getLpmKey(&specialCidr)
if err := bpfMapForIP.Put(key, &value); err != nil {
zlog.Error(err.Error(), "; bpfMapForIP Put error")
}
}
}
zlog.Debugf("🐙 after add; %s cidrMapRuleArr: %d", name, len(specialCidrMapRuleArr))
} else if NEW_OPS_ACTION_DEL == newIpMapOps.Action {
for _, addr := range newIpMapOps.AddrArr {
specialCidrMapRuleArrWithUpdate := make(map[SpecialCidr][]uint32)
aSpecialCidr := addr.CidrSpecial
for bSpecialCidr, ruleArr := range specialCidrMapRuleArr {
compareRet := compareCIDR(&aSpecialCidr, &bSpecialCidr)
if CIDR_EQUAL == compareRet || CIDR_CONTAIN == compareRet {
// 新项 cidr 与 遍历项 cidr 相同 || 新项 cidr 比 遍历项 cidr 大
for ruleInx := 0; ruleInx < len(ruleArr); ruleInx++ {
if ruleArr[ruleInx] == newIpMapOps.Priority {
ruleArr = append(ruleArr[:ruleInx], ruleArr[ruleInx+1:]...)
break
}
}
if ipMapKeyCanDel(&bSpecialCidr, name) {
delete(specialCidrMapRuleArr, bSpecialCidr)
specialCidrMapRuleArrWithUpdate[bSpecialCidr] = ruleArr[:0]
} else {
specialCidrMapRuleArr[bSpecialCidr] = ruleArr
specialCidrMapRuleArrWithUpdate[bSpecialCidr] = ruleArr
}
}
// CIDR_NO_CROSS 新项 cidr 与 遍历项 cidr 无交叉; 啥也不做
// CIDR_INCLUDED 新项 cidr 比 遍历项 cidr 小; 啥也不做
}
// 下发配置
for specialCidr, rulePriorityArr := range specialCidrMapRuleArrWithUpdate {
key := getLpmKey(&specialCidr)
if len(rulePriorityArr) == 0 {
// del
if err := bpfMapForIP.Delete(key); err != nil {
zlog.Error(err.Error(), "; bpfMapForIP Delete error")
}
} else {
// update
var value RuleBitmapArrV4
for ruleInx := 0; ruleInx < len(rulePriorityArr); ruleInx++ {
setBitmapBit(&value, rulePriorityArr[ruleInx])
}
if err := bpfMapForIP.Put(key, &value); err != nil {
zlog.Error(err.Error(), "; bpfMapForIP Put error")
}
}
}
}
zlog.Debugf("🐙 after del; %s cidrMapRuleArr: %d", name, len(specialCidrMapRuleArr))
}
zlog.Debugf("🍉 name: %s. Cost=%+v.", name, time.Since(b))
(*wgWorkerPtr).Done()
}
zlog.Debug(name, "🍊 exit 🍊")
}
func updatePortMap(wgWorkerPtr *sync.WaitGroup, newOpsBufferForPort chan NewPortMapOps, bpfMapForPort *ebpf.Map, commonPortRulePtr *RuleBitmapArrV4, specifiedPortRule map[uint16][]uint32, name string) {
zlog.Debug(name, "🍁 start 🍁")
var portMapKey uint16
var portMapValue RuleBitmapArrV4
for newPortMapOps := range newOpsBufferForPort {
b := time.Now()
if NEW_OPS_ACTION_ADD == newPortMapOps.Action {
if len(newPortMapOps.PortArr) == 0 {
setBitmapBit(commonPortRulePtr, newPortMapOps.Priority)
// 下发配置
for port := PORT_MIN; port <= PORT_MAX; port++ {
portMapKey = htons(uint16(port))
if specifiedPortRuleArr, ok := specifiedPortRule[uint16(port)]; !ok {
if err := bpfMapForPort.Put(portMapKey, commonPortRulePtr); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
} else {
portMapValue = *commonPortRulePtr
for i := 0; i < len(specifiedPortRuleArr); i++ {
setBitmapBit(&portMapValue, specifiedPortRuleArr[i])
}
if err := bpfMapForPort.Put(portMapKey, &portMapValue); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
}
}
} else {
for portInx := 0; portInx < len(newPortMapOps.PortArr); portInx++ {
specifiedPortRule[newPortMapOps.PortArr[portInx]] = append(specifiedPortRule[newPortMapOps.PortArr[portInx]], newPortMapOps.Priority)
portMapKey = htons(newPortMapOps.PortArr[portInx])
portMapValue = *commonPortRulePtr
for ruleInx := 0; ruleInx < len(specifiedPortRule[newPortMapOps.PortArr[portInx]]); ruleInx++ {
setBitmapBit(&portMapValue, specifiedPortRule[newPortMapOps.PortArr[portInx]][ruleInx])
}
if err := bpfMapForPort.Put(portMapKey, &portMapValue); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
}
}
} else if NEW_OPS_ACTION_DEL == newPortMapOps.Action {
if len(newPortMapOps.PortArr) == 0 {
// 生成配置
resetBitmapBit(commonPortRulePtr, newPortMapOps.Priority)
// 下发配置
for inx := PORT_MIN; inx <= PORT_MAX; inx++ {
portMapKey = htons(uint16(inx))
if specifiedPortRuleArr, ok := specifiedPortRule[uint16(inx)]; !ok {
if err := bpfMapForPort.Put(portMapKey, commonPortRulePtr); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
} else {
portMapValue = *commonPortRulePtr
for ruleInx := 0; ruleInx < len(specifiedPortRuleArr); ruleInx++ {
setBitmapBit(&portMapValue, specifiedPortRuleArr[ruleInx])
}
if err := bpfMapForPort.Put(portMapKey, &portMapValue); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
}
}
} else {
for portInx := 0; portInx < len(newPortMapOps.PortArr); portInx++ {
for ruleInx := 0; ruleInx < len(specifiedPortRule[newPortMapOps.PortArr[portInx]]); ruleInx++ {
if specifiedPortRule[newPortMapOps.PortArr[portInx]][ruleInx] == newPortMapOps.Priority {
specifiedPortRule[newPortMapOps.PortArr[portInx]] = append(specifiedPortRule[newPortMapOps.PortArr[portInx]][:ruleInx], specifiedPortRule[newPortMapOps.PortArr[portInx]][ruleInx+1:]...)
break
}
}
portMapKey = htons(newPortMapOps.PortArr[portInx])
if len(specifiedPortRule[newPortMapOps.PortArr[portInx]]) == 0 {
delete(specifiedPortRule, newPortMapOps.PortArr[portInx])
if err := bpfMapForPort.Put(portMapKey, commonPortRulePtr); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
} else {
portMapValue = *commonPortRulePtr
for ruleInx := 0; ruleInx < len(specifiedPortRule[newPortMapOps.PortArr[portInx]]); ruleInx++ {
setBitmapBit(&portMapValue, specifiedPortRule[newPortMapOps.PortArr[portInx]][ruleInx])
}
if err := bpfMapForPort.Put(portMapKey, &portMapValue); err != nil {
zlog.Error(err.Error(), "; bpfMapForPort Put error")
}
}
}
}
}
zlog.Debugf("🍉 name: %s. Cost=%+v.", name, time.Since(b))
(*wgWorkerPtr).Done()
}
zlog.Debug(name, "🍁 exit 🍁")
}
func updateProtoMap(wgWorkerPtr *sync.WaitGroup, newOpsBufferForProto chan NewProtoMapOps, bpfMapForProto *ebpf.Map, name string) {
zlog.Debug(name, "🍟 start 🍟")
// icmp: 1; tcp: 6; udp: 17;
for newProtoMapOps := range newOpsBufferForProto {
var value RuleBitmapArrV4
for i := 0; i < len(newProtoMapOps.Protos); i++ {
if err := bpfMapForProto.Lookup(newProtoMapOps.Protos[i], &value); nil != err {
// 此分支不应该出现,除非出错
zlog.Error(err.Error(), "; bpfMapForProto Lookup error")
} else {
// key 已在初始化时添加
if NEW_OPS_ACTION_ADD == newProtoMapOps.Action {
setBitmapBit(&value, newProtoMapOps.Priority)
} else {
// NEW_OPS_ACTION_DEL
resetBitmapBit(&value, newProtoMapOps.Priority)
}
zlog.Debug("🌺 ", name, "; value[0]: ", value[0], "; value[1]: ", value[1])
if err := bpfMapForProto.Put(newProtoMapOps.Protos[i], &value); err != nil {
zlog.Error(err.Error(), "; bpfMapForProto Put error")
}
}
}
(*wgWorkerPtr).Done()
}
zlog.Debug(name, "🍟 exit 🍟")
}
func getProtos(protos uint8) []ProtoMapKey {
protoSlice := make([]ProtoMapKey, 0, 3)
if (protos & PROTO_TCP_BIT) > 0 {
protoSlice = append(protoSlice, ProtoMapKey(PROTO_TCP))
}
if (protos & PROTO_UDP_BIT) > 0 {
protoSlice = append(protoSlice, ProtoMapKey(PROTO_UDP))
}
if (protos & PROTO_ICMP_BIT) > 0 {
protoSlice = append(protoSlice, ProtoMapKey(PROTO_ICMP))
}
return protoSlice
}
func updateRuleActionMap(newOps *NewOps, bpfMapForRuleAction *ebpf.Map, name string) {
var ruleActionKey RuleActionKey
genRuleActionKey(uint64(newOps.Rule.Priority), &ruleActionKey)
ruleActionArr := make([]RuleAction, NumCPU)
genRuleActionValue(uint64(newOps.Rule.Strategy), &ruleActionArr)
if NEW_OPS_ACTION_ADD == newOps.Action {
if err := bpfMapForRuleAction.Put(ruleActionKey, ruleActionArr); err != nil {
zlog.Error("bpfMapForRuleAction.Put: ", err)
}
} else {
// NEW_OPS_ACTION_DEL
ruleActionMapMutex.Lock()
defer ruleActionMapMutex.Unlock()
if err := bpfMapForRuleAction.Delete(ruleActionKey); err != nil {
zlog.Error("bpfMapForRuleAction.Delete: ", err)
}
}
}
func adjustRuleList(newOps *NewOps) (string, error) {
if NEW_OPS_ACTION_ADD == newOps.Action {
// 添加到首部
ruleList = append(RuleArr{newOps.Rule}, ruleList...)
} else {
notFindTarget := true
for ruleInx := 0; ruleInx < len(ruleList); ruleInx++ {
if newOps.Rule.Priority == ruleList[ruleInx].Priority {
newOps.Rule = ruleList[ruleInx]
ruleList = append(ruleList[:ruleInx], ruleList[ruleInx+1:]...)
notFindTarget = false
break
}
}
if notFindTarget {
return "", errors.New("not find specified rule: " + fmt.Sprintf("%d", newOps.Rule.Priority))
}
}
if str, err := json.MarshalIndent(ruleList, "", " "); err == nil {
return string(str), nil
} else {
return "", err
}
}
func loadImmediateRules(name string) {
newOpsBufferForIPSrc, newOpsBufferForIPDst := make(chan NewIpMapOps, 1), make(chan NewIpMapOps, 1)
newOpsBufferForPortSrc, newOpsBufferForPortDst := make(chan NewPortMapOps, 1), make(chan NewPortMapOps, 1)
newOpsBufferForProto := make(chan NewProtoMapOps, 1)
wgGlobal.Add(1)
var immediateRuleWg, saveFileWg sync.WaitGroup
// 启动 子协程
go updateIPMap(&immediateRuleWg, newOpsBufferForIPSrc, objs.SrcV4, srcSpecialCidrMapInheritRuleArr, MAP_TYPE_IP_SRC)
go updateIPMap(&immediateRuleWg, newOpsBufferForIPDst, objs.DstV4, dstSpecialCidrMapInheritRuleArr, MAP_TYPE_IP_DST)
go updatePortMap(&immediateRuleWg, newOpsBufferForPortSrc, objs.SportV4, &commonSrcPortRule, specifiedSrcPortRule, MAP_TYPE_PORT_SRC)
go updatePortMap(&immediateRuleWg, newOpsBufferForPortDst, objs.DportV4, &commonDstPortRule, specifiedDstPortRule, MAP_TYPE_PORT_DST)
go updateProtoMap(&immediateRuleWg, newOpsBufferForProto, objs.ProtoV4, MAP_TYPE_PROTO)
go saveFile(&saveFileWg)
for newOps := range newOpsBuffer {
zlog.Debug("---- rcv newOps")
if NEW_OPS_ACTION_ADD != newOps.Action && NEW_OPS_ACTION_DEL != newOps.Action {
zlog.Errorf("unknown ops rule: %d; action: %d;", newOps.Rule.Priority, newOps.Action)
continue
}
// 调整 rules 列表
if rulesStr, err := adjustRuleList(&newOps); err != nil {
continue
} else if err == nil && rulesStr != "" {
saveFileWg.Add(1)
bufferForJsonFile <- rulesStr
}
// ------------ 下发规则 开始
b := time.Now()
if NEW_OPS_ACTION_DEL == newOps.Action {
updateRuleActionMap(&newOps, objs.RuleActionV4, MAP_TYPE_RULE_ACTION)
}
immediateRuleWg.Add(2)
newOpsBufferForIPSrc <- NewIpMapOps{
Action: newOps.Action,
Priority: newOps.Rule.Priority,
AddrArr: newOps.Rule.AddrSrcArr,
}
newOpsBufferForIPDst <- NewIpMapOps{
Action: newOps.Action,
Priority: newOps.Rule.Priority,
AddrArr: newOps.Rule.AddrDstArr,
}
if !onlyContainICMP(newOps.Rule.Protos) {
immediateRuleWg.Add(2)
// (若只有 ICMP,则不走此分支)
newOpsBufferForPortSrc <- NewPortMapOps{
Action: newOps.Action,
Priority: newOps.Rule.Priority,
PortArr: newOps.Rule.PortSrcArr,
}
newOpsBufferForPortDst <- NewPortMapOps{
Action: newOps.Action,
Priority: newOps.Rule.Priority,
PortArr: newOps.Rule.PortDstArr,
}
zlog.Debug("不包含 ICMP 或者 不只ICMP")
}
immediateRuleWg.Add(1)
newOpsBufferForProto <- NewProtoMapOps{
Action: newOps.Action,
Priority: newOps.Rule.Priority,
Protos: getProtos(newOps.Rule.Protos),
}
immediateRuleWg.Wait()
if NEW_OPS_ACTION_ADD == newOps.Action {
updateRuleActionMap(&newOps, objs.RuleActionV4, MAP_TYPE_RULE_ACTION)
zlog.Info("add rule: ", newOps.Rule.Priority, " 👌")
} else if NEW_OPS_ACTION_DEL == newOps.Action {
zlog.Info("del rule: ", newOps.Rule.Priority, " 👌")
} else {
zlog.Info("unknown ops rule: ", newOps.Rule.Priority, " 👌")
}
zlog.Debugf("🍉 name: %s. Cost=%+v.", "total", time.Since(b))
// ------------ 下发规则 结束
}
// close 子任务 channel
close(bufferForJsonFile)
close(newOpsBufferForIPSrc)
close(newOpsBufferForIPDst)
close(newOpsBufferForPortSrc)
close(newOpsBufferForPortDst)
close(newOpsBufferForProto)
saveFileWg.Wait()
wgGlobal.Done()
zlog.Info("takeApartRuleAndAssignWork-----exit")
}