-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipam.go
429 lines (362 loc) · 11.9 KB
/
ipam.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
/*
MIT License
(C) Copyright 2022 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
// Package ipam provides IP address management functionality.
// Mostly a copy/paste from https://github.com/giantswarm/ipam without prioprietary service/error handling
package ipam
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"math/bits"
"net"
"reflect"
"sort"
)
// IPRange defines a pair of IPs, over a range.
type IPRange struct {
start net.IP
end net.IP
}
// IPNets is a helper type for sorting net.IPNets.
type IPNets []net.IPNet
func (s IPNets) Len() int {
return len(s)
}
func (s IPNets) Less(i, j int) bool {
return ipToDecimal(s[i].IP) < ipToDecimal(s[j].IP)
}
func (s IPNets) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// CalculateSubnetMask calculates new subnet mask to accommodate n subnets.
func CalculateSubnetMask(networkMask net.IPMask, n uint) (net.IPMask, error) {
if n == 0 {
return nil, errors.New("divide by zero")
}
// Calculate amount of bits needed to accommodate at least N subnets.
subnetBitsNeeded := bits.Len(n - 1)
maskOnes, maskBits := networkMask.Size()
if subnetBitsNeeded > maskBits-maskOnes {
return nil, fmt.Errorf("no room in network mask %s to accommodate %d subnets", networkMask.String(), n)
}
return net.CIDRMask(maskOnes+subnetBitsNeeded, maskBits), nil
}
// CanonicalizeSubnets iterates over subnets and returns deduplicated list of
// networks that belong to networkRange. Subnets that overlap each other but
// aren't exactly the same are not removed. Subnets are returned in the same
// order as they appear in input.
//
// Example:
//
// networkRange: 192.168.2.0/24
// subnets: [172.168.2.0/25, 192.168.2.0/25, 192.168.3.128/25, 192.168.2.0/25, 192.168.2.128/25]
// returned: [192.168.2.0/25, 192.168.2.128/25]
//
// Example 2:
//
// networkRange: 10.0.0.0/8
// subnets: [10.1.0.0/16, 10.1.0.0/24, 10.1.1.0/24]
// returned: [10.1.0.0/16, 10.1.0.0/24, 10.1.1.0/24]
func CanonicalizeSubnets(networkRange net.IPNet, subnets []net.IPNet) []net.IPNet {
// Naive deduplication as net.IPNet cannot be used as key for map. This
// should be ok for current foreseeable future.
for i := 0; i < len(subnets); i++ {
// Remove subnets that don't belong to our desired network.
if !networkRange.Contains(subnets[i].IP) {
subnets = append(subnets[:i], subnets[i+1:]...)
i--
continue
}
// Remove duplicates.
for j := i + 1; j < len(subnets); j++ {
if reflect.DeepEqual(subnets[i], subnets[j]) {
subnets = append(subnets[:j], subnets[j+1:]...)
j--
}
}
}
return subnets
}
// Contains returns true when the subnet is a part of the network, false
// otherwise.
func Contains(network, subnet net.IPNet) bool {
subnetRange := NewIPRange(subnet)
return network.Contains(subnetRange.start) && network.Contains(subnetRange.end)
}
// Free takes a network, a mask, and a list of subnets.
// An available network, within the first network, is returned.
func Free(network net.IPNet, mask net.IPMask, subnets []net.IPNet) (net.IPNet, error) {
if size(network.Mask) < size(mask) {
return net.IPNet{},
fmt.Errorf("have: %v, requested: %v", network.Mask, mask)
}
for _, subnet := range subnets {
if !network.Contains(subnet.IP) {
return net.IPNet{},
fmt.Errorf("%v is not contained by %v", subnet.IP, network)
}
}
sort.Sort(IPNets(subnets))
// Find all the free IP ranges.
freeIPRanges, err := freeIPRanges(network, subnets)
if err != nil {
return net.IPNet{}, err
}
// Attempt to find a free space, of the required size.
freeIP, err := space(freeIPRanges, mask)
if err != nil {
return net.IPNet{}, err
}
// Invariant: The IP of the network returned should not be nil.
if freeIP == nil {
return net.IPNet{}, errors.New("no available ips")
}
freeNetwork := net.IPNet{IP: freeIP, Mask: mask}
// Invariant: The IP of the network returned should be contained
// within the network supplied.
if !network.Contains(freeNetwork.IP) {
return net.IPNet{},
fmt.Errorf("%v is not contained by %v", freeNetwork.IP, network)
}
// Invariant: The mask of the network returned should be equal to
// the mask supplied as an argument.
if !bytes.Equal(mask, freeNetwork.Mask) {
return net.IPNet{},
fmt.Errorf("have: %v, requested: %v", freeNetwork.Mask, mask)
}
return freeNetwork, nil
}
// Half takes a network and returns two subnets which split the network in
// half.
func Half(network net.IPNet) (first, second net.IPNet, err error) {
ones, parts := network.Mask.Size()
if ones == parts {
return net.IPNet{}, net.IPNet{}, fmt.Errorf("single IP mask %q is not allowed", network.Mask.String())
}
// Bit shift is dividing by 2.
ones++
mask := net.CIDRMask(ones, parts)
// Compute first half.
first, err = Free(network, mask, nil)
if err != nil {
return net.IPNet{}, net.IPNet{}, err
}
// Second half is computed by getting next free.
second, err = Free(network, mask, []net.IPNet{first})
if err != nil {
return net.IPNet{}, net.IPNet{}, err
}
return first, second, nil
}
// Split returns n subnets from network.
func Split(network net.IPNet, n uint) ([]net.IPNet, error) {
mask, err := CalculateSubnetMask(network.Mask, n)
if err != nil {
return nil, err
}
var subnets []net.IPNet
for i := uint(0); i < n; i++ {
subnet, err := Free(network, mask, subnets)
if err != nil {
return nil, err
}
subnets = append(subnets, subnet)
}
return subnets, nil
}
// Add increments the given IP by the number.
// e.g: add(10.0.4.0, 1) -> 10.0.4.1.
// Negative values are allowed for decrementing.
func Add(ip net.IP, number int) net.IP {
return decimalToIP(ipToDecimal(ip) + number)
}
// decimalToIP converts an int to a net.IP.
func decimalToIP(ip int) net.IP {
t := make(net.IP, 4)
binary.BigEndian.PutUint32(t, uint32(ip))
return t
}
// freeIPRanges takes a network, and a list of subnets.
// It calculates available IPRanges, within the original network.
func freeIPRanges(network net.IPNet, subnets []net.IPNet) ([]IPRange, error) {
freeSubnets := []IPRange{}
networkRange := NewIPRange(network)
if len(subnets) == 0 {
freeSubnets = append(freeSubnets, networkRange)
return freeSubnets, nil
}
{
// Check space between start of network and first subnet.
firstSubnetRange := NewIPRange(subnets[0])
// Check the first subnet doesn't start at the start of the network.
if !networkRange.start.Equal(firstSubnetRange.start) {
// It doesn't, so we have a free range between the start
// of the network, and the start of the first subnet.
end := Add(firstSubnetRange.start, -1)
freeSubnets = append(freeSubnets,
IPRange{start: networkRange.start, end: end},
)
}
}
{
// Check space between each subnet.
for i := 0; i < len(subnets)-1; i++ {
currentSubnetRange := NewIPRange(subnets[i])
nextSubnetRange := NewIPRange(subnets[i+1])
// If the two subnets are not contiguous,
if ipToDecimal(currentSubnetRange.end)+1 != ipToDecimal(nextSubnetRange.start) {
// Then there is a free range between them.
start := Add(currentSubnetRange.end, 1)
end := Add(nextSubnetRange.start, -1)
freeSubnets = append(freeSubnets, IPRange{start: start, end: end})
}
}
}
{
// Check space between last subnet and end of network.
lastSubnetRange := NewIPRange(subnets[len(subnets)-1])
// Check the last subnet doesn't end at the end of the network.
if !lastSubnetRange.end.Equal(networkRange.end) {
// It doesn't, so we have a free range between the end of the
// last subnet, and the end of the network.
start := Add(lastSubnetRange.end, 1)
freeSubnets = append(freeSubnets,
IPRange{start: start, end: networkRange.end},
)
}
}
return freeSubnets, nil
}
// ipToDecimal converts a net.IP to an int.
func ipToDecimal(ip net.IP) int {
t := ip
if len(ip) == 16 {
t = ip[12:16]
}
return int(binary.BigEndian.Uint32(t))
}
// NewIPRange takes an IPNet, and returns the ipRange of the network.
func NewIPRange(network net.IPNet) IPRange {
start := network.IP
end := Add(network.IP, size(network.Mask)-1)
return IPRange{start: start, end: end}
}
// size takes a mask, and returns the number of addresses.
func size(mask net.IPMask) int {
ones, _ := mask.Size()
size := int(math.Pow(2, float64(32-ones)))
return size
}
// Broadcast takes a net.IPNet and returns the broadcast address as net.IP
func Broadcast(network net.IPNet) net.IP {
return Add(network.IP, size(network.Mask)-1)
}
// space takes a list of free ip ranges, and a mask,
// and returns the start IP of the first range that could fit the mask.
func space(freeIPRanges []IPRange, mask net.IPMask) (net.IP, error) {
for _, freeIPRange := range freeIPRanges {
start := ipToDecimal(freeIPRange.start)
end := ipToDecimal(freeIPRange.end)
// When subnet allocations contain various different subnet sizes, it can be
// that free IP range starts from smaller network than what we are finding
// for. Therefore we must first adjust the start IP such that it can hold the
// whole network that we are looking space for.
//
// Example: Free IP range starts at 10.1.2.192 and ends 10.1.255.255.
// We look for next available /24 network so first suitable
// start IP for this would be 10.1.3.0.
//
ones, _ := mask.Size()
trailingZeros := bits.TrailingZeros32(uint32(start))
for (start < end) && (ones < (32 - trailingZeros)) {
var mask uint32
for i := 0; i < trailingZeros; i++ {
mask |= 1 << uint32(i)
}
start = int(uint32(start) | mask)
start++
trailingZeros = bits.TrailingZeros32(uint32(start))
}
if end-start+1 >= size(mask) {
return decimalToIP(start), nil
}
}
return nil, fmt.Errorf("tried to fit: %v", mask)
}
// SubnetWithin returns the smallest subnet than can contain (size) hosts
func SubnetWithin(network net.IPNet, hostNumber int) (net.IPNet, error) {
var n net.IPNet
ip := network.IP.String()
// sort the map
keys := make([]int, 0)
for k := range netmasks {
keys = append(keys, k)
}
sort.Ints(keys)
// run through the sorted map
for _, k := range keys {
subnet := netmasks[k]
if k > hostNumber {
_, mynet, err := net.ParseCIDR(fmt.Sprintf("%v%v", ip, subnet))
return *mynet, err
}
}
return n, nil
}
// NetIPInSlice makes it easy to assess if an IP address is present in a list of ips
func NetIPInSlice(a net.IP, list []net.IP) int {
for index, b := range list {
if b.Equal(a) {
return index
}
}
return 0
}
// IPLessThan compare two ip addresses
// by section left-most is most significant
func IPLessThan(a, b net.IP) bool {
for i := range a { // go left to right and compare each one
if a[i] != b[i] {
return a[i] < b[i]
}
}
return false // they are equal
}
// Quick const map for mapping the number of hosts to the netmask shorthand
// I'm bad at math.
var netmasks = map[int]string{
2: "/30",
6: "/29",
14: "/28",
30: "/27",
62: "/26",
126: "/25",
254: "/24",
510: "/23",
1022: "/22",
2046: "/21",
4094: "/20",
8190: "/19",
16382: "/18",
32766: "/17",
65534: "/16",
}