forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathipmasq.go
More file actions
295 lines (249 loc) · 7.81 KB
/
Copy pathipmasq.go
File metadata and controls
295 lines (249 loc) · 7.81 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipmasq
import (
"encoding/json"
"fmt"
"maps"
"net/netip"
"os"
"path/filepath"
"slices"
"strings"
"github.com/fsnotify/fsnotify"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
)
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "ipmasq")
// The following reserved by RFCs IP addr ranges are used by
// https://github.com/kubernetes-sigs/ip-masq-agent
defaultNonMasqCIDRs = map[string]netip.Prefix{
"10.0.0.0/8": netip.MustParsePrefix("10.0.0.0/8"),
"172.16.0.0/12": netip.MustParsePrefix("172.16.0.0/12"),
"192.168.0.0/16": netip.MustParsePrefix("192.168.0.0/16"),
"100.64.0.0/10": netip.MustParsePrefix("100.64.0.0/10"),
"192.0.0.0/24": netip.MustParsePrefix("192.0.0.0/24"),
"192.0.2.0/24": netip.MustParsePrefix("192.0.2.0/24"),
"192.88.99.0/24": netip.MustParsePrefix("192.88.99.0/24"),
"198.18.0.0/15": netip.MustParsePrefix("198.18.0.0/15"),
"198.51.100.0/24": netip.MustParsePrefix("198.51.100.0/24"),
"203.0.113.0/24": netip.MustParsePrefix("203.0.113.0/24"),
"240.0.0.0/4": netip.MustParsePrefix("240.0.0.0/4"),
}
linkLocalCIDRIPv4Str = "169.254.0.0/16"
linkLocalCIDRIPv4 = netip.MustParsePrefix(linkLocalCIDRIPv4Str)
linkLocalCIDRIPv6Str = "fe80::/10"
linkLocalCIDRIPv6 = netip.MustParsePrefix(linkLocalCIDRIPv6Str)
)
// ipnet is a wrapper type for netip.Prefix to enable de-serialization
// of CIDRs
type Ipnet netip.Prefix
func (c *Ipnet) UnmarshalJSON(json []byte) error {
str := string(json)
if json[0] != '"' {
return fmt.Errorf("Invalid CIDR: %s", str)
}
n, err := parseCIDR(strings.Trim(str, `"`))
if err != nil {
return err
}
*c = Ipnet(n)
return nil
}
func parseCIDR(c string) (netip.Prefix, error) {
n, err := netip.ParsePrefix(c)
if err != nil {
return netip.Prefix{}, fmt.Errorf("Invalid CIDR %q: %w", c, err)
}
return n.Masked(), nil
}
// config represents the ip-masq-agent configuration file encoded as YAML
type config struct {
NonMasqCIDRs []Ipnet `json:"nonMasqueradeCIDRs"`
MasqLinkLocalIPv4 bool `json:"masqLinkLocal"`
MasqLinkLocalIPv6 bool `json:"masqLinkLocalIPv6"`
}
// IPMasqMap is an interface describing methods for manipulating an ipmasq map
type IPMasqMap interface {
Update(cidr netip.Prefix) error
Delete(cidr netip.Prefix) error
Dump() ([]netip.Prefix, error)
}
// IPMasqAgent represents a state of the ip-masq-agent
type IPMasqAgent struct {
lock lock.Mutex
configPath string
masqLinkLocalIPv4 bool
masqLinkLocalIPv6 bool
nonMasqCIDRsFromConfig map[string]netip.Prefix
nonMasqCIDRsInMap map[string]netip.Prefix
ipMasqMap IPMasqMap
watcher *fsnotify.Watcher
stop chan struct{}
handlerFinished chan struct{}
}
func NewIPMasqAgent(configPath string, ipMasqMap IPMasqMap) *IPMasqAgent {
a := &IPMasqAgent{
configPath: configPath,
nonMasqCIDRsFromConfig: map[string]netip.Prefix{},
nonMasqCIDRsInMap: map[string]netip.Prefix{},
ipMasqMap: ipMasqMap,
}
return a
}
// Start starts the ip-masq-agent goroutine which tracks the config file and
// updates the BPF map accordingly.
func (a *IPMasqAgent) Start() error {
a.lock.Lock()
defer a.lock.Unlock()
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to create fsnotify watcher: %w", err)
}
a.watcher = watcher
configDir := filepath.Dir(a.configPath)
// The directory of the config should exist at this time, otherwise
// the watcher will fail to add
if err := a.watcher.Add(configDir); err != nil {
a.watcher.Close()
return fmt.Errorf("failed to add %q dir to fsnotify watcher: %w", configDir, err)
}
if err := a.restore(); err != nil {
log.WithError(err).Warn("Failed to restore")
}
if err := a.update(); err != nil {
log.WithError(err).Warn("Failed to update")
}
a.stop = make(chan struct{})
a.handlerFinished = make(chan struct{})
go func() {
for {
select {
case event := <-a.watcher.Events:
log.Debugf("Received fsnotify event: %+v", event)
switch {
case event.Has(fsnotify.Create),
event.Has(fsnotify.Write),
event.Has(fsnotify.Chmod),
event.Has(fsnotify.Remove),
event.Has(fsnotify.Rename):
if err := a.Update(); err != nil {
log.WithError(err).Warn("Failed to update")
}
default:
log.Warnf("Watcher received unknown event: %s. Ignoring.", event)
}
case err := <-a.watcher.Errors:
log.WithError(err).Warn("Watcher received an error")
case <-a.stop:
log.Info("Stopping ip-masq-agent")
close(a.handlerFinished)
return
}
}
}()
return nil
}
// Stop stops the ip-masq-agent goroutine and the watcher.
func (a *IPMasqAgent) Stop() {
close(a.stop)
<-a.handlerFinished
a.watcher.Close()
}
func (a *IPMasqAgent) Update() error {
a.lock.Lock()
defer a.lock.Unlock()
return a.update()
}
// Update updates the ipmasq BPF map entries with ones from the config file.
func (a *IPMasqAgent) update() error {
isEmpty, err := a.readConfig()
if err != nil {
return err
}
// Set default nonMasq CIDRS if user hasn't specified any
if isEmpty {
for cidrStr, cidr := range defaultNonMasqCIDRs {
a.nonMasqCIDRsFromConfig[cidrStr] = cidr
}
}
if !a.masqLinkLocalIPv4 {
a.nonMasqCIDRsFromConfig[linkLocalCIDRIPv4Str] = linkLocalCIDRIPv4
}
if !a.masqLinkLocalIPv6 {
a.nonMasqCIDRsFromConfig[linkLocalCIDRIPv6Str] = linkLocalCIDRIPv6
}
for cidrStr, cidr := range a.nonMasqCIDRsFromConfig {
if _, ok := a.nonMasqCIDRsInMap[cidrStr]; !ok {
log.WithField(logfields.CIDR, cidrStr).Info("Adding CIDR")
a.ipMasqMap.Update(cidr)
a.nonMasqCIDRsInMap[cidrStr] = cidr
}
}
for cidrStr, cidr := range a.nonMasqCIDRsInMap {
if _, ok := a.nonMasqCIDRsFromConfig[cidrStr]; !ok {
log.WithField(logfields.CIDR, cidrStr).Info("Removing CIDR")
a.ipMasqMap.Delete(cidr)
delete(a.nonMasqCIDRsInMap, cidrStr)
}
}
return nil
}
// readConfig reads the config file and populates IPMasqAgent.nonMasqCIDRsFromConfig
// with the CIDRs from the file.
func (a *IPMasqAgent) readConfig() (bool, error) {
var cfg config
raw, err := os.ReadFile(a.configPath)
if err != nil {
if os.IsNotExist(err) {
log.WithField(logfields.Path, a.configPath).Info("Config file not found")
a.nonMasqCIDRsFromConfig = map[string]netip.Prefix{}
a.masqLinkLocalIPv4 = false
a.masqLinkLocalIPv6 = false
return true, nil
}
return false, fmt.Errorf("Failed to read %s: %w", a.configPath, err)
}
if len(raw) == 0 {
a.nonMasqCIDRsFromConfig = map[string]netip.Prefix{}
a.masqLinkLocalIPv4 = false
a.masqLinkLocalIPv6 = false
return true, nil
}
jsonStr, err := yaml.ToJSON(raw)
if err != nil {
return false, fmt.Errorf("Failed to convert to json: %w", err)
}
if err := json.Unmarshal(jsonStr, &cfg); err != nil {
return false, fmt.Errorf("Failed to de-serialize json: %w", err)
}
nonMasqCIDRs := map[string]netip.Prefix{}
for _, cidr := range cfg.NonMasqCIDRs {
n := netip.Prefix(cidr)
nonMasqCIDRs[n.String()] = n
}
a.nonMasqCIDRsFromConfig = nonMasqCIDRs
a.masqLinkLocalIPv4 = cfg.MasqLinkLocalIPv4
a.masqLinkLocalIPv6 = cfg.MasqLinkLocalIPv6
return false, nil
}
func (a *IPMasqAgent) NonMasqCIDRsFromConfig() []netip.Prefix {
return slices.Collect(maps.Values(a.nonMasqCIDRsFromConfig))
}
// restore dumps the ipmasq BPF map and populates IPMasqAgent.nonMasqCIDRsInMap
// with the CIDRs from the map.
func (a *IPMasqAgent) restore() error {
cidrsInMap, err := a.ipMasqMap.Dump()
if err != nil {
return fmt.Errorf("Failed to dump ip-masq-agent cidrs from map: %w", err)
}
cidrs := map[string]netip.Prefix{}
for _, cidr := range cidrsInMap {
cidrs[cidr.String()] = cidr
}
a.nonMasqCIDRsInMap = cidrs
return nil
}