traffic.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package iptables
  14. import (
  15. "fmt"
  16. "net"
  17. "k8s.io/klog"
  18. utiliptables "k8s.io/kubernetes/pkg/util/iptables"
  19. utilnet "k8s.io/utils/net"
  20. )
  21. // LocalTrafficDetector in a interface to take action (jump) based on whether traffic originated locally
  22. // at the node or not
  23. type LocalTrafficDetector interface {
  24. // IsImplemented returns true if the implementation does something, false otherwise
  25. IsImplemented() bool
  26. // JumpIfLocal appends conditions to jump to a target chain if traffic detected to be
  27. // of local origin
  28. JumpIfLocal(args []string, toChain string) []string
  29. // JumpINotfLocal appends conditions to jump to a target chain if traffic detected not to be
  30. // of local origin
  31. JumpIfNotLocal(args []string, toChain string) []string
  32. }
  33. type noOpLocalDetector struct{}
  34. // NewNoOpLocalDetector is a no-op implementation of LocalTrafficDetector
  35. func NewNoOpLocalDetector() LocalTrafficDetector {
  36. return &noOpLocalDetector{}
  37. }
  38. func (n *noOpLocalDetector) IsImplemented() bool {
  39. return false
  40. }
  41. func (n *noOpLocalDetector) JumpIfLocal(args []string, toChain string) []string {
  42. return args // no-op
  43. }
  44. func (n *noOpLocalDetector) JumpIfNotLocal(args []string, toChain string) []string {
  45. return args // no-op
  46. }
  47. type detectLocalByCIDR struct {
  48. cidr string
  49. }
  50. // NewDetectLocalByCIDR implements the LocalTrafficDetector interface using a CIDR. This can be used when a single CIDR
  51. // range can be used to capture the notion of local traffic.
  52. func NewDetectLocalByCIDR(cidr string, ipt utiliptables.Interface) (LocalTrafficDetector, error) {
  53. if utilnet.IsIPv6CIDRString(cidr) != ipt.IsIpv6() {
  54. return nil, fmt.Errorf("CIDR %s has incorrect IP version: expect isIPv6=%t", cidr, ipt.IsIpv6())
  55. }
  56. _, _, err := net.ParseCIDR(cidr)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return &detectLocalByCIDR{cidr: cidr}, nil
  61. }
  62. func (d *detectLocalByCIDR) IsImplemented() bool {
  63. return true
  64. }
  65. func (d *detectLocalByCIDR) JumpIfLocal(args []string, toChain string) []string {
  66. line := append(args, "-s", d.cidr, "-j", toChain)
  67. klog.V(4).Info("[DetectLocalByCIDR (", d.cidr, ")", " Jump Local: ", line)
  68. return line
  69. }
  70. func (d *detectLocalByCIDR) JumpIfNotLocal(args []string, toChain string) []string {
  71. line := append(args, "!", "-s", d.cidr, "-j", toChain)
  72. klog.V(4).Info("[DetectLocalByCIDR (", d.cidr, ")]", " Jump Not Local: ", line)
  73. return line
  74. }