| 1 | // Copyright 2022 Arista Networks |
| 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 | "errors" |
| 20 | "fmt" |
| 21 | "net" |
| 22 | |
| 23 | "github.com/vishvananda/netlink" |
| 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 | "github.com/containernetworking/plugins/pkg/ip" |
| 30 | "github.com/containernetworking/plugins/pkg/ipam" |
| 31 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 32 | "github.com/containernetworking/plugins/pkg/ns" |
| 33 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 34 | ) |
| 35 | |
| 36 | func parseNetConf(bytes []byte) (*types.NetConf, error) { |
| 37 | conf := &types.NetConf{} |
| 38 | if err := json.Unmarshal(bytes, conf); err != nil { |
| 39 | return nil, fmt.Errorf("failed to parse network config: %v", err) |
| 40 | } |
| 41 | return conf, nil |
| 42 | } |
| 43 | |
| 44 | func createDummy(ifName string, netns ns.NetNS) (*current.Interface, error) { |
| 45 | dummy := ¤t.Interface{} |
| 46 | |
| 47 | err := netns.Do(func(_ ns.NetNS) error { |
| 48 | linkAttrs := netlink.NewLinkAttrs() |
| 49 | linkAttrs.Name = ifName |
| 50 | |
| 51 | dm := &netlink.Dummy{ |
| 52 | LinkAttrs: linkAttrs, |
| 53 | } |
| 54 | |
| 55 | if err := netlink.LinkAdd(dm); err != nil { |
| 56 | return fmt.Errorf("failed to create dummy: %v", err) |
| 57 | } |
| 58 | dummy.Name = ifName |
| 59 | |
| 60 | // Re-fetch interface to get all properties/attributes |
| 61 | contDummy, err := netlinksafe.LinkByName(ifName) |
| 62 | if err != nil { |
| 63 | return fmt.Errorf("failed to fetch dummy%q: %v", ifName, err) |
| 64 | } |
| 65 | |
| 66 | dummy.Mac = contDummy.Attrs().HardwareAddr.String() |
| 67 | dummy.Sandbox = netns.Path() |
| 68 | |
| 69 | return nil |
| 70 | }) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | |
| 75 | return dummy, nil |
| 76 | } |
| 77 | |
| 78 | func cmdAdd(args *skel.CmdArgs) error { |
| 79 | conf, err := parseNetConf(args.StdinData) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | |
| 84 | if conf.IPAM.Type == "" { |
| 85 | return errors.New("dummy interface requires an IPAM configuration") |
| 86 | } |
| 87 | |
| 88 | netns, err := ns.GetNS(args.Netns) |
| 89 | if err != nil { |
| 90 | return fmt.Errorf("failed to open netns %q: %v", netns, err) |
| 91 | } |
| 92 | defer netns.Close() |
| 93 | |
| 94 | dummyInterface, err := createDummy(args.IfName, netns) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | |
| 99 | // Delete link if err to avoid link leak in this ns |
| 100 | defer func() { |
| 101 | if err != nil { |
| 102 | netns.Do(func(_ ns.NetNS) error { |
| 103 | return ip.DelLinkByName(args.IfName) |
| 104 | }) |
| 105 | } |
| 106 | }() |
| 107 | |
| 108 | r, err := ipam.ExecAdd(conf.IPAM.Type, args.StdinData) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |
| 113 | // defer ipam deletion to avoid ip leak |
| 114 | defer func() { |
| 115 | if err != nil { |
| 116 | ipam.ExecDel(conf.IPAM.Type, args.StdinData) |
| 117 | } |
| 118 | }() |
| 119 | |
| 120 | // convert IPAMResult to current Result type |
| 121 | result, err := current.NewResultFromResult(r) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | if len(result.IPs) == 0 { |
| 127 | return errors.New("IPAM plugin returned missing IP config") |
| 128 | } |
| 129 | |
| 130 | for _, ipc := range result.IPs { |
| 131 | // all addresses apply to the container dummy interface |
| 132 | ipc.Interface = current.Int(0) |
| 133 | } |
| 134 | |
| 135 | result.Interfaces = []*current.Interface{dummyInterface} |
| 136 | |
| 137 | err = netns.Do(func(_ ns.NetNS) error { |
| 138 | return ipam.ConfigureIface(args.IfName, result) |
| 139 | }) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | |
| 144 | return types.PrintResult(result, conf.CNIVersion) |
| 145 | } |
| 146 | |
| 147 | func cmdDel(args *skel.CmdArgs) error { |
| 148 | conf, err := parseNetConf(args.StdinData) |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | |
| 153 | if err = ipam.ExecDel(conf.IPAM.Type, args.StdinData); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | if args.Netns == "" { |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | err = ns.WithNetNSPath(args.Netns, func(ns.NetNS) error { |
| 162 | err = ip.DelLinkByName(args.IfName) |
| 163 | if err != nil && err == ip.ErrLinkNotFound { |
| 164 | return nil |
| 165 | } |
| 166 | return err |
| 167 | }) |
| 168 | if err != nil { |
| 169 | // if NetNs is passed down by the Cloud Orchestration Engine, or if it called multiple times |
| 170 | // so don't return an error if the device is already removed. |
| 171 | // https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444 |
| 172 | _, ok := err.(ns.NSPathNotExistErr) |
| 173 | if ok { |
| 174 | return nil |
| 175 | } |
| 176 | return err |
| 177 | } |
| 178 | |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | func main() { |
| 183 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 184 | Add: cmdAdd, |
| 185 | Check: cmdCheck, |
| 186 | Del: cmdDel, |
| 187 | Status: cmdStatus, |
| 188 | /* FIXME GC */ |
| 189 | }, version.All, bv.BuildString("dummy")) |
| 190 | } |
| 191 | |
| 192 | func cmdCheck(args *skel.CmdArgs) error { |
| 193 | conf, err := parseNetConf(args.StdinData) |
| 194 | if err != nil { |
| 195 | return err |
| 196 | } |
| 197 | |
| 198 | if conf.IPAM.Type == "" { |
| 199 | return errors.New("dummy interface requires an IPAM configuration") |
| 200 | } |
| 201 | |
| 202 | netns, err := ns.GetNS(args.Netns) |
| 203 | if err != nil { |
| 204 | return fmt.Errorf("failed to open netns %q: %v", args.Netns, err) |
| 205 | } |
| 206 | defer netns.Close() |
| 207 | |
| 208 | // run the IPAM plugin and get back the config to apply |
| 209 | err = ipam.ExecCheck(conf.IPAM.Type, args.StdinData) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | |
| 214 | if conf.RawPrevResult == nil { |
| 215 | return fmt.Errorf("dummy: Required prevResult missing") |
| 216 | } |
| 217 | |
| 218 | if err := version.ParsePrevResult(conf); err != nil { |
| 219 | return err |
| 220 | } |
| 221 | |
| 222 | // Convert whatever the IPAM result was into the current Result type |
| 223 | result, err := current.NewResultFromResult(conf.PrevResult) |
| 224 | if err != nil { |
| 225 | return err |
| 226 | } |
| 227 | |
| 228 | var contMap current.Interface |
| 229 | // Find interfaces for name whe know, that of dummy device inside container |
| 230 | for _, intf := range result.Interfaces { |
| 231 | if args.IfName == intf.Name { |
| 232 | if args.Netns == intf.Sandbox { |
| 233 | contMap = *intf |
| 234 | continue |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // The namespace must be the same as what was configured |
| 240 | if args.Netns != contMap.Sandbox { |
| 241 | return fmt.Errorf("Sandbox in prevResult %s doesn't match configured netns: %s", |
| 242 | contMap.Sandbox, args.Netns) |
| 243 | } |
| 244 | |
| 245 | // |
| 246 | // Check prevResults for ips, routes and dns against values found in the container |
| 247 | if err := netns.Do(func(_ ns.NetNS) error { |
| 248 | // Check interface against values found in the container |
| 249 | err := validateCniContainerInterface(contMap) |
| 250 | if err != nil { |
| 251 | return err |
| 252 | } |
| 253 | |
| 254 | err = ip.ValidateExpectedInterfaceIPs(args.IfName, result.IPs) |
| 255 | if err != nil { |
| 256 | return err |
| 257 | } |
| 258 | return nil |
| 259 | }); err != nil { |
| 260 | return err |
| 261 | } |
| 262 | |
| 263 | return nil |
| 264 | } |
| 265 | |
| 266 | func validateCniContainerInterface(intf current.Interface) error { |
| 267 | var link netlink.Link |
| 268 | var err error |
| 269 | |
| 270 | if intf.Name == "" { |
| 271 | return fmt.Errorf("Container interface name missing in prevResult: %v", intf.Name) |
| 272 | } |
| 273 | link, err = netlinksafe.LinkByName(intf.Name) |
| 274 | if err != nil { |
| 275 | return fmt.Errorf("Container Interface name in prevResult: %s not found", intf.Name) |
| 276 | } |
| 277 | if intf.Sandbox == "" { |
| 278 | return fmt.Errorf("Error: Container interface %s should not be in host namespace", link.Attrs().Name) |
| 279 | } |
| 280 | |
| 281 | _, isDummy := link.(*netlink.Dummy) |
| 282 | if !isDummy { |
| 283 | return fmt.Errorf("Error: Container interface %s not of type dummy", link.Attrs().Name) |
| 284 | } |
| 285 | |
| 286 | if intf.Mac != "" { |
| 287 | if intf.Mac != link.Attrs().HardwareAddr.String() { |
| 288 | return fmt.Errorf("Interface %s Mac %s doesn't match container Mac: %s", intf.Name, intf.Mac, link.Attrs().HardwareAddr) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if link.Attrs().Flags&net.FlagUp != net.FlagUp { |
| 293 | return fmt.Errorf("Interface %s is down", intf.Name) |
| 294 | } |
| 295 | |
| 296 | return nil |
| 297 | } |
| 298 | |
| 299 | func cmdStatus(args *skel.CmdArgs) error { |
| 300 | conf := types.NetConf{} |
| 301 | if err := json.Unmarshal(args.StdinData, &conf); err != nil { |
| 302 | return fmt.Errorf("failed to load netconf: %w", err) |
| 303 | } |
| 304 | |
| 305 | if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil { |
| 306 | return err |
| 307 | } |
| 308 | |
| 309 | return nil |
| 310 | } |