-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
ipmasq.go
279 lines (237 loc) · 7.26 KB
/
ipmasq.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package ipmasq
import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/ipmasq"
)
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]net.IPNet{
"10.0.0.0/8": mustParseCIDR("10.0.0.0/8"),
"172.16.0.0/12": mustParseCIDR("172.16.0.0/12"),
"192.168.0.0/16": mustParseCIDR("192.168.0.0/16"),
"100.64.0.0/10": mustParseCIDR("100.64.0.0/10"),
"192.0.0.0/24": mustParseCIDR("192.0.0.0/24"),
"192.0.2.0/24": mustParseCIDR("192.0.2.0/24"),
"192.88.99.0/24": mustParseCIDR("192.88.99.0/24"),
"198.18.0.0/15": mustParseCIDR("198.18.0.0/15"),
"198.51.100.0/24": mustParseCIDR("198.51.100.0/24"),
"203.0.113.0/24": mustParseCIDR("203.0.113.0/24"),
"240.0.0.0/4": mustParseCIDR("240.0.0.0/4"),
}
linkLocalCIDRStr = "169.254.0.0/16"
linkLocalCIDR = mustParseCIDR(linkLocalCIDRStr)
)
// ipnet is a wrapper type for net.IPNet to enable de-serialization of CIDRs
type Ipnet net.IPNet
func (c *Ipnet) UnmarshalJSON(json []byte) error {
str := string(json)
if json[0] != '"' {
return fmt.Errorf("Invalid CIDR: %s", str)
}
n, err := parseCIDRv4(strings.Trim(str, `"`))
if err != nil {
return err
}
*c = Ipnet(*n)
return nil
}
func parseCIDRv4(c string) (*net.IPNet, error) {
ip, n, err := net.ParseCIDR(c)
if err != nil {
return nil, fmt.Errorf("Invalid CIDR %s: %s", c, err)
}
if ip.To4() == nil {
return nil, fmt.Errorf("Invalid CIDR %s: only IPv4 is supported", c)
}
return n, nil
}
// config represents the ip-masq-agent configuration file encoded as YAML
type config struct {
NonMasqCIDRs []Ipnet `json:"nonMasqueradeCIDRs"`
MasqLinkLocal bool `json:"masqLinkLocal"`
}
// IPMasqMap is an interface describing methods for manipulating an ipmasq map
type IPMasqMap interface {
Update(cidr net.IPNet) error
Delete(cidr net.IPNet) error
Dump() ([]net.IPNet, error)
}
// IPMasqAgent represents a state of the ip-masq-agent
type IPMasqAgent struct {
configPath string
masqLinkLocal bool
nonMasqCIDRsFromConfig map[string]net.IPNet
nonMasqCIDRsInMap map[string]net.IPNet
ipMasqMap IPMasqMap
watcher *fsnotify.Watcher
stop chan struct{}
handlerFinished chan struct{}
}
func NewIPMasqAgent(configPath string) (*IPMasqAgent, error) {
return newIPMasqAgent(configPath, &ipmasq.IPMasqBPFMap{})
}
func newIPMasqAgent(configPath string, ipMasqMap IPMasqMap) (*IPMasqAgent, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, fmt.Errorf("Failed to create fsnotify watcher: %s", err)
}
configDir := filepath.Dir(configPath)
// The directory of the config should exist at this time, otherwise
// the watcher will fail to add
if err := watcher.Add(configDir); err != nil {
watcher.Close()
return nil, fmt.Errorf("Failed to add %q dir to fsnotify watcher: %s", configDir, err)
}
a := &IPMasqAgent{
configPath: configPath,
nonMasqCIDRsFromConfig: map[string]net.IPNet{},
nonMasqCIDRsInMap: map[string]net.IPNet{},
ipMasqMap: ipMasqMap,
watcher: watcher,
}
return a, nil
}
// Start starts the ip-masq-agent goroutine which tracks the config file and
// updates the BPF map accordingly.
func (a *IPMasqAgent) Start() {
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
}
}
}()
}
// Stop stops the ip-masq-agent goroutine and the watcher.
func (a *IPMasqAgent) Stop() {
close(a.stop)
<-a.handlerFinished
a.watcher.Close()
}
// 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.masqLinkLocal {
a.nonMasqCIDRsFromConfig[linkLocalCIDRStr] = linkLocalCIDR
}
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]net.IPNet{}
a.masqLinkLocal = false
return true, nil
}
return false, fmt.Errorf("Failed to read %s: %s", a.configPath, err)
}
if len(raw) == 0 {
a.nonMasqCIDRsFromConfig = map[string]net.IPNet{}
a.masqLinkLocal = false
return true, nil
}
jsonStr, err := yaml.ToJSON(raw)
if err != nil {
return false, fmt.Errorf("Failed to convert to json: %s", err)
}
if err := json.Unmarshal(jsonStr, &cfg); err != nil {
return false, fmt.Errorf("Failed to de-serialize json: %s", err)
}
nonMasqCIDRs := map[string]net.IPNet{}
for _, cidr := range cfg.NonMasqCIDRs {
n := net.IPNet(cidr)
nonMasqCIDRs[n.String()] = n
}
a.nonMasqCIDRsFromConfig = nonMasqCIDRs
a.masqLinkLocal = cfg.MasqLinkLocal
return false, nil
}
// 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: %s", err)
}
cidrs := map[string]net.IPNet{}
for _, cidr := range cidrsInMap {
cidrs[cidr.String()] = cidr
}
a.nonMasqCIDRsInMap = cidrs
return nil
}
func mustParseCIDR(c string) net.IPNet {
n, err := parseCIDRv4(c)
if err != nil {
panic(err)
}
return *n
}