router_windows.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // +build windows
  2. // Copyright 2015 flannel authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package routing
  16. import (
  17. "fmt"
  18. "github.com/coreos/flannel/pkg/powershell"
  19. "net"
  20. )
  21. // Router manages network routes on Windows OS using MSFT_NetRoute
  22. // See also https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/hh872448(v%3Dvs.85)
  23. type RouterWindows struct{}
  24. func (r RouterWindows) GetAllRoutes() ([]Route, error) {
  25. return parseNetRoutes("@(Get-NetRoute | Select-Object -Property IfIndex,DestinationPrefix,NextHop)")
  26. }
  27. func (r RouterWindows) GetRoutesFromInterfaceToSubnet(interfaceIndex int, destinationSubnet *net.IPNet) ([]Route, error) {
  28. return parseNetRoutes(fmt.Sprintf("@(Get-NetRoute -InterfaceIndex %d -DestinationPrefix %s | Select-Object -Property IfIndex,DestinationPrefix,NextHop)", interfaceIndex, destinationSubnet.String()))
  29. }
  30. func (r RouterWindows) CreateRoute(interfaceIndex int, destinationSubnet *net.IPNet, gatewayAddress net.IP) error {
  31. _, err := powershell.RunCommandf("New-NetRoute -InterfaceIndex %d -DestinationPrefix %s -NextHop %s", interfaceIndex, destinationSubnet.String(), gatewayAddress.String())
  32. return err
  33. }
  34. func (r RouterWindows) DeleteRoute(interfaceIndex int, destinationSubnet *net.IPNet, gatewayAddress net.IP) error {
  35. _, err := powershell.RunCommandf("Remove-NetRoute -InterfaceIndex %d -DestinationPrefix %s -NextHop %s -Verbose -Confirm:$false", interfaceIndex, destinationSubnet.String(), gatewayAddress.String())
  36. return err
  37. }
  38. type winNetRoute struct {
  39. IfIndex int
  40. DestinationPrefix string
  41. NextHop string
  42. }
  43. func parseNetRoutes(cmd string) ([]Route, error) {
  44. powerShellJsonData := make([]winNetRoute, 0)
  45. err := powershell.RunCommandWithJsonResult(cmd, &powerShellJsonData)
  46. if err != nil {
  47. return nil, err
  48. }
  49. routes := make([]Route, 0)
  50. for _, r := range powerShellJsonData {
  51. route := Route{
  52. InterfaceIndex: r.IfIndex,
  53. }
  54. _, destinationSubnet, err := net.ParseCIDR(r.DestinationPrefix)
  55. if err != nil {
  56. continue
  57. }
  58. route.DestinationSubnet = destinationSubnet
  59. gatewayAddress := net.ParseIP(r.NextHop)
  60. if gatewayAddress == nil {
  61. continue
  62. }
  63. route.GatewayAddress = gatewayAddress
  64. routes = append(routes, route)
  65. }
  66. return routes, nil
  67. }