| 1 | // Copyright 2016 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 "meta-plugin". It reads in its own netconf, it does not create |
| 16 | // any network interface but just changes the network sysctl. |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "net" |
| 23 | |
| 24 | "github.com/coreos/go-iptables/iptables" |
| 25 | |
| 26 | current "github.com/containernetworking/cni/pkg/types/100" |
| 27 | "github.com/containernetworking/plugins/pkg/utils" |
| 28 | ) |
| 29 | |
| 30 | func getPrivChainRules(ip string) [][]string { |
| 31 | var rules [][]string |
| 32 | rules = append(rules, []string{"-d", ip, "-m", "conntrack", "--ctstate", "RELATED,ESTABLISHED", "-j", "ACCEPT"}) |
| 33 | rules = append(rules, []string{"-s", ip, "-j", "ACCEPT"}) |
| 34 | return rules |
| 35 | } |
| 36 | |
| 37 | func generateFilterRule(privChainName string) []string { |
| 38 | return []string{"-m", "comment", "--comment", "CNI firewall plugin rules", "-j", privChainName} |
| 39 | } |
| 40 | |
| 41 | func generateAdminRule(adminChainName string) []string { |
| 42 | return []string{"-m", "comment", "--comment", "CNI firewall plugin admin overrides", "-j", adminChainName} |
| 43 | } |
| 44 | |
| 45 | func cleanupRules(ipt *iptables.IPTables, privChainName string, rules [][]string) { |
| 46 | for _, rule := range rules { |
| 47 | ipt.Delete("filter", privChainName, rule...) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func ensureFirstChainRule(ipt *iptables.IPTables, chain string, rule []string) error { |
| 52 | exists, err := ipt.Exists("filter", chain, rule...) |
| 53 | if !exists && err == nil { |
| 54 | err = ipt.Insert("filter", chain, 1, rule...) |
| 55 | } |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | func (ib *iptablesBackend) setupChains(ipt *iptables.IPTables) error { |
| 60 | privRule := generateFilterRule(ib.privChainName) |
| 61 | adminRule := generateAdminRule(ib.adminChainName) |
| 62 | |
| 63 | // Ensure our private chains exist |
| 64 | if err := utils.EnsureChain(ipt, "filter", ib.privChainName); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | if err := utils.EnsureChain(ipt, "filter", ib.adminChainName); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | // Ensure our filter rule exists in the forward chain |
| 72 | if err := ensureFirstChainRule(ipt, "FORWARD", privRule); err != nil { |
| 73 | return err |
| 74 | } |
| 75 | |
| 76 | // Ensure our admin override chain rule exists in our private chain |
| 77 | return ensureFirstChainRule(ipt, ib.privChainName, adminRule) |
| 78 | } |
| 79 | |
| 80 | func protoForIP(ip net.IPNet) iptables.Protocol { |
| 81 | if ip.IP.To4() != nil { |
| 82 | return iptables.ProtocolIPv4 |
| 83 | } |
| 84 | return iptables.ProtocolIPv6 |
| 85 | } |
| 86 | |
| 87 | func (ib *iptablesBackend) addRules(_ *FirewallNetConf, result *current.Result, ipt *iptables.IPTables, proto iptables.Protocol) error { |
| 88 | rules := make([][]string, 0) |
| 89 | for _, ip := range result.IPs { |
| 90 | if protoForIP(ip.Address) == proto { |
| 91 | rules = append(rules, getPrivChainRules(ipString(ip.Address))...) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if len(rules) > 0 { |
| 96 | if err := ib.setupChains(ipt); err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | // Clean up on any errors |
| 101 | var err error |
| 102 | defer func() { |
| 103 | if err != nil { |
| 104 | cleanupRules(ipt, ib.privChainName, rules) |
| 105 | } |
| 106 | }() |
| 107 | |
| 108 | for _, rule := range rules { |
| 109 | err = ipt.AppendUnique("filter", ib.privChainName, rule...) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | func (ib *iptablesBackend) delRules(_ *FirewallNetConf, result *current.Result, ipt *iptables.IPTables, proto iptables.Protocol) { |
| 120 | rules := make([][]string, 0) |
| 121 | for _, ip := range result.IPs { |
| 122 | if protoForIP(ip.Address) == proto { |
| 123 | rules = append(rules, getPrivChainRules(ipString(ip.Address))...) |
| 124 | } |
| 125 | } |
| 126 | if len(rules) > 0 { |
| 127 | cleanupRules(ipt, ib.privChainName, rules) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func (ib *iptablesBackend) checkRules(_ *FirewallNetConf, result *current.Result, ipt *iptables.IPTables, proto iptables.Protocol) error { |
| 132 | rules := make([][]string, 0) |
| 133 | for _, ip := range result.IPs { |
| 134 | if protoForIP(ip.Address) == proto { |
| 135 | rules = append(rules, getPrivChainRules(ipString(ip.Address))...) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if len(rules) == 0 { |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | // Ensure our private chains exist |
| 144 | if err := utils.EnsureChain(ipt, "filter", ib.privChainName); err != nil { |
| 145 | return err |
| 146 | } |
| 147 | if err := utils.EnsureChain(ipt, "filter", ib.adminChainName); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | // Ensure our filter rule exists in the forward chain |
| 152 | privRule := generateFilterRule(ib.privChainName) |
| 153 | privExists, err := ipt.Exists("filter", "FORWARD", privRule...) |
| 154 | if err != nil { |
| 155 | return err |
| 156 | } |
| 157 | if !privExists { |
| 158 | return fmt.Errorf("expected %v rule %v not found", "FORWARD", privRule) |
| 159 | } |
| 160 | |
| 161 | // Ensure our admin override chain rule exists in our private chain |
| 162 | adminRule := generateAdminRule(ib.adminChainName) |
| 163 | adminExists, err := ipt.Exists("filter", ib.privChainName, adminRule...) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if !adminExists { |
| 168 | return fmt.Errorf("expected %v rule %v not found", ib.privChainName, adminRule) |
| 169 | } |
| 170 | |
| 171 | // ensure rules for this IP address exist |
| 172 | for _, rule := range rules { |
| 173 | // Ensure our rule exists in our private chain |
| 174 | exists, err := ipt.Exists("filter", ib.privChainName, rule...) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if !exists { |
| 179 | return fmt.Errorf("expected rule %v not found", rule) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | func findProtos(conf *FirewallNetConf) []iptables.Protocol { |
| 187 | protos := []iptables.Protocol{iptables.ProtocolIPv4, iptables.ProtocolIPv6} |
| 188 | if conf.PrevResult != nil { |
| 189 | // If PrevResult is given, scan all IP addresses to figure out |
| 190 | // which IP versions to use |
| 191 | protos = []iptables.Protocol{} |
| 192 | result, _ := current.NewResultFromResult(conf.PrevResult) |
| 193 | for _, addr := range result.IPs { |
| 194 | if addr.Address.IP.To4() != nil { |
| 195 | protos = append(protos, iptables.ProtocolIPv4) |
| 196 | } else { |
| 197 | protos = append(protos, iptables.ProtocolIPv6) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | return protos |
| 202 | } |
| 203 | |
| 204 | type iptablesBackend struct { |
| 205 | protos map[iptables.Protocol]*iptables.IPTables |
| 206 | privChainName string |
| 207 | adminChainName string |
| 208 | } |
| 209 | |
| 210 | // iptablesBackend implements the FirewallBackend interface |
| 211 | var _ FirewallBackend = &iptablesBackend{} |
| 212 | |
| 213 | func newIptablesBackend(conf *FirewallNetConf) (FirewallBackend, error) { |
| 214 | adminChainName := conf.IptablesAdminChainName |
| 215 | if adminChainName == "" { |
| 216 | adminChainName = "CNI-ADMIN" |
| 217 | } |
| 218 | |
| 219 | backend := &iptablesBackend{ |
| 220 | privChainName: "CNI-FORWARD", |
| 221 | adminChainName: adminChainName, |
| 222 | protos: make(map[iptables.Protocol]*iptables.IPTables), |
| 223 | } |
| 224 | |
| 225 | for _, proto := range []iptables.Protocol{iptables.ProtocolIPv4, iptables.ProtocolIPv6} { |
| 226 | ipt, err := iptables.NewWithProtocol(proto) |
| 227 | if err != nil { |
| 228 | return nil, fmt.Errorf("could not initialize iptables protocol %v: %v", proto, err) |
| 229 | } |
| 230 | backend.protos[proto] = ipt |
| 231 | } |
| 232 | |
| 233 | return backend, nil |
| 234 | } |
| 235 | |
| 236 | func (ib *iptablesBackend) Add(conf *FirewallNetConf, result *current.Result) error { |
| 237 | for proto, ipt := range ib.protos { |
| 238 | if err := ib.addRules(conf, result, ipt, proto); err != nil { |
| 239 | return err |
| 240 | } |
| 241 | } |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | func (ib *iptablesBackend) Del(conf *FirewallNetConf, result *current.Result) error { |
| 246 | for proto, ipt := range ib.protos { |
| 247 | ib.delRules(conf, result, ipt, proto) |
| 248 | } |
| 249 | return nil |
| 250 | } |
| 251 | |
| 252 | func (ib *iptablesBackend) Check(conf *FirewallNetConf, result *current.Result) error { |
| 253 | for proto, ipt := range ib.protos { |
| 254 | if err := ib.checkRules(conf, result, ipt, proto); err != nil { |
| 255 | return err |
| 256 | } |
| 257 | } |
| 258 | return nil |
| 259 | } |