utils_linux.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/vishvananda/netlink"
  21. "github.com/containernetworking/cni/pkg/types"
  22. current "github.com/containernetworking/cni/pkg/types/100"
  23. "github.com/containernetworking/plugins/pkg/netlinksafe"
  24. )
  25. func ValidateExpectedInterfaceIPs(ifName string, resultIPs []*current.IPConfig) error {
  26. // Ensure ips
  27. for _, ips := range resultIPs {
  28. ourAddr := netlink.Addr{IPNet: &ips.Address}
  29. match := false
  30. link, err := netlinksafe.LinkByName(ifName)
  31. if err != nil {
  32. return fmt.Errorf("Cannot find container link %v", ifName)
  33. }
  34. addrList, err := netlinksafe.AddrList(link, netlink.FAMILY_ALL)
  35. if err != nil {
  36. return fmt.Errorf("Cannot obtain List of IP Addresses")
  37. }
  38. for _, addr := range addrList {
  39. if addr.Equal(ourAddr) {
  40. match = true
  41. break
  42. }
  43. }
  44. if !match {
  45. return fmt.Errorf("Failed to match addr %v on interface %v", ourAddr, ifName)
  46. }
  47. // Convert the host/prefixlen to just prefix for route lookup.
  48. _, ourPrefix, err := net.ParseCIDR(ourAddr.String())
  49. if err != nil {
  50. return err
  51. }
  52. findGwy := &netlink.Route{Dst: ourPrefix}
  53. routeFilter := netlink.RT_FILTER_DST
  54. family := netlink.FAMILY_V6
  55. if ips.Address.IP.To4() != nil {
  56. family = netlink.FAMILY_V4
  57. }
  58. gwy, err := netlinksafe.RouteListFiltered(family, findGwy, routeFilter)
  59. if err != nil {
  60. return fmt.Errorf("Error %v trying to find Gateway %v for interface %v", err, ips.Gateway, ifName)
  61. }
  62. if gwy == nil {
  63. return fmt.Errorf("Failed to find Gateway %v for interface %v", ips.Gateway, ifName)
  64. }
  65. }
  66. return nil
  67. }
  68. func ValidateExpectedRoute(resultRoutes []*types.Route) error {
  69. // Ensure that each static route in prevResults is found in the routing table
  70. for _, route := range resultRoutes {
  71. find := &netlink.Route{Dst: &route.Dst, Gw: route.GW}
  72. routeFilter := netlink.RT_FILTER_DST
  73. if route.GW != nil {
  74. routeFilter |= netlink.RT_FILTER_GW
  75. }
  76. var family int
  77. switch {
  78. case route.Dst.IP.To4() != nil:
  79. family = netlink.FAMILY_V4
  80. // Default route needs Dst set to nil
  81. if route.Dst.String() == "0.0.0.0/0" {
  82. find = &netlink.Route{Dst: nil, Gw: route.GW}
  83. routeFilter = netlink.RT_FILTER_DST
  84. }
  85. case len(route.Dst.IP) == net.IPv6len:
  86. family = netlink.FAMILY_V6
  87. // Default route needs Dst set to nil
  88. if route.Dst.String() == "::/0" {
  89. find = &netlink.Route{Dst: nil, Gw: route.GW}
  90. routeFilter = netlink.RT_FILTER_DST
  91. }
  92. default:
  93. return fmt.Errorf("Invalid static route found %v", route)
  94. }
  95. wasFound, err := netlinksafe.RouteListFiltered(family, find, routeFilter)
  96. if err != nil {
  97. return fmt.Errorf("Expected Route %v not route table lookup error %v", route, err)
  98. }
  99. if wasFound == nil {
  100. return fmt.Errorf("Expected Route %v not found in routing table", route)
  101. }
  102. }
  103. return nil
  104. }