-
Notifications
You must be signed in to change notification settings - Fork 373
/
ip.go
305 lines (273 loc) · 9.03 KB
/
ip.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
// Copyright 2020 Antrea Authors
//
// 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 ip
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"net/netip"
"sort"
utilnet "k8s.io/utils/net"
"antrea.io/antrea/pkg/apis/controlplane/v1beta2"
)
const (
V4BitLen = 8 * net.IPv4len
V6BitLen = 8 * net.IPv6len
)
type DualStackIPs struct {
IPv4 net.IP
IPv6 net.IP
}
func (ips DualStackIPs) Equal(x DualStackIPs) bool {
return ips.IPv4.Equal(x.IPv4) && ips.IPv6.Equal(x.IPv6)
}
// This function takes in one allow CIDR and multiple except CIDRs and gives diff CIDRs
// in allowCIDR eliminating except CIDRs. It currently supports only IPv4. except CIDR input
// can be changed.
func DiffFromCIDRs(allowCIDR *net.IPNet, exceptCIDRs []*net.IPNet) ([]*net.IPNet, error) {
// Remove the redundant CIDRs
exceptCIDRs = MergeCIDRs(exceptCIDRs)
newCIDRs := []*net.IPNet{allowCIDR}
for _, exceptCIDR := range exceptCIDRs {
beginLoop:
for i, indCIDR := range newCIDRs {
// Consider masked IP from IPNet struct
if indCIDR.Contains(exceptCIDR.IP.Mask(exceptCIDR.Mask)) {
result := diffFromCIDR(indCIDR, exceptCIDR)
// Delete the considered CIDR block and add resulting CIDR blocks
copy(newCIDRs[i:], newCIDRs[i+1:])
newCIDRs[len(newCIDRs)-1] = nil
newCIDRs = newCIDRs[:len(newCIDRs)-1]
// Append the result CIDRs
newCIDRs = append(newCIDRs, result...)
// This step can be optimized by having iterator over just the index. Went with reinitialization of iterator.
goto beginLoop
} else if exceptCIDR.Contains(indCIDR.IP) {
// Just delete the CIDR block
copy(newCIDRs[i:], newCIDRs[i+1:])
newCIDRs[len(newCIDRs)-1] = nil
newCIDRs = newCIDRs[:len(newCIDRs)-1]
goto beginLoop
}
}
}
return newCIDRs, nil
}
// This function gives diff CIDRs between a superset CIDR (allow CIDR) and subset CIDR
// (except CIDR)
func diffFromCIDR(allowCIDR, exceptCIDR *net.IPNet) []*net.IPNet {
allowPrefix, _ := allowCIDR.Mask.Size()
exceptPrefix, _ := exceptCIDR.Mask.Size()
// Mask the IP to get the start IP of range
allowStartIP := allowCIDR.IP.Mask(allowCIDR.Mask)
exceptStartIP := exceptCIDR.IP.Mask(exceptCIDR.Mask)
var bits int
if allowStartIP.To4() != nil {
bits = V4BitLen
} else {
bits = V6BitLen
}
// New CIDRs should not contain the IPs in exceptCIDR. Manipulating the bits in start IP of
// exceptCIDR will give remainder IPs in allowCIDR, specifically the masked IPs for remaining
// CIDRs with prefix ranging from [allowPrefix+1, exceptPrefix].
remainingCIDRs := make([]*net.IPNet, 0, exceptPrefix-allowPrefix)
for i := allowPrefix + 1; i <= exceptPrefix; i++ {
// Flip the (ipBitLen - i)th bit from LSB in exceptCIDR to get the IP which is not in exceptCIDR
ipOfNewCIDR := flipSingleBit(&exceptStartIP, bits-i)
newCIDRMask := net.CIDRMask(i, bits)
for j := range allowStartIP {
ipOfNewCIDR[j] = allowStartIP[j] | ipOfNewCIDR[j]
}
newCIDR := net.IPNet{IP: ipOfNewCIDR.Mask(newCIDRMask), Mask: newCIDRMask}
remainingCIDRs = append(remainingCIDRs, &newCIDR)
}
return remainingCIDRs
}
func flipSingleBit(ip *net.IP, bitIndex int) net.IP {
newIP := make(net.IP, len(*ip))
copy(newIP, *ip)
byteIndex := len(newIP) - (bitIndex / 8) - 1
// XOR bit operation to flip
newIP[byteIndex] = newIP[byteIndex] ^ (1 << (bitIndex % 8))
return newIP
}
// This function is to check for redundant CIDRs in the list that are
// covered by other CIDRs and remove them. Input array can be modified.
func MergeCIDRs(cidrBlocks []*net.IPNet) []*net.IPNet {
// Sort the list by netmask in ascending order
sort.Slice(cidrBlocks, func(i, j int) bool {
return bytes.Compare(cidrBlocks[i].Mask, cidrBlocks[j].Mask) < 0
})
// Check and remove if there are redundant CIDRs that are part of bigger CIDRs
// or repeated CIDRs
for i := 0; i < len(cidrBlocks); i++ {
for j := i + 1; j < len(cidrBlocks); j++ {
if cidrBlocks[i].Contains(cidrBlocks[j].IP) {
// Delete the CIDR block and truncate the slice
copy(cidrBlocks[j:], cidrBlocks[j+1:])
cidrBlocks[len(cidrBlocks)-1] = nil
cidrBlocks = cidrBlocks[:len(cidrBlocks)-1]
// Decrement the tracker to consider next element
j = j - 1
}
}
}
return cidrBlocks
}
// IPNetToNetIPNet converts Antrea IPNet to *net.IPNet.
// Note that K8s allows non-standard CIDRs to be specified (e.g. 10.0.1.1/16, fe80::7015:efff:fe9a:146b/64). However,
// OVS will report OFPBMC_BAD_WILDCARDS error if using them in the OpenFlow messages. The function will normalize the
// CIDR if it's non-standard.
func IPNetToNetIPNet(ipNet *v1beta2.IPNet) *net.IPNet {
ip := net.IP(ipNet.IP)
ipLen := net.IPv4len
if ip.To4() == nil {
ipLen = net.IPv6len
}
mask := net.CIDRMask(int(ipNet.PrefixLength), 8*ipLen)
maskedIP := ip.Mask(mask)
return &net.IPNet{IP: maskedIP, Mask: mask}
}
const (
ICMPProtocol = 1
IGMPProtocol = 2
TCPProtocol = 6
UDPProtocol = 17
ICMPv6Protocol = 58
SCTPProtocol = 132
)
// IPProtocolNumberToString returns the string name of the IP protocol with number protocolNum. If
// the number does not match a "known" protocol, we return the defaultValue string.
func IPProtocolNumberToString(protocolNum uint8, defaultValue string) string {
switch protocolNum {
case ICMPProtocol:
return "ICMP"
case IGMPProtocol:
return "IGMP"
case TCPProtocol:
return "TCP"
case UDPProtocol:
return "UDP"
case ICMPv6Protocol:
return "IPv6-ICMP"
case SCTPProtocol:
return "SCTP"
default:
return defaultValue
}
}
// MustParseCIDR turns the given string into IPNet or panics, for tests or other cases where the string must be valid.
func MustParseCIDR(cidr string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
panic(fmt.Errorf("cannot parse '%v': %v", cidr, err))
}
return ipNet
}
func MustParseMAC(mac string) net.HardwareAddr {
addr, err := net.ParseMAC(mac)
if err != nil {
panic(fmt.Errorf("cannot parse '%v': %v", mac, err))
}
return addr
}
// IPNetEqual returns if the provided IPNets are the same subnet.
func IPNetEqual(ipNet1, ipNet2 *net.IPNet) bool {
if ipNet1 == nil && ipNet2 == nil {
return true
}
if ipNet1 == nil || ipNet2 == nil {
return false
}
if !bytes.Equal(ipNet1.Mask, ipNet2.Mask) {
return false
}
if !ipNet1.IP.Equal(ipNet2.IP) {
return false
}
return true
}
// IPNetContains returns if the first IPNet contains the second IPNet.
// For example:
//
// 10.0.0.0/24 contains 10.0.0.0/24.
// 10.0.0.0/24 contains 10.0.0.0/25.
// 10.0.0.0/24 contains 10.0.0.128/25.
// 10.0.0.0/24 does not contain 10.0.0.0/23.
// 10.0.0.0/24 does not contain 10.0.1.0/25.
func IPNetContains(ipNet1, ipNet2 *net.IPNet) bool {
if ipNet1 == nil || ipNet2 == nil {
return false
}
ones1, bits1 := ipNet1.Mask.Size()
ones2, bits2 := ipNet2.Mask.Size()
if bits1 != bits2 {
return false
}
if ones1 > ones2 {
return false
}
if !ipNet1.Contains(ipNet2.IP) {
return false
}
return true
}
func MustIPv6(s string) net.IP {
ip := net.ParseIP(s)
if !utilnet.IsIPv6(ip) {
panic(fmt.Errorf("invalid IPv6 address: %s", s))
}
return ip
}
// GetLocalBroadcastIP returns the last IP address in a subnet. This IP is always working as the broadcast address in
// the subnet on Windows, and an active route entry that uses it as the destination is added by default when a new IP is
// configured on the interface.
func GetLocalBroadcastIP(ipNet *net.IPNet) net.IP {
lastAddr := make(net.IP, len(ipNet.IP.To4()))
binary.BigEndian.PutUint32(lastAddr, binary.BigEndian.Uint32(ipNet.IP.To4())|^binary.BigEndian.Uint32(net.IP(ipNet.Mask).To4()))
return lastAddr
}
// AppendPortIfMissing appends the given port to the address if the address doesn't contain any port.
func AppendPortIfMissing(addr, port string) string {
if _, _, err := net.SplitHostPort(addr); err == nil {
return addr
}
ip := net.ParseIP(addr)
// Return the address directly if it's not a valid address.
if ip == nil {
return addr
}
return net.JoinHostPort(addr, port)
}
// GetStartAndEndOfPrefix retrieves the start and end addresses of a netip.Prefix.
// For example: 10.10.40.0/24 -> 10.10.40.0, 10.10.40.255
func GetStartAndEndOfPrefix(prefix netip.Prefix) (netip.Addr, netip.Addr) {
var start, end netip.Addr
var mask net.IPMask
if prefix.Addr().Is4() {
mask = net.CIDRMask(prefix.Bits(), 32)
} else {
mask = net.CIDRMask(prefix.Bits(), 128)
}
// use gateway address, of canonical form of prefix, as start address.
start = prefix.Masked().Addr()
// calculate the end address by performing bitwise OR with the complement of the mask.
slice := start.AsSlice()
for i := 0; i < len(slice); i++ {
slice[i] |= ^mask[i]
}
end, _ = netip.AddrFromSlice(slice)
return start, end
}