forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 3
/
feature_detect.go
152 lines (131 loc) · 4.85 KB
/
feature_detect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2018-2019 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package iptables
import (
"io"
"regexp"
"sync"
version "github.com/hashicorp/go-version"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/versionparse"
)
var (
vXDotYDotZRegexp = regexp.MustCompile(`v(\d+\.\d+\.\d+)`)
kernelVersionRegexp = regexp.MustCompile(`Linux version (\d+\.\d+\.\d+)`)
// iptables versions:
// v1Dot4Dot7 is the oldest version we've ever supported.
v1Dot4Dot7 = versionparse.MustParseVersion("1.4.7")
// v1Dot6Dot0 added --random-fully to SNAT.
v1Dot6Dot0 = versionparse.MustParseVersion("1.6.0")
// v1Dot6Dot2 added --random-fully to MASQUERADE and the xtables lock to iptables-restore.
v1Dot6Dot2 = versionparse.MustParseVersion("1.6.2")
// Linux kernel versions:
// v3Dot10Dot0 is the oldest version we support at time of writing.
v3Dot10Dot0 = versionparse.MustParseVersion("3.10.0")
// v3Dot14Dot0 added the random-fully feature on the iptables interface.
v3Dot14Dot0 = versionparse.MustParseVersion("3.14.0")
)
type Features struct {
// SNATFullyRandom is true if --random-fully is supported by the SNAT action.
SNATFullyRandom bool
// MASQFullyRandom is true if --random-fully is supported by the MASQUERADE action.
MASQFullyRandom bool
// RestoreSupportsLock is true if the iptables-restore command supports taking the xtables lock and the
// associated -w and -W arguments.
RestoreSupportsLock bool
}
type FeatureDetector struct {
lock sync.Mutex
featureCache *Features
// Path to file with kernel version
GetKernelVersionReader func() (io.Reader, error)
// Factory for making commands, used by UTs to shim exec.Command().
NewCmd cmdFactory
}
func NewFeatureDetector() *FeatureDetector {
return &FeatureDetector{
GetKernelVersionReader: versionparse.GetKernelVersionReader,
NewCmd: newRealCmd,
}
}
func (d *FeatureDetector) GetFeatures() *Features {
d.lock.Lock()
defer d.lock.Unlock()
if d.featureCache == nil {
d.refreshFeaturesLockHeld()
}
return d.featureCache
}
func (d *FeatureDetector) RefreshFeatures() {
d.lock.Lock()
defer d.lock.Unlock()
d.refreshFeaturesLockHeld()
}
func (d *FeatureDetector) refreshFeaturesLockHeld() {
// Get the versions. If we fail to detect a version for some reason, we use a safe default.
log.Debug("Refreshing detected iptables features")
iptV := d.getIptablesVersion()
kerV := d.getKernelVersion()
// Calculate the features.
features := Features{
SNATFullyRandom: iptV.Compare(v1Dot6Dot0) >= 0 && kerV.Compare(v3Dot14Dot0) >= 0,
MASQFullyRandom: iptV.Compare(v1Dot6Dot2) >= 0 && kerV.Compare(v3Dot14Dot0) >= 0,
RestoreSupportsLock: iptV.Compare(v1Dot6Dot2) >= 0,
}
if d.featureCache == nil || *d.featureCache != features {
log.WithFields(log.Fields{
"features": features,
"kernelVersion": kerV,
"iptablesVersion": iptV,
}).Info("Updating detected iptables features")
d.featureCache = &features
}
}
func (d *FeatureDetector) getIptablesVersion() *version.Version {
cmd := d.NewCmd("iptables", "--version")
out, err := cmd.Output()
if err != nil {
log.WithError(err).Warn("Failed to get iptables version, assuming old version with no optional features")
return v1Dot4Dot7
}
s := string(out)
log.WithField("rawVersion", s).Debug("Ran iptables --version")
matches := vXDotYDotZRegexp.FindStringSubmatch(s)
if len(matches) == 0 {
log.WithField("rawVersion", s).Warn(
"Failed to parse iptables version, assuming old version with no optional features")
return v1Dot4Dot7
}
parsedVersion, err := version.NewVersion(matches[1])
if err != nil {
log.WithField("rawVersion", s).WithError(err).Warn(
"Failed to parse iptables version, assuming old version with no optional features")
return v1Dot4Dot7
}
log.WithField("version", parsedVersion).Debug("Parsed iptables version")
return parsedVersion
}
func (d *FeatureDetector) getKernelVersion() *version.Version {
reader, err := d.GetKernelVersionReader()
if err != nil {
log.WithError(err).Warn("Failed to get the kernel version reader, assuming old version with no optional features")
return v3Dot10Dot0
}
kernVersion, err := versionparse.GetKernelVersion(reader)
if err != nil {
log.WithError(err).Warn("Failed to get kernel version, assuming old version with no optional features")
return v3Dot10Dot0
}
return kernVersion
}