Debian Codegraph @

pkg/ip/ipmasq_linux.go

raw symbols SCIP
1 // Copyright 2015 CNI authors
2 //
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 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ipscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/
16
17 import (
18 "errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/"
19 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
20 "netscip-go gomod github.com/golang/go/src go1.24.2 net/"
21 "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/"
22
23 "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/"
24 "github.com/containernetworking/plugins/pkg/utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/"
25 )
26
27 // SetupIPMasqForNetworks installs rules to masquerade traffic coming from ips of ipns and
28 // going outside of ipns, using a chain name based on network, ifname, and containerID. The
29 // backend can be either "iptables" or "nftables"; if it is nil, then a suitable default
30 // implementation will be used.
31 func SetupIPMasqForNetworksscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/SetupIPMasqForNetworks().(backend *string, ipns []*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, network, ifname, containerID string) error {
32 if backend == nil {
33 // Prefer iptables, unless only nftables is available
34 defaultBackend := "iptables"
35 if !utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.SupportsIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/SupportsIPTables().() && utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.SupportsNFTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/SupportsNFTables().() {
36 defaultBackend = "nftables"
37 }
38 backend = &defaultBackend
39 }
40
41 switch *backend {
42 case "iptables":
43 return setupIPMasqIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/setupIPMasqIPTables().(ipns, network, ifname, containerID)
44 case "nftables":
45 return setupIPMasqNFTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/setupIPMasqNFTables().(ipns, network, ifname, containerID)
46 default:
47 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("unknown ipmasq backend %q", *backend)
48 }
49 }
50
51 // TeardownIPMasqForNetworks undoes the effects of SetupIPMasqForNetworks
52 func TeardownIPMasqForNetworksscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/TeardownIPMasqForNetworks().(ipns []*netscip-go gomod github.com/golang/go/src go1.24.2 net/.IPNetscip-go gomod github.com/golang/go/src go1.24.2 net/IPNet#, network, ifname, containerID string) error {
53 var errs []string
54
55 // Do both the iptables and the nftables cleanup, since the pod may have been
56 // created with a different version of this plugin or a different configuration.
57
58 err := teardownIPMasqIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/teardownIPMasqIPTables().(ipns, network, ifname, containerID)
59 if err != nil && utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.SupportsIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/SupportsIPTables().() {
60 errs = append(errs, err.Error())
61 }
62
63 err = teardownIPMasqNFTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/teardownIPMasqNFTables().(ipns, network, ifname, containerID)
64 if err != nil && utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.SupportsNFTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/SupportsNFTables().() {
65 errs = append(errs, err.Error())
66 }
67
68 if errs == nil {
69 return nil
70 }
71 return errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/.Newscip-go gomod github.com/golang/go/src go1.24.2 errors/New().(stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(errs, "\n"))
72 }
73
74 // GCIPMasqForNetwork garbage collects stale IPMasq entries for network
75 func GCIPMasqForNetworkscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/GCIPMasqForNetwork().(network string, attachments []typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.GCAttachmentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/GCAttachment#) error {
76 var errs []string
77
78 err := gcIPMasqIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/gcIPMasqIPTables().(network, attachments)
79 if err != nil && utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.SupportsIPTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/SupportsIPTables().() {
80 errs = append(errs, err.Error())
81 }
82
83 err = gcIPMasqNFTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ip`/gcIPMasqNFTables().(network, attachments)
84 if err != nil && utilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/.SupportsNFTablesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils`/SupportsNFTables().() {
85 errs = append(errs, err.Error())
86 }
87
88 if errs == nil {
89 return nil
90 }
91 return errorsscip-go gomod github.com/golang/go/src go1.24.2 errors/.Newscip-go gomod github.com/golang/go/src go1.24.2 errors/New().(stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(errs, "\n"))
92 }