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
|
package network
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net"
"os"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/rootless"
"github.com/containers/podman/v2/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
var (
// BridgeNetworkDriver defines the bridge cni driver
BridgeNetworkDriver = "bridge"
// DefaultNetworkDriver is the default network type used
DefaultNetworkDriver = BridgeNetworkDriver
// MacVLANNetworkDriver defines the macvlan cni driver
MacVLANNetworkDriver = "macvlan"
)
// SupportedNetworkDrivers describes the list of supported drivers
var SupportedNetworkDrivers = []string{BridgeNetworkDriver, MacVLANNetworkDriver}
// isSupportedDriver checks if the user provided driver is supported
func isSupportedDriver(driver string) error {
if util.StringInSlice(driver, SupportedNetworkDrivers) {
return nil
}
return errors.Errorf("driver '%s' is not supported", driver)
}
// GetLiveNetworks returns a slice of networks representing what the system
// has defined as network interfaces
func GetLiveNetworks() ([]*net.IPNet, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
nets := make([]*net.IPNet, 0, len(addrs))
for _, address := range addrs {
_, n, err := net.ParseCIDR(address.String())
if err != nil {
return nil, err
}
nets = append(nets, n)
}
return nets, nil
}
// GetLiveNetworkNames returns a list of network interfaces on the system
func GetLiveNetworkNames() ([]string, error) {
liveInterfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
interfaceNames := make([]string, 0, len(liveInterfaces))
for _, i := range liveInterfaces {
interfaceNames = append(interfaceNames, i.Name)
}
return interfaceNames, nil
}
// GetFreeNetwork looks for a free network according to existing cni configuration
// files and network interfaces.
func GetFreeNetwork(config *config.Config) (*net.IPNet, error) {
networks, err := GetNetworksFromFilesystem(config)
if err != nil {
return nil, err
}
liveNetworks, err := GetLiveNetworks()
if err != nil {
return nil, err
}
nextNetwork, err := GetDefaultPodmanNetwork()
if err != nil {
return nil, err
}
logrus.Debugf("default network is %s", nextNetwork.String())
for {
newNetwork, err := NextSubnet(nextNetwork)
if err != nil {
return nil, err
}
logrus.Debugf("checking if network %s intersects with other cni networks", nextNetwork.String())
if intersectsConfig, _ := networkIntersectsWithNetworks(newNetwork, allocatorToIPNets(networks)); intersectsConfig {
logrus.Debugf("network %s is already being used by a cni configuration", nextNetwork.String())
nextNetwork = newNetwork
continue
}
logrus.Debugf("checking if network %s intersects with any network interfaces", nextNetwork.String())
if intersectsLive, _ := networkIntersectsWithNetworks(newNetwork, liveNetworks); !intersectsLive {
break
}
logrus.Debugf("network %s is being used by a network interface", nextNetwork.String())
nextNetwork = newNetwork
}
return nextNetwork, nil
}
func allocatorToIPNets(networks []*allocator.Net) []*net.IPNet {
var nets []*net.IPNet
for _, network := range networks {
if len(network.IPAM.Ranges) > 0 {
// this is the new IPAM range style
// append each subnet from ipam the rangeset
for _, r := range network.IPAM.Ranges[0] {
nets = append(nets, newIPNetFromSubnet(r.Subnet))
}
} else {
// looks like the old, deprecated style
nets = append(nets, newIPNetFromSubnet(network.IPAM.Subnet))
}
}
return nets
}
func newIPNetFromSubnet(subnet types.IPNet) *net.IPNet {
n := net.IPNet{
IP: subnet.IP,
Mask: subnet.Mask,
}
return &n
}
func networkIntersectsWithNetworks(n *net.IPNet, networklist []*net.IPNet) (bool, *net.IPNet) {
for _, nw := range networklist {
if networkIntersect(n, nw) {
return true, nw
}
}
return false, nil
}
func networkIntersect(n1, n2 *net.IPNet) bool {
return n2.Contains(n1.IP) || n1.Contains(n2.IP)
}
// ValidateUserNetworkIsAvailable returns via an error if a network is available
// to be used
func ValidateUserNetworkIsAvailable(config *config.Config, userNet *net.IPNet) error {
if len(userNet.IP) == 0 || len(userNet.Mask) == 0 {
return errors.Errorf("network %s's ip or mask cannot be empty", userNet.String())
}
ones, bit := userNet.Mask.Size()
if ones == 0 || bit == 0 {
return errors.Errorf("network %s's mask is invalid", userNet.String())
}
networks, err := GetNetworksFromFilesystem(config)
if err != nil {
return err
}
liveNetworks, err := GetLiveNetworks()
if err != nil {
return err
}
logrus.Debugf("checking if network %s exists in cni networks", userNet.String())
if intersectsConfig, _ := networkIntersectsWithNetworks(userNet, allocatorToIPNets(networks)); intersectsConfig {
return errors.Errorf("network %s is already being used by a cni configuration", userNet.String())
}
logrus.Debugf("checking if network %s exists in any network interfaces", userNet.String())
if intersectsLive, _ := networkIntersectsWithNetworks(userNet, liveNetworks); intersectsLive {
return errors.Errorf("network %s is being used by a network interface", userNet.String())
}
return nil
}
// removeNetwork is removes a cni network without a lock and should only be called
// when a lock was otherwise acquired.
func removeNetwork(config *config.Config, name string) error {
cniPath, err := GetCNIConfigPathByNameOrID(config, name)
if err != nil {
return err
}
// Before we delete the configuration file, we need to make sure we can read and parse
// it to get the network interface name so we can remove that too
interfaceName, err := GetInterfaceNameFromConfig(cniPath)
if err == nil {
// Don't try to remove the network interface if we are not root
if !rootless.IsRootless() {
liveNetworkNames, err := GetLiveNetworkNames()
if err != nil {
return errors.Wrapf(err, "failed to get live network names")
}
if util.StringInSlice(interfaceName, liveNetworkNames) {
if err := RemoveInterface(interfaceName); err != nil {
return errors.Wrapf(err, "failed to delete the network interface %q", interfaceName)
}
}
}
} else if err != ErrNoSuchNetworkInterface {
// Don't error if we couldn't find the network interface name
return err
}
// Remove the configuration file
if err := os.Remove(cniPath); err != nil {
return errors.Wrap(err, "failed to remove network configuration")
}
return nil
}
// RemoveNetwork removes a given network by name. If the network has container associated with it, that
// must be handled outside the context of this.
func RemoveNetwork(config *config.Config, name string) error {
l, err := acquireCNILock(config)
if err != nil {
return err
}
defer l.releaseCNILock()
return removeNetwork(config, name)
}
// InspectNetwork reads a CNI config and returns its configuration
func InspectNetwork(config *config.Config, name string) (map[string]interface{}, error) {
b, err := ReadRawCNIConfByName(config, name)
if err != nil {
return nil, err
}
rawList := make(map[string]interface{})
err = json.Unmarshal(b, &rawList)
return rawList, err
}
// Exists says whether a given network exists or not; it meant
// specifically for restful responses so 404s can be used
func Exists(config *config.Config, name string) (bool, error) {
_, err := ReadRawCNIConfByName(config, name)
if err != nil {
if errors.Cause(err) == define.ErrNoSuchNetwork {
return false, nil
}
return false, err
}
return true, nil
}
// GetNetworkID return the network ID for a given name.
// It is just the sha256 hash but this should be good enough.
func GetNetworkID(name string) string {
hash := sha256.Sum256([]byte(name))
return hex.EncodeToString(hash[:])
}
// PruneNetworks removes networks that are not being used and that is not the default
// network. To keep proper fencing for imports, you must provide the used networks
// to this function as a map. the key is meaningful in the map, the book is a no-op
func PruneNetworks(rtc *config.Config, usedNetworks map[string]bool) ([]*entities.NetworkPruneReport, error) {
var reports []*entities.NetworkPruneReport
lock, err := acquireCNILock(rtc)
if err != nil {
return nil, err
}
defer lock.releaseCNILock()
nets, err := GetNetworkNamesFromFileSystem(rtc)
if err != nil {
return nil, err
}
for _, n := range nets {
_, found := usedNetworks[n]
// Remove is not default network and not found in the used list
if n != rtc.Network.DefaultNetwork && !found {
reports = append(reports, &entities.NetworkPruneReport{
Name: n,
Error: removeNetwork(rtc, n),
})
}
}
return reports, nil
}
|