-
Notifications
You must be signed in to change notification settings - Fork 11
/
nfrule_l3.go
168 lines (147 loc) · 3.88 KB
/
nfrule_l3.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
package nftableslib
import (
"fmt"
"math/rand"
"github.com/google/nftables"
"github.com/google/nftables/expr"
)
func createL3(l3proto nftables.TableFamily, rule *Rule) ([]expr.Any, []*nfSet, error) {
re := []expr.Any{}
e := []expr.Any{}
sets := make([]*nfSet, 0)
var set []*nfSet
var err error
// Processing non-nil keys defined in L3 portion of a rule
if rule.L3.Version != nil {
if e, _, err = processVersion(*rule.L3.Version, rule.L3.RelOp); err != nil {
return nil, nil, err
}
re = append(re, e...)
}
if rule.L3.Protocol != nil {
if e, _, err = processProtocol(l3proto, *rule.L3.Protocol, rule.L3.RelOp); err != nil {
return nil, nil, err
}
re = append(re, e...)
}
if rule.L3.Src != nil {
if e, set, err = processIPAddr(l3proto, rule.L3.Src, true, rule.L3.Src.RelOp); err != nil {
return nil, nil, err
}
if set != nil {
sets = append(sets, set...)
}
re = append(re, e...)
}
if rule.L3.Dst != nil {
if e, set, err = processIPAddr(l3proto, rule.L3.Dst, false, rule.L3.Dst.RelOp); err != nil {
return nil, nil, err
}
if set != nil {
sets = append(sets, set...)
}
re = append(re, e...)
}
if rule.L3.Counter != nil {
re = append(re, getExprForCounter()...)
}
return re, sets, nil
}
func processAddrList(l3proto nftables.TableFamily, offset uint32, list []*IPAddr,
op Operator) ([]expr.Any, *nfSet, error) {
if len(list) == 1 {
// Special case when a single IP is provided in the list
re, err := getExprForSingleIP(l3proto, offset, list[0], op)
if err != nil {
return nil, nil, err
}
return re, nil, nil
}
nfset := &nfSet{}
set := &nftables.Set{
Anonymous: false,
Constant: true,
Name: getSetName(),
ID: uint32(rand.Intn(0xffff)),
}
var se []nftables.SetElement
se = buildElementRanges(list)
set.Interval = true
if len(se) == 0 {
return nil, nil, fmt.Errorf("unknown nftables.TableFamily %#02x", l3proto)
}
nfset.set = set
nfset.elements = se
re, err := getExprForListIP(l3proto, set, offset, op)
if err != nil {
return nil, nil, err
}
return re, nfset, nil
}
func processAddrRange(l3proto nftables.TableFamily, offset uint32, rng [2]*IPAddr, op Operator) ([]expr.Any, *nfSet, error) {
re, err := getExprForRangeIP(l3proto, offset, rng, op)
if err != nil {
return nil, nil, err
}
return re, nil, nil
}
func processVersion(version byte, op Operator) ([]expr.Any, *nfSet, error) {
re, err := getExprForIPVersion(version, op)
if err != nil {
return nil, nil, err
}
return re, nil, nil
}
func processProtocol(l3proto nftables.TableFamily, proto uint32, op Operator) ([]expr.Any, *nfSet, error) {
re, err := getExprForProtocol(l3proto, proto, op)
if err != nil {
return nil, nil, err
}
return re, nil, nil
}
func processIPAddr(l3proto nftables.TableFamily, addrs *IPAddrSpec, src bool, op Operator) ([]expr.Any, []*nfSet, error) {
var addrOffset uint32
var keyType nftables.SetDatatype
var set *nfSet
var err error
sets := make([]*nfSet, 0)
e := []expr.Any{}
re := []expr.Any{}
switch l3proto {
case nftables.TableFamilyIPv4:
if src {
addrOffset = 12
} else {
addrOffset = 16
}
keyType = nftables.TypeIPAddr
case nftables.TableFamilyIPv6:
if src {
addrOffset = 8
} else {
addrOffset = 24
}
keyType = nftables.TypeIP6Addr
}
// There are three sources for addresses; List, Range and Set/Map/Vmap
switch {
case addrs.List != nil:
if e, set, err = processAddrList(l3proto, addrOffset, addrs.List, op); err != nil {
return nil, nil, err
}
case addrs.Range[0] != nil && addrs.Range[1] != nil:
if e, set, err = processAddrRange(l3proto, addrOffset, addrs.Range, op); err != nil {
return nil, nil, err
}
case addrs.SetRef != nil:
if e, err = getExprForAddrSet(l3proto, addrOffset, addrs.SetRef, op); err != nil {
return nil, nil, err
}
}
if set != nil {
set.set.KeyType = keyType
sets = append(sets, set)
}
re = append(re, e...)
return re, sets, nil
}