| 1 | //go:build linux |
| 2 | // +build linux |
| 3 | |
| 4 | // Copyright 2016 CNI authors |
| 5 | // |
| 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | // you may not use this file except in compliance with the License. |
| 8 | // You may obtain a copy of the License at |
| 9 | // |
| 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | // |
| 12 | // Unless required by applicable law or agreed to in writing, software |
| 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | // See the License for the specific language governing permissions and |
| 16 | // limitations under the License. |
| 17 | |
| 18 | package ip |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "net" |
| 23 | |
| 24 | "github.com/vishvananda/netlink" |
| 25 | |
| 26 | "github.com/containernetworking/cni/pkg/types" |
| 27 | current "github.com/containernetworking/cni/pkg/types/100" |
| 28 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 29 | ) |
| 30 | |
| 31 | func ValidateExpectedInterfaceIPs(ifName string, resultIPs []*current.IPConfig) error { |
| 32 | // Ensure ips |
| 33 | for _, ips := range resultIPs { |
| 34 | ourAddr := netlink.Addr{IPNet: &ips.Address} |
| 35 | match := false |
| 36 | |
| 37 | link, err := netlinksafe.LinkByName(ifName) |
| 38 | if err != nil { |
| 39 | return fmt.Errorf("Cannot find container link %v", ifName) |
| 40 | } |
| 41 | |
| 42 | addrList, err := netlinksafe.AddrList(link, netlink.FAMILY_ALL) |
| 43 | if err != nil { |
| 44 | return fmt.Errorf("Cannot obtain List of IP Addresses") |
| 45 | } |
| 46 | |
| 47 | for _, addr := range addrList { |
| 48 | if addr.Equal(ourAddr) { |
| 49 | match = true |
| 50 | break |
| 51 | } |
| 52 | } |
| 53 | if !match { |
| 54 | return fmt.Errorf("Failed to match addr %v on interface %v", ourAddr, ifName) |
| 55 | } |
| 56 | |
| 57 | // Convert the host/prefixlen to just prefix for route lookup. |
| 58 | _, ourPrefix, err := net.ParseCIDR(ourAddr.String()) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | findGwy := &netlink.Route{Dst: ourPrefix} |
| 64 | routeFilter := netlink.RT_FILTER_DST |
| 65 | |
| 66 | family := netlink.FAMILY_V6 |
| 67 | if ips.Address.IP.To4() != nil { |
| 68 | family = netlink.FAMILY_V4 |
| 69 | } |
| 70 | |
| 71 | gwy, err := netlinksafe.RouteListFiltered(family, findGwy, routeFilter) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("Error %v trying to find Gateway %v for interface %v", err, ips.Gateway, ifName) |
| 74 | } |
| 75 | if gwy == nil { |
| 76 | return fmt.Errorf("Failed to find Gateway %v for interface %v", ips.Gateway, ifName) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | func ValidateExpectedRoute(resultRoutes []*types.Route) error { |
| 84 | // Ensure that each static route in prevResults is found in the routing table |
| 85 | for _, route := range resultRoutes { |
| 86 | find := &netlink.Route{Dst: &route.Dst, Gw: route.GW} |
| 87 | routeFilter := netlink.RT_FILTER_DST |
| 88 | if route.GW != nil { |
| 89 | routeFilter |= netlink.RT_FILTER_GW |
| 90 | } |
| 91 | var family int |
| 92 | |
| 93 | switch { |
| 94 | case route.Dst.IP.To4() != nil: |
| 95 | family = netlink.FAMILY_V4 |
| 96 | // Default route needs Dst set to nil |
| 97 | if route.Dst.String() == "0.0.0.0/0" { |
| 98 | find = &netlink.Route{Dst: nil, Gw: route.GW} |
| 99 | routeFilter = netlink.RT_FILTER_DST |
| 100 | } |
| 101 | case len(route.Dst.IP) == net.IPv6len: |
| 102 | family = netlink.FAMILY_V6 |
| 103 | // Default route needs Dst set to nil |
| 104 | if route.Dst.String() == "::/0" { |
| 105 | find = &netlink.Route{Dst: nil, Gw: route.GW} |
| 106 | routeFilter = netlink.RT_FILTER_DST |
| 107 | } |
| 108 | default: |
| 109 | return fmt.Errorf("Invalid static route found %v", route) |
| 110 | } |
| 111 | |
| 112 | wasFound, err := netlinksafe.RouteListFiltered(family, find, routeFilter) |
| 113 | if err != nil { |
| 114 | return fmt.Errorf("Expected Route %v not route table lookup error %v", route, err) |
| 115 | } |
| 116 | if wasFound == nil { |
| 117 | return fmt.Errorf("Expected Route %v not found in routing table", route) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return nil |
| 122 | } |