2
0

ipip.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/kilo-io/kilo/pkg/iproute"
  19. "github.com/kilo-io/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. // Index returns the index of the IPIP interface.
  41. func (i *ipip) Index() int {
  42. return i.iface
  43. }
  44. // Init initializes the IPIP interface.
  45. func (i *ipip) Init(base int) error {
  46. iface, err := iproute.NewIPIP(base)
  47. if err != nil {
  48. return fmt.Errorf("failed to create tunnel interface: %v", err)
  49. }
  50. if err := iproute.Set(iface, true); err != nil {
  51. return fmt.Errorf("failed to set tunnel interface up: %v", err)
  52. }
  53. i.iface = iface
  54. return nil
  55. }
  56. // Rules returns a set of iptables rules that are necessary
  57. // when traffic between nodes must be encapsulated.
  58. func (i *ipip) Rules(nodes []*net.IPNet) []iptables.Rule {
  59. var rules []iptables.Rule
  60. proto := ipipProtocolName()
  61. rules = append(rules, iptables.NewIPv4Chain("filter", "KILO-IPIP"))
  62. rules = append(rules, iptables.NewIPv6Chain("filter", "KILO-IPIP"))
  63. rules = append(rules, iptables.NewIPv4Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-j", "KILO-IPIP"))
  64. rules = append(rules, iptables.NewIPv6Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-j", "KILO-IPIP"))
  65. for _, n := range nodes {
  66. // Accept encapsulated traffic from peers.
  67. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(n.IP)), "filter", "KILO-IPIP", "-s", n.String(), "-m", "comment", "--comment", "Kilo: allow IPIP traffic", "-j", "ACCEPT"))
  68. }
  69. // Drop all other IPIP traffic.
  70. rules = append(rules, iptables.NewIPv4Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-j", "DROP"))
  71. rules = append(rules, iptables.NewIPv6Rule("filter", "INPUT", "-p", proto, "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-j", "DROP"))
  72. return rules
  73. }
  74. // Set sets the IP address of the IPIP interface.
  75. func (i *ipip) Set(cidr *net.IPNet) error {
  76. return iproute.SetAddress(i.iface, cidr)
  77. }
  78. // Strategy returns the configured strategy for encapsulation.
  79. func (i *ipip) Strategy() Strategy {
  80. return i.strategy
  81. }