| 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 allocator |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net" |
| 20 | |
| 21 | "github.com/containernetworking/cni/pkg/types" |
| 22 | "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 *Range) Canonicalize() error { |
| 28 | if err := canonicalizeIP(&r.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.Subnet.Mask.Size() |
| 35 | if ones > masklen-2 { |
| 36 | return fmt.Errorf("Network %s too small to allocate from", (*net.IPNet)(&r.Subnet).String()) |
| 37 | } |
| 38 | |
| 39 | if len(r.Subnet.IP) != len(r.Subnet.Mask) { |
| 40 | return 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.Subnet.IP.Mask(r.Subnet.Mask) |
| 45 | if !r.Subnet.IP.Equal(networkIP) { |
| 46 | return fmt.Errorf("Network has host bits set. For a subnet mask of length %d the network address is %s", ones, networkIP.String()) |
| 47 | } |
| 48 | |
| 49 | // If the gateway is nil, claim .1 |
| 50 | if r.Gateway == nil { |
| 51 | r.Gateway = ip.NextIP(r.Subnet.IP) |
| 52 | } else { |
| 53 | if err := canonicalizeIP(&r.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.RangeStart != nil { |
| 62 | if err := canonicalizeIP(&r.RangeStart); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | if !r.Contains(r.RangeStart) { |
| 67 | return fmt.Errorf("RangeStart %s not in network %s", r.RangeStart.String(), (*net.IPNet)(&r.Subnet).String()) |
| 68 | } |
| 69 | } else { |
| 70 | r.RangeStart = ip.NextIP(r.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.RangeEnd != nil { |
| 76 | if err := canonicalizeIP(&r.RangeEnd); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | if !r.Contains(r.RangeEnd) { |
| 81 | return fmt.Errorf("RangeEnd %s not in network %s", r.RangeEnd.String(), (*net.IPNet)(&r.Subnet).String()) |
| 82 | } |
| 83 | } else { |
| 84 | r.RangeEnd = lastIP(r.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 *Range) Contains(addr net.IP) bool { |
| 92 | if err := canonicalizeIP(&addr); err != nil { |
| 93 | return false |
| 94 | } |
| 95 | |
| 96 | subnet := (net.IPNet)(r.Subnet) |
| 97 | |
| 98 | // Not the same address family |
| 99 | if len(addr) != len(r.Subnet.IP) { |
| 100 | return false |
| 101 | } |
| 102 | |
| 103 | // Not in network |
| 104 | if !subnet.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.RangeStart != nil { |
| 110 | // Before the range start |
| 111 | if ip.Cmp(addr, r.RangeStart) < 0 { |
| 112 | return false |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if r.RangeEnd != nil { |
| 117 | if ip.Cmp(addr, r.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 *Range) Overlaps(r1 *Range) bool { |
| 128 | // different families |
| 129 | if len(r.RangeStart) != len(r1.RangeStart) { |
| 130 | return false |
| 131 | } |
| 132 | |
| 133 | return r.Contains(r1.RangeStart) || |
| 134 | r.Contains(r1.RangeEnd) || |
| 135 | r1.Contains(r.RangeStart) || |
| 136 | r1.Contains(r.RangeEnd) |
| 137 | } |
| 138 | |
| 139 | func (r *Range) String() string { |
| 140 | return fmt.Sprintf("%s-%s", r.RangeStart.String(), r.RangeEnd.String()) |
| 141 | } |
| 142 | |
| 143 | // canonicalizeIP makes sure a provided ip is in standard form |
| 144 | func canonicalizeIP(ip *net.IP) error { |
| 145 | if ip.To4() != nil { |
| 146 | *ip = ip.To4() |
| 147 | return nil |
| 148 | } else if ip.To16() != nil { |
| 149 | *ip = ip.To16() |
| 150 | return nil |
| 151 | } |
| 152 | return 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 lastIP(subnet types.IPNet) net.IP { |
| 157 | var end 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.To4() != nil { |
| 162 | end[3]-- |
| 163 | } |
| 164 | |
| 165 | return end |
| 166 | } |