utils_linux.go 3.2 KB

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