| 1 | // Copyright 2022 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 | // This is a sample chained plugin that supports multiple CNI versions. It |
| 16 | // parses prevResult according to the cniVersion |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | |
| 22 | "github.com/coreos/go-iptables/iptables" |
| 23 | |
| 24 | types100 "github.com/containernetworking/cni/pkg/types/100" |
| 25 | "github.com/containernetworking/plugins/pkg/utils" |
| 26 | ) |
| 27 | |
| 28 | func setupIngressPolicy(conf *FirewallNetConf, prevResult *types100.Result) error { |
| 29 | switch conf.IngressPolicy { |
| 30 | case "", IngressPolicyOpen: |
| 31 | // NOP |
| 32 | return nil |
| 33 | case IngressPolicySameBridge: |
| 34 | return setupIngressPolicyBridgeIsolation(conf, prevResult, false) |
| 35 | case IngressPolicyIsolated: |
| 36 | return setupIngressPolicyBridgeIsolation(conf, prevResult, true) |
| 37 | default: |
| 38 | return fmt.Errorf("unknown ingress policy: %q", conf.IngressPolicy) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func setupIngressPolicyBridgeIsolation(conf *FirewallNetConf, prevResult *types100.Result, isolated bool) error { |
| 43 | if len(prevResult.Interfaces) == 0 { |
| 44 | return fmt.Errorf("interface needs to be set for ingress policy %q, make sure to chain \"firewall\" plugin with \"bridge\"", |
| 45 | conf.IngressPolicy) |
| 46 | } |
| 47 | intf := prevResult.Interfaces[0] |
| 48 | if intf == nil { |
| 49 | return fmt.Errorf("got nil interface") |
| 50 | } |
| 51 | bridgeName := intf.Name |
| 52 | if bridgeName == "" { |
| 53 | return fmt.Errorf("got empty bridge name") |
| 54 | } |
| 55 | for _, iptProto := range findProtos(conf) { |
| 56 | ipt, err := iptables.NewWithProtocol(iptProto) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | if err := setupIsolationChains(ipt, bridgeName, isolated); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | func teardownIngressPolicy(conf *FirewallNetConf) error { |
| 68 | switch conf.IngressPolicy { |
| 69 | case "", IngressPolicyOpen: |
| 70 | // NOP |
| 71 | return nil |
| 72 | case IngressPolicySameBridge, IngressPolicyIsolated: |
| 73 | // NOP |
| 74 | // |
| 75 | // We can't be sure whether conf.bridgeName is still in use by other containers. |
| 76 | // So we do not remove the iptable rules that are created per bridge. |
| 77 | return nil |
| 78 | default: |
| 79 | return fmt.Errorf("unknown ingress policy: %q", conf.IngressPolicy) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | const ( |
| 84 | filterTableName = "filter" // built-in |
| 85 | forwardChainName = "FORWARD" // built-in |
| 86 | ) |
| 87 | |
| 88 | // setupIsolationChains executes the following iptables commands for isolating networks: |
| 89 | // ``` |
| 90 | // iptables -N CNI-ISOLATION-STAGE-1 |
| 91 | // iptables -N CNI-ISOLATION-STAGE-2 |
| 92 | // # NOTE: "-j CNI-ISOLATION-STAGE-1" needs to be before "CNI-FORWARD" chain. So we use -I here. |
| 93 | // iptables -I FORWARD -j CNI-ISOLATION-STAGE-1 |
| 94 | // iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} ! -o ${bridgeName} -j CNI-ISOLATION-STAGE-2 |
| 95 | // [isolated=true] iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} -o ${bridgeName} -j DROP |
| 96 | // iptables -A CNI-ISOLATION-STAGE-1 -j RETURN |
| 97 | // iptables -A CNI-ISOLATION-STAGE-2 -o ${bridgeName} -j DROP |
| 98 | // iptables -A CNI-ISOLATION-STAGE-2 -j RETURN |
| 99 | // ``` |
| 100 | func setupIsolationChains(ipt *iptables.IPTables, bridgeName string, isolated bool) error { |
| 101 | const ( |
| 102 | // Future version may support custom chain names |
| 103 | stage1Chain = "CNI-ISOLATION-STAGE-1" |
| 104 | stage2Chain = "CNI-ISOLATION-STAGE-2" |
| 105 | ) |
| 106 | |
| 107 | ingressPolicyName := "same-bridge" |
| 108 | if isolated { |
| 109 | ingressPolicyName = "isolated" |
| 110 | } |
| 111 | |
| 112 | withPolicyComment := withPolicyComment(ingressPolicyName) |
| 113 | // Commands: |
| 114 | // ``` |
| 115 | // iptables -N CNI-ISOLATION-STAGE-1 |
| 116 | // iptables -N CNI-ISOLATION-STAGE-2 |
| 117 | // ``` |
| 118 | for _, chain := range []string{stage1Chain, stage2Chain} { |
| 119 | if err := utils.EnsureChain(ipt, filterTableName, chain); err != nil { |
| 120 | return err |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Commands: |
| 125 | // ``` |
| 126 | // iptables -I FORWARD -j CNI-ISOLATION-STAGE-1 |
| 127 | // ``` |
| 128 | jumpToStage1 := withDefaultComment([]string{"-j", stage1Chain}) |
| 129 | // NOTE: "-j CNI-ISOLATION-STAGE-1" needs to be before "CNI-FORWARD" created by CNI firewall plugin. |
| 130 | // So we specify prepend = true . |
| 131 | const jumpToStage1Prepend = true |
| 132 | if err := utils.InsertUnique(ipt, filterTableName, forwardChainName, jumpToStage1Prepend, jumpToStage1); err != nil { |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | // Commands: |
| 137 | // ``` |
| 138 | // iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} ! -o ${bridgeName} -j CNI-ISOLATION-STAGE-2 |
| 139 | // [isolate=true] iptables -A CNI-ISOLATION-STAGE-1 -i ${bridgeName} -o ${bridgeName} -j DROP |
| 140 | // iptables -A CNI-ISOLATION-STAGE-1 -j RETURN |
| 141 | // ``` |
| 142 | stage1BridgeRule := withPolicyComment(isolationStage1BridgeRule(bridgeName, stage2Chain)) |
| 143 | stage1BridgeDropRule := withPolicyComment(isolationStage1BridgeDropRule(bridgeName)) |
| 144 | // prepend = true because this needs to be before "-j RETURN" |
| 145 | const stage1BridgePrepend = true |
| 146 | if err := utils.InsertUnique(ipt, filterTableName, stage1Chain, stage1BridgePrepend, stage1BridgeRule); err != nil { |
| 147 | return err |
| 148 | } |
| 149 | if isolated { |
| 150 | if err := utils.InsertUnique(ipt, filterTableName, stage1Chain, stage1BridgePrepend, stage1BridgeDropRule); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | stage1Return := withDefaultComment([]string{"-j", "RETURN"}) |
| 156 | if err := utils.InsertUnique(ipt, filterTableName, stage1Chain, false, stage1Return); err != nil { |
| 157 | return err |
| 158 | } |
| 159 | |
| 160 | // Commands: |
| 161 | // ``` |
| 162 | // iptables -A CNI-ISOLATION-STAGE-2 -o ${bridgeName} -j DROP |
| 163 | // iptables -A CNI-ISOLATION-STAGE-2 -j RETURN |
| 164 | // ``` |
| 165 | stage2Bridge := withDefaultComment(isolationStage2BridgeRule(bridgeName)) |
| 166 | // prepend = true because this needs to be before "-j RETURN" |
| 167 | const stage2BridgePrepend = true |
| 168 | if err := utils.InsertUnique(ipt, filterTableName, stage2Chain, stage2BridgePrepend, stage2Bridge); err != nil { |
| 169 | return err |
| 170 | } |
| 171 | stage2Return := withDefaultComment([]string{"-j", "RETURN"}) |
| 172 | return utils.InsertUnique(ipt, filterTableName, stage2Chain, false, stage2Return) |
| 173 | } |
| 174 | |
| 175 | func isolationStage1BridgeRule(bridgeName, stage2Chain string) []string { |
| 176 | return []string{"-i", bridgeName, "!", "-o", bridgeName, "-j", stage2Chain} |
| 177 | } |
| 178 | |
| 179 | func isolationStage1BridgeDropRule(bridgeName string) []string { |
| 180 | return []string{"-i", bridgeName, "-o", bridgeName, "-j", "DROP"} |
| 181 | } |
| 182 | |
| 183 | func isolationStage2BridgeRule(bridgeName string) []string { |
| 184 | return []string{"-o", bridgeName, "-j", "DROP"} |
| 185 | } |
| 186 | |
| 187 | func withPolicyComment(policy string) func([]string) []string { |
| 188 | return func(rule []string) []string { |
| 189 | comment := fmt.Sprintf("CNI firewall plugin rules (ingressPolicy: %s)", policy) |
| 190 | |
| 191 | return withComment(rule, comment) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | func withDefaultComment(rule []string) []string { |
| 196 | defaultComment := "CNI firewall plugin rules (ingressPolicy: same-bridge|isolated)" |
| 197 | return withComment(rule, defaultComment) |
| 198 | } |
| 199 | |
| 200 | func withComment(rule []string, comment string) []string { |
| 201 | return append(rule, []string{"-m", "comment", "--comment", comment}...) |
| 202 | } |