ipip.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // 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. rules = append(rules, iptables.NewIPv4Chain("filter", "KILO-IPIP"))
  61. rules = append(rules, iptables.NewIPv6Chain("filter", "KILO-IPIP"))
  62. rules = append(rules, iptables.NewIPv4Rule("filter", "INPUT", "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-p", "4", "-j", "KILO-IPIP"))
  63. rules = append(rules, iptables.NewIPv6Rule("filter", "INPUT", "-m", "comment", "--comment", "Kilo: jump to IPIP chain", "-p", "4", "-j", "KILO-IPIP"))
  64. for _, n := range nodes {
  65. // Accept encapsulated traffic from peers.
  66. rules = append(rules, iptables.NewRule(iptables.GetProtocol(len(n.IP)), "filter", "KILO-IPIP", "-m", "comment", "--comment", "Kilo: allow IPIP traffic", "-s", n.IP.String(), "-j", "ACCEPT"))
  67. }
  68. // Drop all other IPIP traffic.
  69. rules = append(rules, iptables.NewIPv4Rule("filter", "INPUT", "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-p", "4", "-j", "DROP"))
  70. rules = append(rules, iptables.NewIPv6Rule("filter", "INPUT", "-m", "comment", "--comment", "Kilo: reject other IPIP traffic", "-p", "4", "-j", "DROP"))
  71. return rules
  72. }
  73. // Set sets the IP address of the IPIP interface.
  74. func (i *ipip) Set(cidr *net.IPNet) error {
  75. return iproute.SetAddress(i.iface, cidr)
  76. }
  77. // Strategy returns the configured strategy for encapsulation.
  78. func (i *ipip) Strategy() Strategy {
  79. return i.strategy
  80. }