-
Notifications
You must be signed in to change notification settings - Fork 614
/
netutil_unix.go
332 lines (310 loc) · 10.3 KB
/
netutil_unix.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//go:build freebsd || linux
/*
Copyright The containerd Authors.
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 netutil
import (
"bytes"
"encoding/json"
"fmt"
"net"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/containerd/log"
"github.com/containerd/nerdctl/v2/pkg/defaults"
"github.com/containerd/nerdctl/v2/pkg/strutil"
"github.com/containerd/nerdctl/v2/pkg/systemutil"
"github.com/mitchellh/mapstructure"
"github.com/vishvananda/netlink"
)
const (
DefaultNetworkName = "bridge"
DefaultCIDR = "10.4.0.0/24"
DefaultIPAMDriver = "host-local"
// When creating non-default network without passing in `--subnet` option,
// nerdctl assigns subnet address for the creation starting from `StartingCIDR`
// This prevents subnet address overlapping with `DefaultCIDR` used by the default network
StartingCIDR = "10.4.1.0/24"
)
func (n *NetworkConfig) subnets() []*net.IPNet {
var subnets []*net.IPNet
if len(n.Plugins) > 0 && n.Plugins[0].Network.Type == "bridge" {
var bridge bridgeConfig
if err := json.Unmarshal(n.Plugins[0].Bytes, &bridge); err != nil {
return subnets
}
if bridge.IPAM["type"] != "host-local" {
return subnets
}
var ipam hostLocalIPAMConfig
if err := mapstructure.Decode(bridge.IPAM, &ipam); err != nil {
return subnets
}
for _, irange := range ipam.Ranges {
if len(irange) > 0 {
_, subnet, err := net.ParseCIDR(irange[0].Subnet)
if err != nil {
continue
}
subnets = append(subnets, subnet)
}
}
}
return subnets
}
func (n *NetworkConfig) clean() error {
// Remove the bridge network interface on the host.
if len(n.Plugins) > 0 && n.Plugins[0].Network.Type == "bridge" {
var bridge bridgeConfig
if err := json.Unmarshal(n.Plugins[0].Bytes, &bridge); err != nil {
return err
}
return removeBridgeNetworkInterface(bridge.BrName)
}
return nil
}
func (e *CNIEnv) generateCNIPlugins(driver string, name string, ipam map[string]interface{}, opts map[string]string, ipv6 bool) ([]CNIPlugin, error) {
var (
plugins []CNIPlugin
err error
)
switch driver {
case "bridge":
mtu := 0
iPMasq := true
for opt, v := range opts {
switch opt {
case "mtu", "com.docker.network.driver.mtu":
mtu, err = ParseMTU(v)
if err != nil {
return nil, err
}
case "ip-masq", "com.docker.network.bridge.enable_ip_masquerade":
iPMasq, err = strconv.ParseBool(v)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
}
var bridge *bridgeConfig
if name == DefaultNetworkName {
bridge = newBridgePlugin("nerdctl0")
} else {
bridge = newBridgePlugin("br-" + networkID(name)[:12])
}
bridge.MTU = mtu
bridge.IPAM = ipam
bridge.IsGW = true
bridge.IPMasq = iPMasq
bridge.HairpinMode = true
if ipv6 {
bridge.Capabilities["ips"] = true
}
plugins = []CNIPlugin{bridge, newPortMapPlugin(), newFirewallPlugin(), newTuningPlugin()}
plugins = fixUpIsolation(e, name, plugins)
case "macvlan", "ipvlan":
mtu := 0
mode := ""
master := ""
for opt, v := range opts {
switch opt {
case "mtu", "com.docker.network.driver.mtu":
mtu, err = ParseMTU(v)
if err != nil {
return nil, err
}
case "mode", "macvlan_mode", "ipvlan_mode":
if driver == "macvlan" && opt != "ipvlan_mode" {
if !strutil.InStringSlice([]string{"bridge"}, v) {
return nil, fmt.Errorf("unknown macvlan mode %q", v)
}
} else if driver == "ipvlan" && opt != "macvlan_mode" {
if !strutil.InStringSlice([]string{"l2", "l3"}, v) {
return nil, fmt.Errorf("unknown ipvlan mode %q", v)
}
} else {
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
mode = v
case "parent":
master = v
default:
return nil, fmt.Errorf("unsupported %q network option %q", driver, opt)
}
}
vlan := newVLANPlugin(driver)
vlan.MTU = mtu
vlan.Master = master
vlan.Mode = mode
vlan.IPAM = ipam
if ipv6 {
vlan.Capabilities["ips"] = true
}
plugins = []CNIPlugin{vlan}
default:
return nil, fmt.Errorf("unsupported cni driver %q", driver)
}
return plugins, nil
}
func (e *CNIEnv) generateIPAM(driver string, subnets []string, gatewayStr, ipRangeStr string, opts map[string]string, ipv6 bool) (map[string]interface{}, error) {
var ipamConfig interface{}
switch driver {
case "default", "host-local":
ipamConf := newHostLocalIPAMConfig()
ipamConf.Routes = []IPAMRoute{
{Dst: "0.0.0.0/0"},
}
ranges, findIPv4, err := e.parseIPAMRanges(subnets, gatewayStr, ipRangeStr, ipv6)
if err != nil {
return nil, err
}
ipamConf.Ranges = append(ipamConf.Ranges, ranges...)
if !findIPv4 {
ranges, _, _ = e.parseIPAMRanges([]string{""}, gatewayStr, ipRangeStr, ipv6)
ipamConf.Ranges = append(ipamConf.Ranges, ranges...)
}
ipamConfig = ipamConf
case "dhcp":
ipamConf := newDHCPIPAMConfig()
ipamConf.DaemonSocketPath = filepath.Join(defaults.CNIRuntimeDir(), "dhcp.sock")
// TODO: support IPAM options for dhcp
if err := systemutil.IsSocketAccessible(ipamConf.DaemonSocketPath); err != nil {
log.L.Warnf("cannot access dhcp socket %q (hint: try running with `dhcp daemon --socketpath=%s &` in CNI_PATH to launch the dhcp daemon)", ipamConf.DaemonSocketPath, ipamConf.DaemonSocketPath)
}
ipamConfig = ipamConf
default:
return nil, fmt.Errorf("unsupported ipam driver %q", driver)
}
ipam, err := structToMap(ipamConfig)
if err != nil {
return nil, err
}
return ipam, nil
}
func (e *CNIEnv) parseIPAMRanges(subnets []string, gateway, ipRange string, ipv6 bool) ([][]IPAMRange, bool, error) {
findIPv4 := false
ranges := make([][]IPAMRange, 0, len(subnets))
for i := range subnets {
subnet, err := e.parseSubnet(subnets[i])
if err != nil {
return nil, findIPv4, err
}
// if ipv6 flag is not set, subnets of ipv6 should be excluded
if !ipv6 && subnet.IP.To4() == nil {
continue
}
if !findIPv4 && subnet.IP.To4() != nil {
findIPv4 = true
}
ipamRange, err := parseIPAMRange(subnet, gateway, ipRange)
if err != nil {
return nil, findIPv4, err
}
ranges = append(ranges, []IPAMRange{*ipamRange})
}
return ranges, findIPv4, nil
}
func fixUpIsolation(e *CNIEnv, name string, plugins []CNIPlugin) []CNIPlugin {
isolationPath := filepath.Join(e.Path, "isolation")
if _, err := exec.LookPath(isolationPath); err == nil {
// the warning is suppressed for DefaultNetworkName (because multi-bridge networking is not involved)
if name != DefaultNetworkName {
log.L.Warnf(`network %q: Using the deprecated CNI "isolation" plugin instead of CNI "firewall" plugin (>= 1.1.0) ingressPolicy.
To dismiss this warning, uninstall %q and install CNI "firewall" plugin (>= 1.1.0) from https://github.com/containernetworking/plugins`,
name, isolationPath)
}
plugins = append(plugins, newIsolationPlugin())
for _, f := range plugins {
if x, ok := f.(*firewallConfig); ok {
if name != DefaultNetworkName {
log.L.Warnf("network %q: Unsetting firewall ingressPolicy %q (because using the deprecated \"isolation\" plugin)", name, x.IngressPolicy)
}
x.IngressPolicy = ""
}
}
} else if name != DefaultNetworkName {
firewallPath := filepath.Join(e.Path, "firewall")
ok, err := firewallPluginGEQ110(firewallPath)
if err != nil {
log.L.WithError(err).Warnf("Failed to detect whether %q is newer than v1.1.0", firewallPath)
}
if !ok {
log.L.Warnf("To isolate bridge networks, CNI plugin \"firewall\" (>= 1.1.0) needs to be installed in CNI_PATH (%q), see https://github.com/containernetworking/plugins",
e.Path)
}
}
return plugins
}
func firewallPluginGEQ110(firewallPath string) (bool, error) {
// TODO: guess true by default in 2023
guessed := false
// Parse the stderr (NOT stdout) of `firewall`, such as "CNI firewall plugin v1.1.0\n", or "CNI firewall plugin version unknown\n"
//
// We do NOT set `CNI_COMMAND=VERSION` here, because the CNI "VERSION" command reports the version of the CNI spec,
// not the version of the firewall plugin implementation.
//
// ```
// $ /opt/cni/bin/firewall
// CNI firewall plugin v1.1.0
// $ CNI_COMMAND=VERSION /opt/cni/bin/firewall
// {"cniVersion":"1.0.0","supportedVersions":["0.4.0","1.0.0"]}
// ```
//
cmd := exec.Command(firewallPath)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
err = fmt.Errorf("failed to run %v: %w (stdout=%q, stderr=%q)", cmd.Args, err, stdout.String(), stderr.String())
return guessed, err
}
ver, err := guessFirewallPluginVersion(stderr.String()) // NOT stdout
if err != nil {
return guessed, fmt.Errorf("failed to guess the version of %q: %w", firewallPath, err)
}
ver110 := semver.MustParse("v1.1.0")
return ver.GreaterThan(ver110) || ver.Equal(ver110), nil
}
// guesssFirewallPluginVersion guess the version of the CNI firewall plugin (not the version of the implemented CNI spec).
//
// stderr is like "CNI firewall plugin v1.1.0\n", or "CNI firewall plugin version unknown\n"
func guessFirewallPluginVersion(stderr string) (*semver.Version, error) {
const prefix = "CNI firewall plugin "
lines := strings.Split(stderr, "\n")
for i, l := range lines {
trimmed := strings.TrimPrefix(l, prefix)
if trimmed == l { // l does not have the expected prefix
continue
}
// trimmed is like "v1.1.1", "v1.1.0", ..., "v0.8.0", or "version unknown"
ver, err := semver.NewVersion(trimmed)
if err != nil {
return nil, fmt.Errorf("failed to parse %q (line %d of stderr=%q) as a semver: %w", trimmed, i+1, stderr, err)
}
return ver, nil
}
return nil, fmt.Errorf("stderr %q does not have any line that starts with %q", stderr, prefix)
}
func removeBridgeNetworkInterface(netIf string) error {
link, err := netlink.LinkByName(netIf)
if err == nil {
if err := netlink.LinkDel(link); err != nil {
return fmt.Errorf("failed to remove network interface %s: %v", netIf, err)
}
}
return nil
}