ipip.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 the Kilo 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. package encapsulation
  15. import (
  16. "fmt"
  17. "net"
  18. "github.com/squat/kilo/pkg/iproute"
  19. "github.com/squat/kilo/pkg/iptables"
  20. )
  21. type ipip struct {
  22. iface int
  23. strategy Strategy
  24. }
  25. // NewIPIP returns an encapsulator that uses IPIP.
  26. func NewIPIP(strategy Strategy) Encapsulator {
  27. return &ipip{strategy: strategy}
  28. }
  29. // CleanUp will remove any created IPIP devices.
  30. func (i *ipip) CleanUp() error {
  31. if err := iproute.DeleteAddresses(i.iface); err != nil {
  32. return nil
  33. }
  34. return iproute.RemoveInterface(i.iface)
  35. }
  36. // Gw returns the correct gateway IP associated with the given node.
  37. func (i *ipip) Gw(_, internal, _ net.IP, _ *net.IPNet) net.IP {
  38. return internal
  39. }
  40. // CNICompatibilityIP is a no-op for IPIP.
  41. func (i *ipip) CNICompatibilityIP() *net.IPNet {
  42. return nil
  43. }
  44. // Index returns the index of the IPIP interface.
  45. func (i *ipip) Index() int {
  46. return i.iface
  47. }
  48. // Init initializes the IPIP interface.
  49. func (i *ipip) Init(base int) error {
  50. iface, err := iproute.NewIPIP(base)
  51. if err != nil {
  52. return fmt.Errorf("failed to create tunnel interface: %v", err)
  53. }
  54. if err := iproute.Set(iface, true); err != nil {
  55. return fmt.Errorf("failed to set tunnel interface up: %v", err)
  56. }
  57. i.iface = iface
  58. return nil
  59. }
  60. // Rules returns a set of iptables rules that are necessary
  61. // when traffic between nodes must be encapsulated.
  62. func (i *ipip) Rules(nodes []*net.IPNet) iptables.RuleSet {
  63. rules := iptables.RuleSet{}
  64. proto := ipipProtocolName()
  65. rules.AddToAppend(iptables.NewIPv4Chain("filter", "KILO-IPIP"))
  66. rules.AddToAppend(iptables.NewIPv6Chain("filter", "KILO-IPIP"))
  67. rules.AddToAppend(iptables.NewIPv4Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-j", "KILO-IPIP"))
  68. rules.AddToAppend(iptables.NewIPv6Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-j", "KILO-IPIP"))
  69. for _, n := range nodes {
  70. // Accept encapsulated traffic from peers.
  71. rules.AddToPrepend(iptables.NewRule(iptables.GetProtocol(n.IP), "filter", "KILO-IPIP", "-s", n.String(), "-m", "comment", "--comment", "Kilo: allow IPIP traffic", "-j", "ACCEPT"))
  72. }
  73. // Drop all other IPIP traffic.
  74. rules.AddToAppend(iptables.NewIPv4Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-j", "DROP"))
  75. rules.AddToAppend(iptables.NewIPv6Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-j", "DROP"))
  76. return rules
  77. }
  78. // Set sets the IP address of the IPIP interface.
  79. func (i *ipip) Set(cidr *net.IPNet) error {
  80. return iproute.SetAddress(i.iface, cidr)
  81. }
  82. // Strategy returns the configured strategy for encapsulation.
  83. func (i *ipip) Strategy() Strategy {
  84. return i.strategy
  85. }