-
Notifications
You must be signed in to change notification settings - Fork 11
/
nfrule_dynamic.go
94 lines (87 loc) · 2.68 KB
/
nfrule_dynamic.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
package nftableslib
import (
"fmt"
"github.com/google/nftables"
"github.com/google/nftables/binaryutil"
"github.com/google/nftables/expr"
"golang.org/x/sys/unix"
)
func getExprForDynamic(l3proto nftables.TableFamily, dynamic *Dynamic) ([]expr.Any, error) {
// If dynamic does not carry a populated Set or Map, return error
if dynamic.SetRef == nil {
return nil, fmt.Errorf("reference to set or map cannot be nil")
}
var l3OffsetSrc, l3OffsetDst, l3AddrLen /*, l4ProtoOffset*/ uint32
l4OffsetSrc := uint32(0)
l4OffsetDst := uint32(2)
re := []expr.Any{}
switch l3proto {
case nftables.TableFamilyIPv4:
l3OffsetSrc = 12
l3OffsetDst = 16
l3AddrLen = 4
// l4ProtoOffset = 9
case nftables.TableFamilyIPv6:
l3OffsetSrc = 8
l3OffsetDst = 24
l3AddrLen = 16
// l4ProtoOffset = 6
default:
return nil, fmt.Errorf("unsupported table family %d", l3proto)
}
switch dynamic.Match {
case MatchTypeL3Src:
re = append(re, &expr.Payload{
DestRegister: 1,
Base: expr.PayloadBaseNetworkHeader,
Offset: l3OffsetSrc, // Offset ip address in network header
Len: uint32(l3AddrLen), // length bytes for ip address
})
case MatchTypeL3Dst:
re = append(re, &expr.Payload{
DestRegister: 1,
Base: expr.PayloadBaseNetworkHeader,
Offset: l3OffsetDst, // Offset ip address in network header
Len: uint32(l3AddrLen), // length bytes for ip address
})
case MatchTypeL4Src:
re = append(re, &expr.Payload{
DestRegister: 1,
Base: expr.PayloadBaseTransportHeader,
Offset: l4OffsetSrc, // Offset for a transport protocol header
Len: 2, // 2 bytes for port
})
case MatchTypeL4Dst:
re = append(re, &expr.Payload{
DestRegister: 1,
Base: expr.PayloadBaseTransportHeader,
Offset: l4OffsetDst, // Offset for a transport protocol header
Len: 2, // 2 bytes for port
})
default:
return nil, fmt.Errorf("unsupported matching criteria %+v", dynamic.Match)
}
if len(re) == 0 {
return nil, fmt.Errorf("no valid matching criteria was found")
}
re = append(re, &expr.Immediate{
// Value of register must match to the value of SrcRegData
Register: 2,
Data: binaryutil.BigEndian.PutUint32(dynamic.Key),
})
de := &expr.Dynset{
SrcRegKey: 1,
// Value of SrcRegData must match to the value of expr.Immediate's Register
SrcRegData: 2,
Operation: dynamic.Op,
SetID: dynamic.SetRef.ID,
SetName: dynamic.SetRef.Name,
Invert: dynamic.Invert,
}
// Entry timeout only makes sense only if Operation is Update
if dynamic.Op == unix.NFT_DYNSET_OP_UPDATE {
de.Timeout = dynamic.Timeout
}
re = append(re, de)
return re, nil
}