-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone.go
179 lines (146 loc) · 3.25 KB
/
zone.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
package yafw
import (
"fmt"
"net"
"github.com/google/nftables"
)
type Zone struct {
Name string `json:"name"`
Description string `json:"description"`
set *nftables.Set
oldM map[string]*net.Interface
m map[string]*net.Interface
}
func (z *Zone) element(member *net.Interface) []nftables.SetElement {
ret := []nftables.SetElement{
{
Key: InterfaceName(member.Name),
},
}
return ret
}
func (z *Zone) elements() []nftables.SetElement {
ret := []nftables.SetElement(nil)
for _, member := range z.m {
if member != nil {
ret = append(ret, z.element(member)...)
}
}
return ret
}
func (z *Zone) AddInterface(iface *net.Interface) *Zone {
_, ok := z.m[iface.Name]
if !ok {
z.m[iface.Name] = iface
}
return z
}
func (z *Zone) RemoveInterface(iface *net.Interface) *Zone {
delete(z.m, iface.Name)
return z
}
func (z *Zone) Members() []*net.Interface {
ret := make([]*net.Interface, 0)
for _, member := range z.m {
ret = append(ret, member)
}
return ret
}
type ZoneTable struct {
r *Router
zoneMap map[string]*Zone // zone name to zone data
interfaceMap map[string]string // interface name to zone name
}
func NewZoneTable(r *Router) *ZoneTable {
return &ZoneTable{
r: r,
zoneMap: make(map[string]*Zone),
interfaceMap: make(map[string]string),
}
}
func (t *ZoneTable) Update(zone *Zone) error {
r := t.r
nft := r.nft
if zone.set != nil {
// incremental update
for key, member := range zone.m {
if _, ok := zone.oldM[key]; !ok {
// incremental addition
nft.SetAddElements(zone.set, zone.element(member))
}
}
for key, member := range zone.oldM {
if _, ok := zone.m[key]; !ok {
// incremental deletion
nft.SetDeleteElements(zone.set, zone.element(member))
}
}
} else {
// create a new set
elements := zone.elements()
zone.set = &nftables.Set{
Table: r.table,
Name: fmt.Sprintf("zone-%s", zone.Name),
KeyType: nftables.TypeIFName,
}
err := nft.AddSet(zone.set, elements)
if err != nil {
return err
}
}
if err := r.Update(); err != nil {
return err
}
zone, ok := t.zoneMap[zone.Name]
if !ok {
t.zoneMap[zone.Name] = zone
}
zone.oldM = zone.m
return nil
}
func (t *ZoneTable) FindZone(name string) *Zone {
if zone, ok := t.zoneMap[name]; ok {
return zone
} else {
return nil
}
}
func (r *ZoneTable) All() []*Zone {
ret := []*Zone{}
for _, z := range r.zoneMap {
ret = append(ret, z)
return ret
}
return ret
}
func (t *ZoneTable) AssignInterfaceToZone(iface *net.Interface, zone string) error {
if oldZone, ok := t.interfaceMap[iface.Name]; ok {
if oldZone != zone {
t.zoneMap[oldZone].RemoveInterface(iface)
t.Update(t.zoneMap[oldZone])
}
}
t.zoneMap[zone].AddInterface(iface)
t.interfaceMap[iface.Name] = zone
t.Update(t.zoneMap[zone])
return nil
}
func (t *ZoneTable) DeleteZone(name string) {
if zone, ok := t.zoneMap[name]; ok {
for _, iface := range zone.Members() {
delete(t.interfaceMap, iface.Name)
}
delete(t.zoneMap, name)
}
}
func (t *ZoneTable) AddZone(name string) *Zone {
z := &Zone{
Name: name,
Description: "",
oldM: make(map[string]*net.Interface),
m: make(map[string]*net.Interface),
set: nil,
}
t.zoneMap[name] = z
return z
}