| 1 | // Copyright 2018 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 main |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "net" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/containernetworking/cni/pkg/skel" |
| 24 | "github.com/containernetworking/cni/pkg/types" |
| 25 | current "github.com/containernetworking/cni/pkg/types/100" |
| 26 | "github.com/containernetworking/cni/pkg/version" |
| 27 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 28 | ) |
| 29 | |
| 30 | // The top-level network config - IPAM plugins are passed the full configuration |
| 31 | // of the calling plugin, not just the IPAM section. |
| 32 | type Net struct { |
| 33 | Name string `json:"name"` |
| 34 | CNIVersion string `json:"cniVersion"` |
| 35 | IPAM *IPAMConfig `json:"ipam"` |
| 36 | |
| 37 | RuntimeConfig struct { |
| 38 | IPs []string `json:"ips,omitempty"` |
| 39 | } `json:"runtimeConfig,omitempty"` |
| 40 | Args *struct { |
| 41 | A *IPAMArgs `json:"cni"` |
| 42 | } `json:"args"` |
| 43 | } |
| 44 | |
| 45 | type IPAMConfig struct { |
| 46 | Name string |
| 47 | Type string `json:"type"` |
| 48 | Routes []*types.Route `json:"routes"` |
| 49 | Addresses []Address `json:"addresses,omitempty"` |
| 50 | DNS types.DNS `json:"dns"` |
| 51 | } |
| 52 | |
| 53 | type IPAMEnvArgs struct { |
| 54 | types.CommonArgs |
| 55 | IP types.UnmarshallableString `json:"ip,omitempty"` |
| 56 | GATEWAY types.UnmarshallableString `json:"gateway,omitempty"` |
| 57 | } |
| 58 | |
| 59 | type IPAMArgs struct { |
| 60 | IPs []string `json:"ips"` |
| 61 | } |
| 62 | |
| 63 | type Address struct { |
| 64 | AddressStr string `json:"address"` |
| 65 | Gateway net.IP `json:"gateway,omitempty"` |
| 66 | Address net.IPNet |
| 67 | Version string |
| 68 | } |
| 69 | |
| 70 | func main() { |
| 71 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 72 | Add: cmdAdd, |
| 73 | Check: cmdCheck, |
| 74 | Del: cmdDel, |
| 75 | /* FIXME GC */ |
| 76 | /* FIXME Status */ |
| 77 | }, version.All, bv.BuildString("static")) |
| 78 | } |
| 79 | |
| 80 | func loadNetConf(bytes []byte) (*types.NetConf, string, error) { |
| 81 | n := &types.NetConf{} |
| 82 | if err := json.Unmarshal(bytes, n); err != nil { |
| 83 | return nil, "", fmt.Errorf("failed to load netconf: %v", err) |
| 84 | } |
| 85 | return n, n.CNIVersion, nil |
| 86 | } |
| 87 | |
| 88 | func cmdCheck(args *skel.CmdArgs) error { |
| 89 | ipamConf, _, err := LoadIPAMConfig(args.StdinData, args.Args) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | // Get PrevResult from stdin... store in RawPrevResult |
| 95 | n, _, err := loadNetConf(args.StdinData) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | // Parse previous result. |
| 101 | if n.RawPrevResult == nil { |
| 102 | return fmt.Errorf("Required prevResult missing") |
| 103 | } |
| 104 | |
| 105 | if err := version.ParsePrevResult(n); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | result, err := current.NewResultFromResult(n.PrevResult) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | |
| 114 | // Each configured IP should be found in result.IPs |
| 115 | for _, rangeset := range ipamConf.Addresses { |
| 116 | for _, ips := range result.IPs { |
| 117 | // Ensure values are what we expect |
| 118 | if rangeset.Address.IP.Equal(ips.Address.IP) { |
| 119 | if rangeset.Gateway == nil { |
| 120 | break |
| 121 | } else if rangeset.Gateway.Equal(ips.Gateway) { |
| 122 | break |
| 123 | } |
| 124 | return fmt.Errorf("static: Failed to match addr %v on interface %v", ips.Address.IP, args.IfName) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | // canonicalizeIP makes sure a provided ip is in standard form |
| 133 | func canonicalizeIP(ip *net.IP) error { |
| 134 | if ip.To4() != nil { |
| 135 | *ip = ip.To4() |
| 136 | return nil |
| 137 | } else if ip.To16() != nil { |
| 138 | *ip = ip.To16() |
| 139 | return nil |
| 140 | } |
| 141 | return fmt.Errorf("IP %s not v4 nor v6", *ip) |
| 142 | } |
| 143 | |
| 144 | // LoadIPAMConfig creates IPAMConfig using json encoded configuration provided |
| 145 | // as `bytes`. At the moment values provided in envArgs are ignored so there |
| 146 | // is no possibility to overload the json configuration using envArgs |
| 147 | func LoadIPAMConfig(bytes []byte, envArgs string) (*IPAMConfig, string, error) { |
| 148 | n := Net{} |
| 149 | if err := json.Unmarshal(bytes, &n); err != nil { |
| 150 | return nil, "", err |
| 151 | } |
| 152 | if n.IPAM == nil { |
| 153 | return nil, "", fmt.Errorf("IPAM config missing 'ipam' key") |
| 154 | } |
| 155 | |
| 156 | // load IP from CNI_ARGS |
| 157 | if envArgs != "" { |
| 158 | e := IPAMEnvArgs{} |
| 159 | err := types.LoadArgs(envArgs, &e) |
| 160 | if err != nil { |
| 161 | return nil, "", err |
| 162 | } |
| 163 | |
| 164 | if e.IP != "" { |
| 165 | for _, item := range strings.Split(string(e.IP), ",") { |
| 166 | ipstr := strings.TrimSpace(item) |
| 167 | |
| 168 | ip, subnet, err := net.ParseCIDR(ipstr) |
| 169 | if err != nil { |
| 170 | return nil, "", fmt.Errorf("the 'ip' field is expected to be in CIDR notation, got: '%s'", ipstr) |
| 171 | } |
| 172 | |
| 173 | addr := Address{ |
| 174 | Address: net.IPNet{IP: ip, Mask: subnet.Mask}, |
| 175 | AddressStr: ipstr, |
| 176 | } |
| 177 | n.IPAM.Addresses = append(n.IPAM.Addresses, addr) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if e.GATEWAY != "" { |
| 182 | for _, item := range strings.Split(string(e.GATEWAY), ",") { |
| 183 | gwip := net.ParseIP(strings.TrimSpace(item)) |
| 184 | if gwip == nil { |
| 185 | return nil, "", fmt.Errorf("invalid gateway address: %s", item) |
| 186 | } |
| 187 | |
| 188 | for i := range n.IPAM.Addresses { |
| 189 | if n.IPAM.Addresses[i].Address.Contains(gwip) { |
| 190 | n.IPAM.Addresses[i].Gateway = gwip |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // import address from args |
| 198 | if n.Args != nil && n.Args.A != nil && len(n.Args.A.IPs) != 0 { |
| 199 | // args IP overwrites IP, so clear IPAM Config |
| 200 | n.IPAM.Addresses = make([]Address, 0, len(n.Args.A.IPs)) |
| 201 | for _, addrStr := range n.Args.A.IPs { |
| 202 | ip, addr, err := net.ParseCIDR(addrStr) |
| 203 | if err != nil { |
| 204 | return nil, "", fmt.Errorf("an entry in the 'ips' field is NOT in CIDR notation, got: '%s'", addrStr) |
| 205 | } |
| 206 | addr.IP = ip |
| 207 | n.IPAM.Addresses = append(n.IPAM.Addresses, Address{AddressStr: addrStr, Address: *addr}) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // import address from runtimeConfig |
| 212 | if len(n.RuntimeConfig.IPs) != 0 { |
| 213 | // runtimeConfig IP overwrites IP, so clear IPAM Config |
| 214 | n.IPAM.Addresses = make([]Address, 0, len(n.RuntimeConfig.IPs)) |
| 215 | for _, addrStr := range n.RuntimeConfig.IPs { |
| 216 | ip, addr, err := net.ParseCIDR(addrStr) |
| 217 | if err != nil { |
| 218 | return nil, "", fmt.Errorf("an entry in the 'ips' field is NOT in CIDR notation, got: '%s'", addrStr) |
| 219 | } |
| 220 | addr.IP = ip |
| 221 | n.IPAM.Addresses = append(n.IPAM.Addresses, Address{AddressStr: addrStr, Address: *addr}) |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // Validate all ranges |
| 226 | numV4 := 0 |
| 227 | numV6 := 0 |
| 228 | |
| 229 | for i := range n.IPAM.Addresses { |
| 230 | if n.IPAM.Addresses[i].Address.IP == nil { |
| 231 | ip, addr, err := net.ParseCIDR(n.IPAM.Addresses[i].AddressStr) |
| 232 | if err != nil { |
| 233 | return nil, "", fmt.Errorf( |
| 234 | "the 'address' field is expected to be in CIDR notation, got: '%s'", n.IPAM.Addresses[i].AddressStr) |
| 235 | } |
| 236 | n.IPAM.Addresses[i].Address = *addr |
| 237 | n.IPAM.Addresses[i].Address.IP = ip |
| 238 | } |
| 239 | |
| 240 | if err := canonicalizeIP(&n.IPAM.Addresses[i].Address.IP); err != nil { |
| 241 | return nil, "", fmt.Errorf("invalid address %d: %s", i, err) |
| 242 | } |
| 243 | |
| 244 | if n.IPAM.Addresses[i].Address.IP.To4() != nil { |
| 245 | numV4++ |
| 246 | } else { |
| 247 | numV6++ |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // CNI spec 0.2.0 and below supported only one v4 and v6 address |
| 252 | if numV4 > 1 || numV6 > 1 { |
| 253 | if ok, _ := version.GreaterThanOrEqualTo(n.CNIVersion, "0.3.0"); !ok { |
| 254 | return nil, "", fmt.Errorf("CNI version %v does not support more than 1 address per family", n.CNIVersion) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Copy net name into IPAM so not to drag Net struct around |
| 259 | n.IPAM.Name = n.Name |
| 260 | |
| 261 | return n.IPAM, n.CNIVersion, nil |
| 262 | } |
| 263 | |
| 264 | func cmdAdd(args *skel.CmdArgs) error { |
| 265 | ipamConf, confVersion, err := LoadIPAMConfig(args.StdinData, args.Args) |
| 266 | if err != nil { |
| 267 | return err |
| 268 | } |
| 269 | |
| 270 | result := ¤t.Result{ |
| 271 | CNIVersion: current.ImplementedSpecVersion, |
| 272 | DNS: ipamConf.DNS, |
| 273 | Routes: ipamConf.Routes, |
| 274 | } |
| 275 | for _, v := range ipamConf.Addresses { |
| 276 | result.IPs = append(result.IPs, ¤t.IPConfig{ |
| 277 | Address: v.Address, |
| 278 | Gateway: v.Gateway, |
| 279 | }) |
| 280 | } |
| 281 | |
| 282 | return types.PrintResult(result, confVersion) |
| 283 | } |
| 284 | |
| 285 | func cmdDel(_ *skel.CmdArgs) error { |
| 286 | // Nothing required because of no resource allocation in static plugin. |
| 287 | return nil |
| 288 | } |