| 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 | "encoding/json" |
| 22 | "fmt" |
| 23 | "net" |
| 24 | |
| 25 | "github.com/containernetworking/cni/pkg/skel" |
| 26 | "github.com/containernetworking/cni/pkg/types" |
| 27 | current "github.com/containernetworking/cni/pkg/types/100" |
| 28 | "github.com/containernetworking/cni/pkg/version" |
| 29 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 30 | ) |
| 31 | |
| 32 | // FirewallNetConf represents the firewall configuration. |
| 33 | type FirewallNetConf struct { |
| 34 | types.NetConf |
| 35 | |
| 36 | // Backend is the firewall type to add rules to. Allowed values are |
| 37 | // 'iptables' and 'firewalld'. |
| 38 | Backend string `json:"backend"` |
| 39 | |
| 40 | // IptablesAdminChainName is an optional name to use instead of the default |
| 41 | // admin rules override chain name that includes the interface name. |
| 42 | IptablesAdminChainName string `json:"iptablesAdminChainName,omitempty"` |
| 43 | |
| 44 | // FirewalldZone is an optional firewalld zone to place the interface into. If |
| 45 | // the firewalld backend is used but the zone is not given, it defaults |
| 46 | // to 'trusted' |
| 47 | FirewalldZone string `json:"firewalldZone,omitempty"` |
| 48 | |
| 49 | // IngressPolicy is an optional ingress policy. |
| 50 | // Defaults to "open". |
| 51 | IngressPolicy IngressPolicy `json:"ingressPolicy,omitempty"` |
| 52 | } |
| 53 | |
| 54 | // IngressPolicy is an ingress policy string. |
| 55 | type IngressPolicy = string |
| 56 | |
| 57 | const ( |
| 58 | // IngressPolicyOpen ("open"): all inbound connections to the container are accepted. |
| 59 | // IngressPolicyOpen is the default ingress policy. |
| 60 | IngressPolicyOpen IngressPolicy = "open" |
| 61 | |
| 62 | // IngressPolicySameBridge ("same-bridge"): connections from the same bridge are accepted, others are blocked. |
| 63 | // This is similar to how Docker libnetwork works. |
| 64 | // IngressPolicySameBridge executes `iptables` regardless to the value of `Backend`. |
| 65 | // IngressPolicySameBridge may not work as expected for non-bridge networks. |
| 66 | IngressPolicySameBridge IngressPolicy = "same-bridge" |
| 67 | |
| 68 | // IngressPolicyIsolated ("isolated"): similar to ingress policy "same-bridge" with the exception |
| 69 | // that connections from the same bridge are also blocked. |
| 70 | // This is equivalent to Docker network option "enable_icc" when set to false. |
| 71 | // IngressPolicyIsolated executes `iptables` regardless to the value of `Backend`. |
| 72 | // IngressPolicyIsolated may not work as expected for non-bridge networks. |
| 73 | IngressPolicyIsolated IngressPolicy = "isolated" |
| 74 | ) |
| 75 | |
| 76 | type FirewallBackend interface { |
| 77 | Add(*FirewallNetConf, *current.Result) error |
| 78 | Del(*FirewallNetConf, *current.Result) error |
| 79 | Check(*FirewallNetConf, *current.Result) error |
| 80 | } |
| 81 | |
| 82 | func ipString(ip net.IPNet) string { |
| 83 | if ip.IP.To4() == nil { |
| 84 | return ip.IP.String() + "/128" |
| 85 | } |
| 86 | return ip.IP.String() + "/32" |
| 87 | } |
| 88 | |
| 89 | func parseConf(data []byte) (*FirewallNetConf, *current.Result, error) { |
| 90 | conf := FirewallNetConf{} |
| 91 | if err := json.Unmarshal(data, &conf); err != nil { |
| 92 | return nil, nil, fmt.Errorf("failed to load netconf: %v", err) |
| 93 | } |
| 94 | |
| 95 | // Default the firewalld zone to trusted |
| 96 | if conf.FirewalldZone == "" { |
| 97 | conf.FirewalldZone = "trusted" |
| 98 | } |
| 99 | |
| 100 | // Parse previous result. |
| 101 | if conf.RawPrevResult == nil { |
| 102 | // return early if there was no previous result, which is allowed for DEL calls |
| 103 | return &conf, ¤t.Result{}, nil |
| 104 | } |
| 105 | |
| 106 | // Parse previous result. |
| 107 | var result *current.Result |
| 108 | var err error |
| 109 | if err = version.ParsePrevResult(&conf.NetConf); err != nil { |
| 110 | return nil, nil, fmt.Errorf("could not parse prevResult: %v", err) |
| 111 | } |
| 112 | |
| 113 | result, err = current.NewResultFromResult(conf.PrevResult) |
| 114 | if err != nil { |
| 115 | return nil, nil, fmt.Errorf("could not convert result to current version: %v", err) |
| 116 | } |
| 117 | |
| 118 | return &conf, result, nil |
| 119 | } |
| 120 | |
| 121 | func getBackend(conf *FirewallNetConf) (FirewallBackend, error) { |
| 122 | switch conf.Backend { |
| 123 | case "iptables": |
| 124 | return newIptablesBackend(conf) |
| 125 | case "firewalld": |
| 126 | return newFirewalldBackend() |
| 127 | } |
| 128 | |
| 129 | // Default to firewalld if it's running |
| 130 | if isFirewalldRunning() { |
| 131 | return newFirewalldBackend() |
| 132 | } |
| 133 | |
| 134 | // Otherwise iptables |
| 135 | return newIptablesBackend(conf) |
| 136 | } |
| 137 | |
| 138 | func cmdAdd(args *skel.CmdArgs) error { |
| 139 | conf, result, err := parseConf(args.StdinData) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | |
| 144 | if conf.PrevResult == nil { |
| 145 | return fmt.Errorf("missing prevResult from earlier plugin") |
| 146 | } |
| 147 | |
| 148 | backend, err := getBackend(conf) |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | |
| 153 | if err := backend.Add(conf, result); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | if err := setupIngressPolicy(conf, result); err != nil { |
| 158 | return err |
| 159 | } |
| 160 | |
| 161 | if result == nil { |
| 162 | result = ¤t.Result{ |
| 163 | CNIVersion: current.ImplementedSpecVersion, |
| 164 | } |
| 165 | } |
| 166 | return types.PrintResult(result, conf.CNIVersion) |
| 167 | } |
| 168 | |
| 169 | func cmdDel(args *skel.CmdArgs) error { |
| 170 | conf, result, err := parseConf(args.StdinData) |
| 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | |
| 175 | backend, err := getBackend(conf) |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | |
| 180 | // Runtime errors are ignored |
| 181 | if err := backend.Del(conf, result); err != nil { |
| 182 | return err |
| 183 | } |
| 184 | |
| 185 | return teardownIngressPolicy(conf) |
| 186 | } |
| 187 | |
| 188 | func main() { |
| 189 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 190 | Add: cmdAdd, |
| 191 | Check: cmdCheck, |
| 192 | Del: cmdDel, |
| 193 | /* FIXME GC */ |
| 194 | /* FIXME Status */ |
| 195 | }, version.VersionsStartingFrom("0.4.0"), bv.BuildString("firewall")) |
| 196 | } |
| 197 | |
| 198 | func cmdCheck(args *skel.CmdArgs) error { |
| 199 | conf, result, err := parseConf(args.StdinData) |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | // Ensure we have previous result. |
| 205 | if conf.PrevResult == nil { |
| 206 | return fmt.Errorf("missing prevResult from earlier plugin") |
| 207 | } |
| 208 | |
| 209 | backend, err := getBackend(conf) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | |
| 214 | return backend.Check(conf, result) |
| 215 | } |