ipip.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // +build !windows
  2. // Copyright 2017 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 ipip
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "syscall"
  20. "github.com/coreos/flannel/backend"
  21. "github.com/coreos/flannel/pkg/ip"
  22. "github.com/coreos/flannel/subnet"
  23. log "github.com/golang/glog"
  24. "github.com/vishvananda/netlink"
  25. "golang.org/x/net/context"
  26. )
  27. const (
  28. backendType = "ipip"
  29. tunnelName = "flannel.ipip"
  30. )
  31. func init() {
  32. backend.Register(backendType, New)
  33. }
  34. type IPIPBackend struct {
  35. sm subnet.Manager
  36. extIface *backend.ExternalInterface
  37. }
  38. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  39. be := &IPIPBackend{
  40. sm: sm,
  41. extIface: extIface,
  42. }
  43. return be, nil
  44. }
  45. func (be *IPIPBackend) RegisterNetwork(ctx context.Context, config *subnet.Config) (backend.Network, error) {
  46. cfg := struct {
  47. DirectRouting bool
  48. }{}
  49. if len(config.Backend) > 0 {
  50. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  51. return nil, fmt.Errorf("error decoding IPIP backend config: %v", err)
  52. }
  53. }
  54. log.Infof("IPIP config: DirectRouting=%v", cfg.DirectRouting)
  55. n := &backend.RouteNetwork{
  56. SimpleNetwork: backend.SimpleNetwork{
  57. ExtIface: be.extIface,
  58. },
  59. SM: be.sm,
  60. BackendType: backendType,
  61. }
  62. attrs := &subnet.LeaseAttrs{
  63. PublicIP: ip.FromIP(be.extIface.ExtAddr),
  64. BackendType: backendType,
  65. }
  66. l, err := be.sm.AcquireLease(ctx, attrs)
  67. switch err {
  68. case nil:
  69. n.SubnetLease = l
  70. case context.Canceled, context.DeadlineExceeded:
  71. return nil, err
  72. default:
  73. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  74. }
  75. link, err := be.configureIPIPDevice(n.SubnetLease)
  76. if err != nil {
  77. return nil, err
  78. }
  79. n.Mtu = link.MTU
  80. n.LinkIndex = link.Index
  81. n.GetRoute = func(lease *subnet.Lease) *netlink.Route {
  82. route := netlink.Route{
  83. Dst: lease.Subnet.ToIPNet(),
  84. Gw: lease.Attrs.PublicIP.ToIP(),
  85. LinkIndex: n.LinkIndex,
  86. Flags: int(netlink.FLAG_ONLINK),
  87. }
  88. if cfg.DirectRouting {
  89. dr, err := ip.DirectRouting(lease.Attrs.PublicIP.ToIP())
  90. if err != nil {
  91. log.Error(err)
  92. }
  93. if dr {
  94. log.V(2).Infof("configure route to %v via direct routing", lease.Attrs.PublicIP.String())
  95. route.LinkIndex = n.ExtIface.Iface.Index
  96. }
  97. }
  98. return &route
  99. }
  100. return n, nil
  101. }
  102. func (be *IPIPBackend) configureIPIPDevice(lease *subnet.Lease) (*netlink.Iptun, error) {
  103. // When modprobe ipip module, a tunl0 ipip device is created automatically per network namespace by ipip kernel module.
  104. // It is the namespace default IPIP device with attributes local=any and remote=any.
  105. // When receiving IPIP protocol packets, kernel will forward them to tunl0 as a fallback device
  106. // if it can't find an option whose local/remote attribute matches their src/dst ip address more precisely.
  107. // See https://github.com/torvalds/linux/blob/v4.13/net/ipv4/ip_tunnel.c#L85-L95 .
  108. // So we have two options of creating ipip device, either rename tunl0 to flannel.ipip or create an new ipip device
  109. // and set local attribute of flannel.ipip to distinguish these two devices.
  110. // Considering tunl0 might be used by users, so choose the later option.
  111. link := &netlink.Iptun{LinkAttrs: netlink.LinkAttrs{Name: tunnelName}, Local: be.extIface.IfaceAddr}
  112. if err := netlink.LinkAdd(link); err != nil {
  113. if err != syscall.EEXIST {
  114. return nil, err
  115. }
  116. // The link already exists, so check existing link attributes.
  117. existing, err := netlink.LinkByName(tunnelName)
  118. if err != nil {
  119. return nil, err
  120. }
  121. // If there's an exists device but it's not an ipip/IpTun device then get the user to fix it (flannel shouldn't
  122. // delete a user's device)
  123. if existing.Type() != "ipip" {
  124. return nil, fmt.Errorf("%v isn't an ipip mode device, please remove device and try again", tunnelName)
  125. }
  126. ipip, ok := existing.(*netlink.Iptun)
  127. if !ok {
  128. return nil, fmt.Errorf("%s isn't an iptun device (%#v), please remove device and try again", tunnelName, link)
  129. }
  130. // local attribute may change if a user changes iface configuration, we need to recreate the device to ensure
  131. // local and remote attribute is expected.
  132. // local should be equal to the extIface.IfaceAddr and remote should be nil (or equal to 0.0.0.0)
  133. if ipip.Local == nil || !ipip.Local.Equal(be.extIface.IfaceAddr) || (ipip.Remote != nil && ipip.Remote.String() != "0.0.0.0") {
  134. log.Warningf("%q already exists with incompatable attributes: local=%v remote=%v; recreating device",
  135. tunnelName, ipip.Local, ipip.Remote)
  136. if err = netlink.LinkDel(existing); err != nil {
  137. return nil, fmt.Errorf("failed to delete interface: %v", err)
  138. }
  139. if err = netlink.LinkAdd(link); err != nil {
  140. return nil, fmt.Errorf("failed to create ipip interface: %v", err)
  141. }
  142. }
  143. }
  144. // Due to the extra 20 byte IP header that the tunnel will add to each packet,
  145. // MTU size for both the workload and tunnel interfaces should be 20 bytes less than the selected iface (specified with the --iface option).
  146. expectMTU := be.extIface.Iface.MTU - 20
  147. if expectMTU <= 0 {
  148. return nil, fmt.Errorf("MTU %d of iface %s is too small for ipip mode to work", be.extIface.Iface.MTU, be.extIface.Iface.Name)
  149. }
  150. oldMTU := link.Attrs().MTU
  151. if oldMTU > expectMTU || oldMTU == 0 {
  152. log.Infof("current MTU of %s is %d, setting it to %d", tunnelName, oldMTU, expectMTU)
  153. err := netlink.LinkSetMTU(link, expectMTU)
  154. if err != nil {
  155. return nil, fmt.Errorf("failed to set %v MTU to %d: %v", tunnelName, expectMTU, err)
  156. }
  157. // change MTU as it will be written into /run/flannel/subnet.env
  158. link.Attrs().MTU = expectMTU
  159. }
  160. // Ensure that the device has a /32 address so that no broadcast routes are created.
  161. // This IP is just used as a source address for host to workload traffic (so
  162. // the return path for the traffic has an address on the flannel network to use as the destination)
  163. if err := ip.EnsureV4AddressOnLink(ip.IP4Net{IP: lease.Subnet.IP, PrefixLen: 32}, link); err != nil {
  164. return nil, fmt.Errorf("failed to ensure address of interface %s: %s", link.Attrs().Name, err)
  165. }
  166. if err := netlink.LinkSetUp(link); err != nil {
  167. return nil, fmt.Errorf("failed to set %v UP: %v", tunnelName, err)
  168. }
  169. return link, nil
  170. }