| 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 | 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/netlinksafe" |
| 30 | "github.com/containernetworking/plugins/pkg/ns" |
| 31 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 32 | ) |
| 33 | |
| 34 | func parseNetConf(bytes []byte) (*types.NetConf, error) { |
| 35 | conf := &types.NetConf{} |
| 36 | if err := json.Unmarshal(bytes, conf); err != nil { |
| 37 | return nil, fmt.Errorf("failed to parse network config: %v", err) |
| 38 | } |
| 39 | |
| 40 | if conf.RawPrevResult != nil { |
| 41 | if err := version.ParsePrevResult(conf); err != nil { |
| 42 | return nil, fmt.Errorf("failed to parse prevResult: %v", err) |
| 43 | } |
| 44 | if _, err := current.NewResultFromResult(conf.PrevResult); err != nil { |
| 45 | return nil, fmt.Errorf("failed to convert result to current version: %v", err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return conf, nil |
| 50 | } |
| 51 | |
| 52 | func cmdAdd(args *skel.CmdArgs) error { |
| 53 | conf, err := parseNetConf(args.StdinData) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | var v4Addr, v6Addr *net.IPNet |
| 59 | |
| 60 | args.IfName = "lo" // ignore config, this only works for loopback |
| 61 | err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 62 | link, err := netlinksafe.LinkByName(args.IfName) |
| 63 | if err != nil { |
| 64 | return err // not tested |
| 65 | } |
| 66 | |
| 67 | err = netlink.LinkSetUp(link) |
| 68 | if err != nil { |
| 69 | return err // not tested |
| 70 | } |
| 71 | |
| 72 | v4Addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V4) |
| 73 | if err != nil { |
| 74 | return err // not tested |
| 75 | } |
| 76 | if len(v4Addrs) != 0 { |
| 77 | v4Addr = v4Addrs[0].IPNet |
| 78 | // sanity check that this is a loopback address |
| 79 | for _, addr := range v4Addrs { |
| 80 | if !addr.IP.IsLoopback() { |
| 81 | return fmt.Errorf("loopback interface found with non-loopback address %q", addr.IP) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | v6Addrs, err := netlinksafe.AddrList(link, netlink.FAMILY_V6) |
| 87 | if err != nil { |
| 88 | return err // not tested |
| 89 | } |
| 90 | if len(v6Addrs) != 0 { |
| 91 | v6Addr = v6Addrs[0].IPNet |
| 92 | // sanity check that this is a loopback address |
| 93 | for _, addr := range v6Addrs { |
| 94 | if !addr.IP.IsLoopback() { |
| 95 | return fmt.Errorf("loopback interface found with non-loopback address %q", addr.IP) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return nil |
| 101 | }) |
| 102 | if err != nil { |
| 103 | return err // not tested |
| 104 | } |
| 105 | |
| 106 | var result types.Result |
| 107 | if conf.PrevResult != nil { |
| 108 | // If loopback has previous result which passes from previous CNI plugin, |
| 109 | // loopback should pass it transparently |
| 110 | result = conf.PrevResult |
| 111 | } else { |
| 112 | r := ¤t.Result{ |
| 113 | CNIVersion: conf.CNIVersion, |
| 114 | Interfaces: []*current.Interface{ |
| 115 | { |
| 116 | Name: args.IfName, |
| 117 | Mac: "00:00:00:00:00:00", |
| 118 | Sandbox: args.Netns, |
| 119 | }, |
| 120 | }, |
| 121 | } |
| 122 | |
| 123 | if v4Addr != nil { |
| 124 | r.IPs = append(r.IPs, ¤t.IPConfig{ |
| 125 | Interface: current.Int(0), |
| 126 | Address: *v4Addr, |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | if v6Addr != nil { |
| 131 | r.IPs = append(r.IPs, ¤t.IPConfig{ |
| 132 | Interface: current.Int(0), |
| 133 | Address: *v6Addr, |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | result = r |
| 138 | } |
| 139 | |
| 140 | return types.PrintResult(result, conf.CNIVersion) |
| 141 | } |
| 142 | |
| 143 | func cmdDel(args *skel.CmdArgs) error { |
| 144 | if args.Netns == "" { |
| 145 | return nil |
| 146 | } |
| 147 | args.IfName = "lo" // ignore config, this only works for loopback |
| 148 | err := ns.WithNetNSPath(args.Netns, func(ns.NetNS) error { |
| 149 | link, err := netlinksafe.LinkByName(args.IfName) |
| 150 | if err != nil { |
| 151 | return err // not tested |
| 152 | } |
| 153 | |
| 154 | err = netlink.LinkSetDown(link) |
| 155 | if err != nil { |
| 156 | return err // not tested |
| 157 | } |
| 158 | |
| 159 | return nil |
| 160 | }) |
| 161 | if err != nil { |
| 162 | // if NetNs is passed down by the Cloud Orchestration Engine, or if it called multiple times |
| 163 | // so don't return an error if the device is already removed. |
| 164 | // https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444 |
| 165 | _, ok := err.(ns.NSPathNotExistErr) |
| 166 | if ok { |
| 167 | return nil |
| 168 | } |
| 169 | return err |
| 170 | } |
| 171 | |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | func main() { |
| 176 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 177 | Add: cmdAdd, |
| 178 | Check: cmdCheck, |
| 179 | Del: cmdDel, |
| 180 | /* FIXME GC */ |
| 181 | /* FIXME Status */ |
| 182 | }, version.All, bv.BuildString("loopback")) |
| 183 | } |
| 184 | |
| 185 | func cmdCheck(args *skel.CmdArgs) error { |
| 186 | args.IfName = "lo" // ignore config, this only works for loopback |
| 187 | |
| 188 | return ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 189 | link, err := netlinksafe.LinkByName(args.IfName) |
| 190 | if err != nil { |
| 191 | return err |
| 192 | } |
| 193 | |
| 194 | if link.Attrs().Flags&net.FlagUp != net.FlagUp { |
| 195 | return errors.New("loopback interface is down") |
| 196 | } |
| 197 | |
| 198 | return nil |
| 199 | }) |
| 200 | } |