forked from cilium/cilium
/
node_address.go
461 lines (394 loc) · 12.6 KB
/
node_address.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Copyright 2016-2018 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package node
import (
"bufio"
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/common"
"github.com/cilium/cilium/pkg/byteorder"
"github.com/cilium/cilium/pkg/cidr"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/option"
)
var (
ipv4ClusterCidrMaskSize = defaults.DefaultIPv4ClusterPrefixLen
ipv4Loopback net.IP
ipv4ExternalAddress net.IP
ipv4InternalAddress net.IP
ipv4NodePortAddress net.IP
ipv6Address net.IP
ipv6RouterAddress net.IP
ipv6NodePortAddress net.IP
ipv4AllocRange *cidr.CIDR
ipv6AllocRange *cidr.CIDR
ipsecKeyIdentity uint8
)
func makeIPv6HostIP() net.IP {
ipstr := "fc00::10CA:1"
ip := net.ParseIP(ipstr)
if ip == nil {
log.WithField(logfields.IPAddr, ipstr).Fatal("Unable to parse IP")
}
return ip
}
// SetIPv4ClusterCidrMaskSize sets the size of the mask of the IPv4 cluster prefix
func SetIPv4ClusterCidrMaskSize(size int) {
ipv4ClusterCidrMaskSize = size
}
// InitDefaultPrefix initializes the node address and allocation prefixes with
// default values derived from the system. device can be set to the primary
// network device of the system in which case the first address with global
// scope will be regarded as the system's node address.
func InitDefaultPrefix(device string) {
if option.Config.EnableIPv4 {
ip, err := firstGlobalV4Addr(device, GetInternalIPv4())
if err != nil {
return
}
if ipv4ExternalAddress == nil {
ipv4ExternalAddress = ip
}
if ipv4AllocRange == nil {
// If the IPv6AllocRange is not nil then the IPv4 allocation should be
// derived from the IPv6AllocRange.
// vvvv vvvv
// FD00:0000:0000:0000:0000:0000:0000:0000
if ipv6AllocRange != nil {
ip = net.IPv4(ipv6AllocRange.IP[8],
ipv6AllocRange.IP[9],
ipv6AllocRange.IP[10],
ipv6AllocRange.IP[11])
}
v4range := fmt.Sprintf(defaults.DefaultIPv4Prefix+"/%d",
ip.To4()[3], defaults.DefaultIPv4PrefixLen)
_, ip4net, err := net.ParseCIDR(v4range)
if err != nil {
log.WithError(err).WithField(logfields.V4Prefix, v4range).Panic("BUG: Invalid default IPv4 prefix")
}
ipv4AllocRange = cidr.NewCIDR(ip4net)
log.WithField(logfields.V4Prefix, ipv4AllocRange).Info("Using autogenerated IPv4 allocation range")
}
if option.Config.EnableNodePort {
// device is always non-nil here
ip, err := firstGlobalV4Addr(device, nil)
if err != nil {
return
}
if ipv4NodePortAddress == nil {
ipv4NodePortAddress = ip
}
}
}
if option.Config.EnableIPv6 {
if ipv6Address == nil {
// Find a IPv6 node address first
ipv6Address, _ = firstGlobalV6Addr(device, GetIPv6Router())
if ipv6Address == nil {
ipv6Address = makeIPv6HostIP()
}
}
if ipv6AllocRange == nil && ipv4AllocRange != nil {
// The IPv6 allocation should be derived from the IPv4 allocation.
ip := ipv4AllocRange.IP
v6range := fmt.Sprintf("%s%02x%02x:%02x%02x:0:0/%d",
option.Config.IPv6ClusterAllocCIDRBase, ip[0], ip[1], ip[2], ip[3],
defaults.IPv6NodePrefixLen)
_, ip6net, err := net.ParseCIDR(v6range)
if err != nil {
log.WithError(err).WithField(logfields.V6Prefix, v6range).Panic("BUG: Invalid default IPv6 prefix")
}
ipv6AllocRange = cidr.NewCIDR(ip6net)
log.WithField(logfields.V6Prefix, ipv6AllocRange).Info("Using autogenerated IPv6 allocation range")
}
if option.Config.EnableNodePort {
ip, err := firstGlobalV6Addr(device, nil)
if err != nil {
return
}
if ipv6NodePortAddress == nil {
ipv6NodePortAddress = ip
}
}
}
}
// GetIPv4ClusterRange returns the IPv4 prefix of the cluster
func GetIPv4ClusterRange() *net.IPNet {
if ipv4AllocRange == nil {
return nil
}
mask := net.CIDRMask(ipv4ClusterCidrMaskSize, 32)
return &net.IPNet{
IP: ipv4AllocRange.IPNet.IP.Mask(mask),
Mask: mask,
}
}
// GetIPv4Loopback returns the loopback IPv4 address of this node.
func GetIPv4Loopback() net.IP {
return ipv4Loopback
}
// SetIPv4Loopback sets the loopback IPv4 address of this node.
func SetIPv4Loopback(ip net.IP) {
ipv4Loopback = ip
}
// GetIPv4AllocRange returns the IPv4 allocation prefix of this node
func GetIPv4AllocRange() *cidr.CIDR {
return ipv4AllocRange
}
// GetIPv6AllocRange returns the IPv6 allocation prefix of this node
func GetIPv6AllocRange() *cidr.CIDR {
if ipv6AllocRange == nil {
return nil
}
mask := net.CIDRMask(defaults.IPv6NodeAllocPrefixLen, 128)
return cidr.NewCIDR(&net.IPNet{
IP: ipv6AllocRange.IPNet.IP.Mask(mask),
Mask: mask,
})
}
// GetIPv6NodeRange returns the IPv6 allocation prefix of this node
func GetIPv6NodeRange() *cidr.CIDR {
return ipv6AllocRange
}
// SetExternalIPv4 sets the external IPv4 node address. It must be reachable on the network.
func SetExternalIPv4(ip net.IP) {
ipv4ExternalAddress = ip
}
// GetExternalIPv4 returns the external IPv4 node address
func GetExternalIPv4() net.IP {
return ipv4ExternalAddress
}
// SetInternalIPv4 sets the internal IPv4 node address, it is allocated from the node prefix
func SetInternalIPv4(ip net.IP) {
ipv4InternalAddress = ip
}
// GetInternalIPv4 returns the internal IPv4 node address
func GetInternalIPv4() net.IP {
return ipv4InternalAddress
}
// GetHostMasqueradeIPv4 returns the IPv4 address to be used for masquerading
// any traffic that is being forwarded from the host into the Cilium cluster.
func GetHostMasqueradeIPv4() net.IP {
return ipv4InternalAddress
}
// SetIPv4AllocRange sets the IPv4 address pool to use when allocating
// addresses for local endpoints
func SetIPv4AllocRange(net *cidr.CIDR) {
ipv4AllocRange = net
}
// Uninitialize resets this package to the default state, for use in
// testsuite code.
func Uninitialize() {
ipv4AllocRange = nil
ipv6AllocRange = nil
}
// GetNodePortIPv4 returns the node-port IPv4 address for NAT
func GetNodePortIPv4() net.IP {
return ipv4NodePortAddress
}
// GetNodePortIPv6 returns the node-port IPv4 address for NAT
func GetNodePortIPv6() net.IP {
return ipv6NodePortAddress
}
// SetIPv6NodeRange sets the IPv6 address pool to be used on this node
func SetIPv6NodeRange(net *net.IPNet) error {
if ones, _ := net.Mask.Size(); ones != defaults.IPv6NodePrefixLen {
return fmt.Errorf("prefix length must be /%d", defaults.IPv6NodePrefixLen)
}
copy := *net
ipv6AllocRange = cidr.NewCIDR(©)
return nil
}
// AutoComplete completes the parts of addressing that can be auto derived
func AutoComplete() error {
if option.Config.EnableHostIPRestore {
// Read the previous cilium host IPs from node_config.h for backward
// compatibility
ipv4GW, ipv6Router := getCiliumHostIPs()
if ipv4GW != nil && option.Config.EnableIPv4 {
log.Infof("Restored IPv4 internal node IP: %s", ipv4GW.String())
SetInternalIPv4(ipv4GW)
}
if ipv6Router != nil && option.Config.EnableIPv6 {
log.Infof("Restored IPv6 router IP: %s", ipv6Router.String())
SetIPv6Router(ipv6Router)
}
}
if option.Config.Device == "undefined" {
InitDefaultPrefix("")
}
if option.Config.EnableIPv6 && ipv6AllocRange == nil {
return fmt.Errorf("IPv6 allocation CIDR is not configured. Please specificy --ipv6-range")
}
if option.Config.EnableIPv4 && ipv4AllocRange == nil {
return fmt.Errorf("IPv4 allocation CIDR is not configured. Please specificy --ipv4-range")
}
return nil
}
// ValidatePostInit validates the entire addressing setup and completes it as
// required
func ValidatePostInit() error {
if option.Config.EnableIPv4 || option.Config.Tunnel != option.TunnelDisabled {
if ipv4ExternalAddress == nil {
return fmt.Errorf("external IPv4 node address could not be derived, please configure via --ipv4-node")
}
}
if option.Config.EnableIPv4 {
if ipv4InternalAddress == nil {
return fmt.Errorf("BUG: Internal IPv4 node address was not configured")
}
ones, _ := ipv4AllocRange.Mask.Size()
if ipv4ClusterCidrMaskSize > ones {
return fmt.Errorf("IPv4 per node allocation prefix (%s) must be inside cluster prefix (%s)",
ipv4AllocRange, GetIPv4ClusterRange())
}
}
return nil
}
// SetIPv6 sets the IPv6 address of the node
func SetIPv6(ip net.IP) {
ipv6Address = ip
}
// GetIPv6 returns the IPv6 address of the node
func GetIPv6() net.IP {
return ipv6Address
}
// GetIPv6Router returns the IPv6 address of the node
func GetIPv6Router() net.IP {
return ipv6RouterAddress
}
// SetIPv6Router returns the IPv6 address of the node
func SetIPv6Router(ip net.IP) {
ipv6RouterAddress = ip
}
// IsHostIPv4 returns true if the IP specified is a host IP
func IsHostIPv4(ip net.IP) bool {
return ip.Equal(GetInternalIPv4()) || ip.Equal(GetExternalIPv4())
}
// IsHostIPv6 returns true if the IP specified is a host IP
func IsHostIPv6(ip net.IP) bool {
return ip.Equal(GetIPv6()) || ip.Equal(GetIPv6Router())
}
// GetNodeAddressing returns the NodeAddressing model for the local IPs.
func GetNodeAddressing() *models.NodeAddressing {
a := &models.NodeAddressing{}
if option.Config.EnableIPv6 {
a.IPV6 = &models.NodeAddressingElement{
Enabled: option.Config.EnableIPv6,
IP: GetIPv6Router().String(),
AllocRange: GetIPv6AllocRange().String(),
}
}
if option.Config.EnableIPv4 {
a.IPV4 = &models.NodeAddressingElement{
Enabled: option.Config.EnableIPv4,
IP: GetInternalIPv4().String(),
AllocRange: GetIPv4AllocRange().String(),
}
}
return a
}
func getCiliumHostIPsFromFile(nodeConfig string) (ipv4GW, ipv6Router net.IP) {
var hasIPv4, hasIPv6 bool
f, err := os.Open(nodeConfig)
switch {
case err != nil:
default:
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
txt := scanner.Text()
switch {
case !hasIPv6 && strings.Contains(txt, defaults.RestoreV6Addr):
defineLine := strings.Split(txt, defaults.RestoreV6Addr)
if len(defineLine) != 2 {
continue
}
ipv6 := common.C2GoArray(defineLine[1])
if len(ipv6) != net.IPv6len {
continue
}
ipv6Router = net.IP(ipv6)
hasIPv6 = true
case !hasIPv4 && strings.Contains(txt, defaults.RestoreV4Addr):
defineLine := strings.Split(txt, defaults.RestoreV4Addr)
if len(defineLine) != 2 {
continue
}
ipv4 := common.C2GoArray(defineLine[1])
if len(ipv4) != net.IPv6len {
continue
}
ipv4GW = net.IP(ipv4)
hasIPv4 = true
// Legacy cases based on the header defines:
case !hasIPv4 && strings.Contains(txt, "IPV4_GATEWAY"):
// #define IPV4_GATEWAY 0xee1c000a
defineLine := strings.Split(txt, " ")
if len(defineLine) != 3 {
continue
}
ipv4GWHex := strings.TrimPrefix(defineLine[2], "0x")
ipv4GWint64, err := strconv.ParseInt(ipv4GWHex, 16, 0)
if err != nil {
continue
}
if ipv4GWint64 != int64(0) {
bs := make([]byte, net.IPv4len)
byteorder.NetworkToHostPut(bs, uint32(ipv4GWint64))
ipv4GW = net.IPv4(bs[0], bs[1], bs[2], bs[3])
hasIPv4 = true
}
case !hasIPv6 && strings.Contains(txt, " ROUTER_IP "):
// #define ROUTER_IP 0xf0, 0xd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8a, 0xd6
defineLine := strings.Split(txt, " ROUTER_IP ")
if len(defineLine) != 2 {
continue
}
ipv6 := common.C2GoArray(defineLine[1])
if len(ipv6) != net.IPv6len {
continue
}
ipv6Router = net.IP(ipv6)
hasIPv6 = true
}
}
}
return ipv4GW, ipv6Router
}
// getCiliumHostIPs returns the Cilium IPv4 gateway and router IPv6 address from
// the node_config.h file if is present; or by deriving it from
// defaults.HostDevice interface, on which only the IPv4 is possible to derive.
func getCiliumHostIPs() (ipv4GW, ipv6Router net.IP) {
nodeConfig := option.Config.GetNodeConfigPath()
ipv4GW, ipv6Router = getCiliumHostIPsFromFile(nodeConfig)
if ipv4GW != nil {
return ipv4GW, ipv6Router
}
return getCiliumHostIPsFromNetDev(option.Config.HostDevice)
}
// SetIPsecKeyIdentity sets the IPsec key identity an opaque value used to
// identity encryption keys used on the node.
func SetIPsecKeyIdentity(id uint8) {
ipsecKeyIdentity = id
}
// GetIPsecKeyIdentity returns the IPsec key identity of the node
func GetIPsecKeyIdentity() uint8 {
return ipsecKeyIdentity
}