| 1 | // Copyright 2017 CNI authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package allocatorscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 19 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 20 | |
| 21 | "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/" |
| 22 | "github.com/containernetworking/plugins/pkg/ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/" |
| 23 | ) |
| 24 | |
| 25 | // Canonicalize takes a given range and ensures that all information is consistent, |
| 26 | // filling out Start, End, and Gateway with sane values if missing |
| 27 | func (r *Rangescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#) Canonicalizescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Canonicalize().() error { |
| 28 | if err := canonicalizeIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/canonicalizeIP().(&r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP); err != nil { |
| 29 | return err |
| 30 | } |
| 31 | |
| 32 | // Can't create an allocator for a network with no addresses, eg |
| 33 | // a /32 or /31 |
| 34 | ones, masklen := r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..Mask.Sizescip-go gomod github.com/golang/go/src go1.24.2 net/IPMask#Size().() |
| 35 | if ones > masklen-2 { |
| 36 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Network %s too small to allocate from", (*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#)(&r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet.).Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().()) |
| 37 | } |
| 38 | |
| 39 | if len(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP) != len(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..Mask) { |
| 40 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("IPNet IP and Mask version mismatch") |
| 41 | } |
| 42 | |
| 43 | // Ensure Subnet IP is the network address, not some other address |
| 44 | networkIP := r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP.Maskscip-go gomod github.com/golang/go/src go1.24.2 net/IP#Mask().(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..Mask) |
| 45 | if !r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP.Equalscip-go gomod github.com/golang/go/src go1.24.2 net/IP#Equal().(networkIP) { |
| 46 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("Network has host bits set. For a subnet mask of length %d the network address is %s", ones, networkIP.Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().()) |
| 47 | } |
| 48 | |
| 49 | // If the gateway is nil, claim .1 |
| 50 | if r.Gatewayscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Gateway. == nil { |
| 51 | r.Gatewayscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Gateway. = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.NextIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/NextIP().(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP) |
| 52 | } else { |
| 53 | if err := canonicalizeIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/canonicalizeIP().(&r.Gatewayscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Gateway.); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // RangeStart: If specified, make sure it's sane (inside the subnet), |
| 59 | // otherwise use the first free IP (i.e. .1) - this will conflict with the |
| 60 | // gateway but we skip it in the iterator |
| 61 | if r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart. != nil { |
| 62 | if err := canonicalizeIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/canonicalizeIP().(&r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | if !r.Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.) { |
| 67 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("RangeStart %s not in network %s", r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), (*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#)(&r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet.).Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().()) |
| 68 | } |
| 69 | } else { |
| 70 | r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart. = ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.NextIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/NextIP().(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP) |
| 71 | } |
| 72 | |
| 73 | // RangeEnd: If specified, verify sanity. Otherwise, add a sensible default |
| 74 | // (e.g. for a /24: .254 if IPv4, ::255 if IPv6) |
| 75 | if r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd. != nil { |
| 76 | if err := canonicalizeIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/canonicalizeIP().(&r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd.); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | if !r.Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd.) { |
| 81 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("RangeEnd %s not in network %s", r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), (*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#)(&r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet.).Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#String().()) |
| 82 | } |
| 83 | } else { |
| 84 | r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd. = lastIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/lastIP().(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet.) |
| 85 | } |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | // IsValidIP checks if a given ip is a valid, allocatable address in a given Range |
| 91 | func (r *Rangescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#) Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(addr netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP#) bool { |
| 92 | if err := canonicalizeIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/canonicalizeIP().(&addr); err != nil { |
| 93 | return false |
| 94 | } |
| 95 | |
| 96 | subnet := (netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#)(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet.) |
| 97 | |
| 98 | // Not the same address family |
| 99 | if len(addr) != len(r.Subnetscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Subnet..IP) { |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | // Not in network |
| 104 | if !subnet.Containsscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#Contains().(addr) { |
| 105 | return false |
| 106 | } |
| 107 | |
| 108 | // We ignore nils here so we can use this function as we initialize the range. |
| 109 | if r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart. != nil { |
| 110 | // Before the range start |
| 111 | if ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.Cmpscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/Cmp().(addr, r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.) < 0 { |
| 112 | return false |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd. != nil { |
| 117 | if ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/.Cmpscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/Cmp().(addr, r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd.) > 0 { |
| 118 | // After the range end |
| 119 | return false |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return true |
| 124 | } |
| 125 | |
| 126 | // Overlaps returns true if there is any overlap between ranges |
| 127 | func (r *Rangescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#) Overlapsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Overlaps().(r1 *Rangescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#) bool { |
| 128 | // different families |
| 129 | if len(r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.) != len(r1.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.) { |
| 130 | return false |
| 131 | } |
| 132 | |
| 133 | return r.Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(r1.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.) || |
| 134 | r.Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(r1.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd.) || |
| 135 | r1.Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart.) || |
| 136 | r1.Containsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#Contains().(r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd.) |
| 137 | } |
| 138 | |
| 139 | func (r *Rangescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#) Stringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#String().() string { |
| 140 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("%s-%s", r.RangeStartscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeStart..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().(), r.RangeEndscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/Range#RangeEnd..Stringscip-go gomod github.com/golang/go/src go1.24.2 net/IP#String().()) |
| 141 | } |
| 142 | |
| 143 | // canonicalizeIP makes sure a provided ip is in standard form |
| 144 | func canonicalizeIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/canonicalizeIP().(ip *netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP#) error { |
| 145 | if ip.To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() != nil { |
| 146 | *ip = ip.To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() |
| 147 | return nil |
| 148 | } else if ip.To16scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To16().() != nil { |
| 149 | *ip = ip.To16scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To16().() |
| 150 | return nil |
| 151 | } |
| 152 | return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("IP %s not v4 nor v6", *ip) |
| 153 | } |
| 154 | |
| 155 | // Determine the last IP of a subnet, excluding the broadcast if IPv4 |
| 156 | func lastIPscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator`/lastIP().(subnet typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.IPNetscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPNet#) netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP# { |
| 157 | var end netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPscip-go gomod github.com/golang/go/src go1.24.2 net/IP# |
| 158 | for i := 0; i < len(subnet.IP); i++ { |
| 159 | end = append(end, subnet.IP[i]|^subnet.Mask[i]) |
| 160 | } |
| 161 | if subnet.IP.To4scip-go gomod github.com/golang/go/src go1.24.2 net/IP#To4().() != nil { |
| 162 | end[3]-- |
| 163 | } |
| 164 | |
| 165 | return end |
| 166 | } |