| 1 | // Copyright 2020 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 main |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "math" |
| 20 | "net" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/vishvananda/netlink" |
| 24 | |
| 25 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 26 | ) |
| 27 | |
| 28 | // findVRF finds a VRF link with the provided name. |
| 29 | func findVRF(name string) (*netlink.Vrf, error) { |
| 30 | link, err := netlinksafe.LinkByName(name) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | vrf, ok := link.(*netlink.Vrf) |
| 35 | if !ok { |
| 36 | return nil, fmt.Errorf("Netlink %s is not a VRF", name) |
| 37 | } |
| 38 | return vrf, nil |
| 39 | } |
| 40 | |
| 41 | // createVRF creates a new VRF and sets it up. |
| 42 | func createVRF(name string, tableID uint32) (*netlink.Vrf, error) { |
| 43 | links, err := netlinksafe.LinkList() |
| 44 | if err != nil { |
| 45 | return nil, fmt.Errorf("createVRF: Failed to find links %v", err) |
| 46 | } |
| 47 | |
| 48 | if tableID == 0 { |
| 49 | tableID, err = findFreeRoutingTableID(links) |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | linkAttrs := netlink.NewLinkAttrs() |
| 56 | linkAttrs.Name = name |
| 57 | vrf := &netlink.Vrf{ |
| 58 | LinkAttrs: linkAttrs, |
| 59 | Table: tableID, |
| 60 | } |
| 61 | |
| 62 | err = netlink.LinkAdd(vrf) |
| 63 | if err != nil { |
| 64 | return nil, fmt.Errorf("could not add VRF %s: %v", name, err) |
| 65 | } |
| 66 | err = netlink.LinkSetUp(vrf) |
| 67 | if err != nil { |
| 68 | return nil, fmt.Errorf("could not set link up for VRF %s: %v", name, err) |
| 69 | } |
| 70 | |
| 71 | return vrf, nil |
| 72 | } |
| 73 | |
| 74 | // assignedInterfaces returns the list of interfaces associated to the given vrf. |
| 75 | func assignedInterfaces(vrf *netlink.Vrf) ([]netlink.Link, error) { |
| 76 | links, err := netlinksafe.LinkList() |
| 77 | if err != nil { |
| 78 | return nil, fmt.Errorf("getAssignedInterfaces: Failed to find links %v", err) |
| 79 | } |
| 80 | res := make([]netlink.Link, 0) |
| 81 | for _, l := range links { |
| 82 | if l.Attrs().MasterIndex == vrf.Index { |
| 83 | res = append(res, l) |
| 84 | } |
| 85 | } |
| 86 | return res, nil |
| 87 | } |
| 88 | |
| 89 | // addInterface adds the given interface to the VRF |
| 90 | func addInterface(vrf *netlink.Vrf, intf string) error { |
| 91 | i, err := netlinksafe.LinkByName(intf) |
| 92 | if err != nil { |
| 93 | return fmt.Errorf("could not get link by name %s", intf) |
| 94 | } |
| 95 | |
| 96 | if i.Attrs().MasterIndex != 0 { |
| 97 | master, err := netlink.LinkByIndex(i.Attrs().MasterIndex) |
| 98 | if err != nil { |
| 99 | return fmt.Errorf("interface %s has already a master set, could not retrieve the name: %v", intf, err) |
| 100 | } |
| 101 | return fmt.Errorf("interface %s has already a master set: %s", intf, master.Attrs().Name) |
| 102 | } |
| 103 | |
| 104 | // Global IPV6 addresses are not maintained unless |
| 105 | // sysctl -w net.ipv6.conf.all.keep_addr_on_down=1 is called |
| 106 | // so we save it, and restore it back. |
| 107 | beforeAddresses, err := getGlobalAddresses(i, netlink.FAMILY_V6) |
| 108 | if err != nil { |
| 109 | return fmt.Errorf("failed getting global ipv6 addresses before slaving interface: %w", err) |
| 110 | } |
| 111 | |
| 112 | // Save all routes that are not local and connected, before setting master, |
| 113 | // because otherwise those routes will be deleted after interface is moved. |
| 114 | filter := &netlink.Route{ |
| 115 | LinkIndex: i.Attrs().Index, |
| 116 | Scope: netlink.SCOPE_UNIVERSE, // Exclude local and connected routes |
| 117 | } |
| 118 | filterMask := netlink.RT_FILTER_OIF | netlink.RT_FILTER_SCOPE // Filter based on link index and scope |
| 119 | globalRoutes, err := netlinksafe.RouteListFiltered(netlink.FAMILY_ALL, filter, filterMask) |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("failed getting all routes for %s", intf) |
| 122 | } |
| 123 | |
| 124 | err = netlink.LinkSetMaster(i, vrf) |
| 125 | if err != nil { |
| 126 | return fmt.Errorf("could not set vrf %s as master of %s: %v", vrf.Name, intf, err) |
| 127 | } |
| 128 | |
| 129 | // Used to identify which global IPV6 addresses are missing |
| 130 | afterAddresses, err := getGlobalAddresses(i, netlink.FAMILY_V6) |
| 131 | if err != nil { |
| 132 | return fmt.Errorf("failed getting global ipv6 addresses after slaving interface: %w", err) |
| 133 | } |
| 134 | |
| 135 | // Since keeping the ipv6 address depends on net.ipv6.conf.all.keep_addr_on_down , |
| 136 | // we check if the new interface does not have them and in case we restore them. |
| 137 | CONTINUE: |
| 138 | for _, toFind := range beforeAddresses { |
| 139 | for _, current := range afterAddresses { |
| 140 | if toFind.Equal(current) { |
| 141 | continue CONTINUE |
| 142 | } |
| 143 | } |
| 144 | // Not found, re-adding it |
| 145 | err = netlink.AddrAdd(i, &toFind) |
| 146 | if err != nil { |
| 147 | return fmt.Errorf("could not restore address %s to %s @ %s: %v", toFind, intf, vrf.Name, err) |
| 148 | } |
| 149 | |
| 150 | // Waits for global IPV6 addresses to be added by the kernel. |
| 151 | backoffBase := 10 * time.Millisecond |
| 152 | maxRetries := 8 |
| 153 | for retryCount := 0; retryCount <= maxRetries; retryCount++ { |
| 154 | routesVRFTable, err := netlinksafe.RouteListFiltered( |
| 155 | netlink.FAMILY_ALL, |
| 156 | &netlink.Route{ |
| 157 | Dst: &net.IPNet{ |
| 158 | IP: toFind.IP, |
| 159 | Mask: net.IPMask{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 160 | }, |
| 161 | Table: int(vrf.Table), |
| 162 | LinkIndex: i.Attrs().Index, |
| 163 | }, |
| 164 | netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE|netlink.RT_FILTER_DST, |
| 165 | ) |
| 166 | if err != nil { |
| 167 | return fmt.Errorf("failed getting routes for %s table %d for dst %s: %v", intf, vrf.Table, toFind.IPNet.String(), err) |
| 168 | } |
| 169 | |
| 170 | if len(routesVRFTable) >= 1 { |
| 171 | break |
| 172 | } |
| 173 | |
| 174 | if retryCount == maxRetries { |
| 175 | return fmt.Errorf("failed getting local/host addresses for %s in table %d with dst %s", intf, vrf.Table, toFind.IPNet.String()) |
| 176 | } |
| 177 | |
| 178 | // Exponential backoff - 10ms, 20m, 40ms, 80ms, 160ms, 320ms, 640ms, 1280ms |
| 179 | // Approx 2,5 seconds total |
| 180 | time.Sleep(backoffBase * time.Duration(1<<retryCount)) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Apply all saved routes for the interface that was moved to the VRF |
| 185 | for _, route := range globalRoutes { |
| 186 | r := route |
| 187 | // Modify original table to vrf one, |
| 188 | r.Table = int(vrf.Table) |
| 189 | // equivalent of 'ip route replace <address> table <int>'. |
| 190 | err = netlink.RouteReplace(&r) |
| 191 | if err != nil { |
| 192 | return fmt.Errorf("could not add route '%s': %v", r, err) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return nil |
| 197 | } |
| 198 | |
| 199 | func findFreeRoutingTableID(links []netlink.Link) (uint32, error) { |
| 200 | takenTables := make(map[uint32]struct{}, len(links)) |
| 201 | for _, l := range links { |
| 202 | if vrf, ok := l.(*netlink.Vrf); ok { |
| 203 | takenTables[vrf.Table] = struct{}{} |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | for res := uint32(1); res < math.MaxUint32; res++ { |
| 208 | if _, ok := takenTables[res]; !ok { |
| 209 | return res, nil |
| 210 | } |
| 211 | } |
| 212 | return 0, fmt.Errorf("findFreeRoutingTableID: Failed to find an available routing id") |
| 213 | } |
| 214 | |
| 215 | func resetMaster(interfaceName string) error { |
| 216 | intf, err := netlinksafe.LinkByName(interfaceName) |
| 217 | if err != nil { |
| 218 | return fmt.Errorf("resetMaster: could not get link by name %s", interfaceName) |
| 219 | } |
| 220 | err = netlink.LinkSetNoMaster(intf) |
| 221 | if err != nil { |
| 222 | return fmt.Errorf("resetMaster: could reset master to %s", interfaceName) |
| 223 | } |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | // getGlobalAddresses returns the global addresses of the given interface |
| 228 | func getGlobalAddresses(link netlink.Link, family int) ([]netlink.Addr, error) { |
| 229 | addresses, err := netlinksafe.AddrList(link, family) |
| 230 | if err != nil { |
| 231 | return nil, fmt.Errorf("failed getting list of IP addresses for %s: %w", link.Attrs().Name, err) |
| 232 | } |
| 233 | |
| 234 | globalAddresses := make([]netlink.Addr, 0, len(addresses)) |
| 235 | for _, addr := range addresses { |
| 236 | if addr.Scope == int(netlink.SCOPE_UNIVERSE) { |
| 237 | globalAddresses = append(globalAddresses, addr) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return globalAddresses, nil |
| 242 | } |