| 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 | "errors" |
| 23 | "fmt" |
| 24 | "net" |
| 25 | "os" |
| 26 | "path" |
| 27 | "path/filepath" |
| 28 | "regexp" |
| 29 | "strings" |
| 30 | |
| 31 | "github.com/vishvananda/netlink" |
| 32 | "golang.org/x/sys/unix" |
| 33 | |
| 34 | "github.com/containernetworking/cni/pkg/skel" |
| 35 | "github.com/containernetworking/cni/pkg/types" |
| 36 | current "github.com/containernetworking/cni/pkg/types/100" |
| 37 | "github.com/containernetworking/cni/pkg/version" |
| 38 | "github.com/containernetworking/plugins/pkg/netlinksafe" |
| 39 | "github.com/containernetworking/plugins/pkg/ns" |
| 40 | bv "github.com/containernetworking/plugins/pkg/utils/buildversion" |
| 41 | ) |
| 42 | |
| 43 | const ( |
| 44 | defaultDataDir = "/run/cni/tuning" |
| 45 | defaultAllowlistDir = "/etc/cni/tuning/" |
| 46 | defaultAllowlistFile = "allowlist.conf" |
| 47 | ) |
| 48 | |
| 49 | // TuningConf represents the network tuning configuration. |
| 50 | type TuningConf struct { |
| 51 | types.NetConf |
| 52 | DataDir string `json:"dataDir,omitempty"` |
| 53 | SysCtl map[string]string `json:"sysctl"` |
| 54 | Mac string `json:"mac,omitempty"` |
| 55 | Promisc bool `json:"promisc,omitempty"` |
| 56 | Mtu int `json:"mtu,omitempty"` |
| 57 | TxQLen *int `json:"txQLen,omitempty"` |
| 58 | Allmulti *bool `json:"allmulti,omitempty"` |
| 59 | |
| 60 | RuntimeConfig struct { |
| 61 | Mac string `json:"mac,omitempty"` |
| 62 | } `json:"runtimeConfig,omitempty"` |
| 63 | Args *struct { |
| 64 | A *IPAMArgs `json:"cni"` |
| 65 | } `json:"args"` |
| 66 | } |
| 67 | |
| 68 | type IPAMArgs struct { |
| 69 | SysCtl *map[string]string `json:"sysctl"` |
| 70 | Mac *string `json:"mac,omitempty"` |
| 71 | Promisc *bool `json:"promisc,omitempty"` |
| 72 | Mtu *int `json:"mtu,omitempty"` |
| 73 | Allmulti *bool `json:"allmulti,omitempty"` |
| 74 | TxQLen *int `json:"txQLen,omitempty"` |
| 75 | } |
| 76 | |
| 77 | // configToRestore will contain interface attributes that should be restored on cmdDel |
| 78 | type configToRestore struct { |
| 79 | Mac string `json:"mac,omitempty"` |
| 80 | Promisc *bool `json:"promisc,omitempty"` |
| 81 | Mtu int `json:"mtu,omitempty"` |
| 82 | Allmulti *bool `json:"allmulti,omitempty"` |
| 83 | TxQLen *int `json:"txQLen,omitempty"` |
| 84 | } |
| 85 | |
| 86 | // MacEnvArgs represents CNI_ARG |
| 87 | type MacEnvArgs struct { |
| 88 | types.CommonArgs |
| 89 | MAC types.UnmarshallableString `json:"mac,omitempty"` |
| 90 | } |
| 91 | |
| 92 | func parseConf(data []byte, envArgs string) (*TuningConf, error) { |
| 93 | conf := TuningConf{Promisc: false} |
| 94 | if err := json.Unmarshal(data, &conf); err != nil { |
| 95 | return nil, fmt.Errorf("failed to load netconf: %v", err) |
| 96 | } |
| 97 | |
| 98 | if conf.DataDir == "" { |
| 99 | conf.DataDir = defaultDataDir |
| 100 | } |
| 101 | |
| 102 | // Parse custom Mac from both env args |
| 103 | if envArgs != "" { |
| 104 | e := MacEnvArgs{} |
| 105 | err := types.LoadArgs(envArgs, &e) |
| 106 | if err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | if e.MAC != "" { |
| 111 | conf.Mac = string(e.MAC) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Parse custom Mac from RuntimeConfig |
| 116 | if conf.RuntimeConfig.Mac != "" { |
| 117 | conf.Mac = conf.RuntimeConfig.Mac |
| 118 | } |
| 119 | |
| 120 | // Get args |
| 121 | if conf.Args != nil && conf.Args.A != nil { |
| 122 | if conf.Args.A.SysCtl != nil { |
| 123 | for k, v := range *conf.Args.A.SysCtl { |
| 124 | conf.SysCtl[k] = v |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if conf.Args.A.Mac != nil { |
| 129 | conf.Mac = *conf.Args.A.Mac |
| 130 | } |
| 131 | |
| 132 | if conf.Args.A.Promisc != nil { |
| 133 | conf.Promisc = *conf.Args.A.Promisc |
| 134 | } |
| 135 | |
| 136 | if conf.Args.A.Mtu != nil { |
| 137 | conf.Mtu = *conf.Args.A.Mtu |
| 138 | } |
| 139 | |
| 140 | if conf.Args.A.Allmulti != nil { |
| 141 | conf.Allmulti = conf.Args.A.Allmulti |
| 142 | } |
| 143 | |
| 144 | if conf.Args.A.TxQLen != nil { |
| 145 | conf.TxQLen = conf.Args.A.TxQLen |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return &conf, nil |
| 150 | } |
| 151 | |
| 152 | func changeMacAddr(ifName string, newMacAddr string) error { |
| 153 | addr, err := net.ParseMAC(newMacAddr) |
| 154 | if err != nil { |
| 155 | return fmt.Errorf("invalid args %v for MAC addr: %v", newMacAddr, err) |
| 156 | } |
| 157 | |
| 158 | link, err := netlinksafe.LinkByName(ifName) |
| 159 | if err != nil { |
| 160 | return fmt.Errorf("failed to get %q: %v", ifName, err) |
| 161 | } |
| 162 | |
| 163 | return netlink.LinkSetHardwareAddr(link, addr) |
| 164 | } |
| 165 | |
| 166 | func updateResultsMacAddr(config *TuningConf, ifName string, newMacAddr string) { |
| 167 | // Parse previous result. |
| 168 | if config.PrevResult == nil { |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | version.ParsePrevResult(&config.NetConf) |
| 173 | result, _ := current.NewResultFromResult(config.PrevResult) |
| 174 | |
| 175 | for _, i := range result.Interfaces { |
| 176 | if i.Name == ifName { |
| 177 | i.Mac = newMacAddr |
| 178 | } |
| 179 | } |
| 180 | config.PrevResult = result |
| 181 | } |
| 182 | |
| 183 | func changePromisc(ifName string, val bool) error { |
| 184 | link, err := netlinksafe.LinkByName(ifName) |
| 185 | if err != nil { |
| 186 | return fmt.Errorf("failed to get %q: %v", ifName, err) |
| 187 | } |
| 188 | |
| 189 | if val { |
| 190 | return netlink.SetPromiscOn(link) |
| 191 | } |
| 192 | return netlink.SetPromiscOff(link) |
| 193 | } |
| 194 | |
| 195 | func changeMtu(ifName string, mtu int) error { |
| 196 | link, err := netlinksafe.LinkByName(ifName) |
| 197 | if err != nil { |
| 198 | return fmt.Errorf("failed to get %q: %v", ifName, err) |
| 199 | } |
| 200 | return netlink.LinkSetMTU(link, mtu) |
| 201 | } |
| 202 | |
| 203 | func changeAllmulti(ifName string, val bool) error { |
| 204 | link, err := netlinksafe.LinkByName(ifName) |
| 205 | if err != nil { |
| 206 | return fmt.Errorf("failed to get %q: %v", ifName, err) |
| 207 | } |
| 208 | |
| 209 | if val { |
| 210 | return netlink.LinkSetAllmulticastOn(link) |
| 211 | } |
| 212 | return netlink.LinkSetAllmulticastOff(link) |
| 213 | } |
| 214 | |
| 215 | func changeTxQLen(ifName string, txQLen int) error { |
| 216 | link, err := netlinksafe.LinkByName(ifName) |
| 217 | if err != nil { |
| 218 | return fmt.Errorf("failed to get %q: %v", ifName, err) |
| 219 | } |
| 220 | return netlink.LinkSetTxQLen(link, txQLen) |
| 221 | } |
| 222 | |
| 223 | func createBackup(ifName, containerID, backupPath string, tuningConf *TuningConf) error { |
| 224 | config := configToRestore{} |
| 225 | link, err := netlinksafe.LinkByName(ifName) |
| 226 | if err != nil { |
| 227 | return fmt.Errorf("failed to get %q: %v", ifName, err) |
| 228 | } |
| 229 | if tuningConf.Mac != "" { |
| 230 | config.Mac = link.Attrs().HardwareAddr.String() |
| 231 | } |
| 232 | if tuningConf.Promisc { |
| 233 | config.Promisc = new(bool) |
| 234 | *config.Promisc = (link.Attrs().Promisc != 0) |
| 235 | } |
| 236 | if tuningConf.Mtu != 0 { |
| 237 | config.Mtu = link.Attrs().MTU |
| 238 | } |
| 239 | if tuningConf.Allmulti != nil { |
| 240 | config.Allmulti = new(bool) |
| 241 | *config.Allmulti = (link.Attrs().RawFlags&unix.IFF_ALLMULTI != 0) |
| 242 | } |
| 243 | if tuningConf.TxQLen != nil { |
| 244 | qlen := link.Attrs().TxQLen |
| 245 | config.TxQLen = &qlen |
| 246 | } |
| 247 | |
| 248 | if _, err := os.Stat(backupPath); os.IsNotExist(err) { |
| 249 | if err = os.MkdirAll(backupPath, 0o600); err != nil { |
| 250 | return fmt.Errorf("failed to create backup directory: %v", err) |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | data, err := json.MarshalIndent(config, "", " ") |
| 255 | if err != nil { |
| 256 | return fmt.Errorf("failed to marshall data for %q: %v", ifName, err) |
| 257 | } |
| 258 | if err = os.WriteFile(path.Join(backupPath, containerID+"_"+ifName+".json"), data, 0o600); err != nil { |
| 259 | return fmt.Errorf("failed to save file %s.json: %v", ifName, err) |
| 260 | } |
| 261 | |
| 262 | return nil |
| 263 | } |
| 264 | |
| 265 | func restoreBackup(ifName, containerID, backupPath string) error { |
| 266 | filePath := path.Join(backupPath, containerID+"_"+ifName+".json") |
| 267 | |
| 268 | if _, err := os.Stat(filePath); os.IsNotExist(err) { |
| 269 | // No backup file - nothing to revert |
| 270 | return nil |
| 271 | } |
| 272 | |
| 273 | file, err := os.ReadFile(filePath) |
| 274 | if err != nil { |
| 275 | return fmt.Errorf("failed to open file %q: %v", filePath, err) |
| 276 | } |
| 277 | |
| 278 | config := configToRestore{} |
| 279 | if err = json.Unmarshal(file, &config); err != nil { |
| 280 | return nil |
| 281 | } |
| 282 | |
| 283 | var errStr []string |
| 284 | |
| 285 | _, err = netlinksafe.LinkByName(ifName) |
| 286 | if err != nil { |
| 287 | return nil |
| 288 | } |
| 289 | |
| 290 | if config.Mtu != 0 { |
| 291 | if err = changeMtu(ifName, config.Mtu); err != nil { |
| 292 | err = fmt.Errorf("failed to restore MTU: %v", err) |
| 293 | errStr = append(errStr, err.Error()) |
| 294 | } |
| 295 | } |
| 296 | if config.Mac != "" { |
| 297 | if err = changeMacAddr(ifName, config.Mac); err != nil { |
| 298 | err = fmt.Errorf("failed to restore MAC address: %v", err) |
| 299 | errStr = append(errStr, err.Error()) |
| 300 | } |
| 301 | } |
| 302 | if config.Promisc != nil { |
| 303 | if err = changePromisc(ifName, *config.Promisc); err != nil { |
| 304 | err = fmt.Errorf("failed to restore promiscuous mode: %v", err) |
| 305 | errStr = append(errStr, err.Error()) |
| 306 | } |
| 307 | } |
| 308 | if config.Allmulti != nil { |
| 309 | if err = changeAllmulti(ifName, *config.Allmulti); err != nil { |
| 310 | err = fmt.Errorf("failed to restore all-multicast mode: %v", err) |
| 311 | errStr = append(errStr, err.Error()) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if config.TxQLen != nil { |
| 316 | if err = changeTxQLen(ifName, *config.TxQLen); err != nil { |
| 317 | err = fmt.Errorf("failed to restore transmit queue length: %v", err) |
| 318 | errStr = append(errStr, err.Error()) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if len(errStr) > 0 { |
| 323 | return errors.New(strings.Join(errStr, "; ")) |
| 324 | } |
| 325 | |
| 326 | if err = os.Remove(filePath); err != nil { |
| 327 | return fmt.Errorf("failed to remove file %v: %v", filePath, err) |
| 328 | } |
| 329 | |
| 330 | return nil |
| 331 | } |
| 332 | |
| 333 | func cmdAdd(args *skel.CmdArgs) error { |
| 334 | if err := validateSysctlConflictingKeys(args.StdinData); err != nil { |
| 335 | return err |
| 336 | } |
| 337 | tuningConf, err := parseConf(args.StdinData, args.Args) |
| 338 | if err != nil { |
| 339 | return err |
| 340 | } |
| 341 | |
| 342 | if err = validateSysctlConf(tuningConf); err != nil { |
| 343 | return err |
| 344 | } |
| 345 | |
| 346 | if err = validateArgs(args); err != nil { |
| 347 | return err |
| 348 | } |
| 349 | |
| 350 | // Parse previous result. |
| 351 | if tuningConf.RawPrevResult == nil { |
| 352 | return fmt.Errorf("Required prevResult missing") |
| 353 | } |
| 354 | |
| 355 | if err := version.ParsePrevResult(&tuningConf.NetConf); err != nil { |
| 356 | return err |
| 357 | } |
| 358 | |
| 359 | // The directory /proc/sys/net is per network namespace. Enter in the |
| 360 | // network namespace before writing on it. |
| 361 | |
| 362 | err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 363 | for key, value := range tuningConf.SysCtl { |
| 364 | fileName, err := getSysctlFilename(key, args.IfName) |
| 365 | if err != nil { |
| 366 | return err |
| 367 | } |
| 368 | |
| 369 | content := []byte(value) |
| 370 | err = os.WriteFile(fileName, content, 0o644) |
| 371 | if err != nil { |
| 372 | return err |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if tuningConf.Mac != "" || tuningConf.Mtu != 0 || tuningConf.Promisc || tuningConf.Allmulti != nil || tuningConf.TxQLen != nil { |
| 377 | if err = createBackup(args.IfName, args.ContainerID, tuningConf.DataDir, tuningConf); err != nil { |
| 378 | return err |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if tuningConf.Mac != "" { |
| 383 | if err = changeMacAddr(args.IfName, tuningConf.Mac); err != nil { |
| 384 | return err |
| 385 | } |
| 386 | |
| 387 | updateResultsMacAddr(tuningConf, args.IfName, tuningConf.Mac) |
| 388 | } |
| 389 | |
| 390 | if tuningConf.Promisc { |
| 391 | if err = changePromisc(args.IfName, true); err != nil { |
| 392 | return err |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if tuningConf.Mtu != 0 { |
| 397 | if err = changeMtu(args.IfName, tuningConf.Mtu); err != nil { |
| 398 | return err |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if tuningConf.Allmulti != nil { |
| 403 | if err = changeAllmulti(args.IfName, *tuningConf.Allmulti); err != nil { |
| 404 | return err |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if tuningConf.TxQLen != nil { |
| 409 | if err = changeTxQLen(args.IfName, *tuningConf.TxQLen); err != nil { |
| 410 | return err |
| 411 | } |
| 412 | } |
| 413 | return nil |
| 414 | }) |
| 415 | if err != nil { |
| 416 | return err |
| 417 | } |
| 418 | |
| 419 | return types.PrintResult(tuningConf.PrevResult, tuningConf.CNIVersion) |
| 420 | } |
| 421 | |
| 422 | // cmdDel will restore NIC attributes to the original ones when called |
| 423 | func cmdDel(args *skel.CmdArgs) error { |
| 424 | tuningConf, err := parseConf(args.StdinData, args.Args) |
| 425 | if err != nil { |
| 426 | return err |
| 427 | } |
| 428 | |
| 429 | ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 430 | // MAC address, MTU, promiscuous and all-multicast mode settings will be restored |
| 431 | return restoreBackup(args.IfName, args.ContainerID, tuningConf.DataDir) |
| 432 | }) |
| 433 | return nil |
| 434 | } |
| 435 | |
| 436 | func main() { |
| 437 | skel.PluginMainFuncs(skel.CNIFuncs{ |
| 438 | Add: cmdAdd, |
| 439 | Check: cmdCheck, |
| 440 | Del: cmdDel, |
| 441 | /* FIXME GC */ |
| 442 | /* FIXME Status */ |
| 443 | }, version.All, bv.BuildString("tuning")) |
| 444 | } |
| 445 | |
| 446 | func cmdCheck(args *skel.CmdArgs) error { |
| 447 | if err := validateSysctlConflictingKeys(args.StdinData); err != nil { |
| 448 | return err |
| 449 | } |
| 450 | tuningConf, err := parseConf(args.StdinData, args.Args) |
| 451 | if err != nil { |
| 452 | return err |
| 453 | } |
| 454 | |
| 455 | // Parse previous result. |
| 456 | if tuningConf.RawPrevResult == nil { |
| 457 | return fmt.Errorf("Required prevResult missing") |
| 458 | } |
| 459 | |
| 460 | if err := version.ParsePrevResult(&tuningConf.NetConf); err != nil { |
| 461 | return err |
| 462 | } |
| 463 | |
| 464 | _, err = current.NewResultFromResult(tuningConf.PrevResult) |
| 465 | if err != nil { |
| 466 | return err |
| 467 | } |
| 468 | |
| 469 | err = ns.WithNetNSPath(args.Netns, func(_ ns.NetNS) error { |
| 470 | // Check each configured value vs what's currently in the container |
| 471 | for key, confValue := range tuningConf.SysCtl { |
| 472 | fileName, err := getSysctlFilename(key, args.IfName) |
| 473 | if err != nil { |
| 474 | return err |
| 475 | } |
| 476 | |
| 477 | contents, err := os.ReadFile(fileName) |
| 478 | if err != nil { |
| 479 | return err |
| 480 | } |
| 481 | curValue := strings.TrimSuffix(string(contents), "\n") |
| 482 | if confValue != curValue { |
| 483 | return fmt.Errorf("Error: Tuning configured value of %s is %s, current value is %s", fileName, confValue, curValue) |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | link, err := netlinksafe.LinkByName(args.IfName) |
| 488 | if err != nil { |
| 489 | return fmt.Errorf("Cannot find container link %v", args.IfName) |
| 490 | } |
| 491 | |
| 492 | if tuningConf.Mac != "" { |
| 493 | if tuningConf.Mac != link.Attrs().HardwareAddr.String() { |
| 494 | return fmt.Errorf("Error: Tuning configured Ethernet of %s is %s, current value is %s", |
| 495 | args.IfName, tuningConf.Mac, link.Attrs().HardwareAddr) |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if tuningConf.Promisc { |
| 500 | if link.Attrs().Promisc == 0 { |
| 501 | return fmt.Errorf("Error: Tuning link %s configured promisc is %v, current value is %d", |
| 502 | args.IfName, tuningConf.Promisc, link.Attrs().Promisc) |
| 503 | } |
| 504 | } else { |
| 505 | if link.Attrs().Promisc != 0 { |
| 506 | return fmt.Errorf("Error: Tuning link %s configured promisc is %v, current value is %d", |
| 507 | args.IfName, tuningConf.Promisc, link.Attrs().Promisc) |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | if tuningConf.Mtu != 0 { |
| 512 | if tuningConf.Mtu != link.Attrs().MTU { |
| 513 | return fmt.Errorf("Error: Tuning configured MTU of %s is %d, current value is %d", |
| 514 | args.IfName, tuningConf.Mtu, link.Attrs().MTU) |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | if tuningConf.Allmulti != nil { |
| 519 | allmulti := (link.Attrs().RawFlags&unix.IFF_ALLMULTI != 0) |
| 520 | if allmulti != *tuningConf.Allmulti { |
| 521 | return fmt.Errorf("Error: Tuning configured all-multicast mode of %s is %v, current value is %v", |
| 522 | args.IfName, tuningConf.Allmulti, allmulti) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if tuningConf.TxQLen != nil { |
| 527 | if *tuningConf.TxQLen != link.Attrs().TxQLen { |
| 528 | return fmt.Errorf("Error: Tuning configured Transmit Queue Length of %s is %d, current value is %d", |
| 529 | args.IfName, tuningConf.TxQLen, link.Attrs().TxQLen) |
| 530 | } |
| 531 | } |
| 532 | return nil |
| 533 | }) |
| 534 | if err != nil { |
| 535 | return err |
| 536 | } |
| 537 | |
| 538 | return nil |
| 539 | } |
| 540 | |
| 541 | // Validate the sysctls in the tuning config are on the sysctl allowlist file. |
| 542 | // Note that if the allowlist file is missing no validation takes place. |
| 543 | func validateSysctlConf(tuningConf *TuningConf) error { |
| 544 | isPresent, allowlist, err := readAllowlist() |
| 545 | if err != nil { |
| 546 | return err |
| 547 | } |
| 548 | if !isPresent { |
| 549 | return nil |
| 550 | } |
| 551 | for sysctl := range tuningConf.SysCtl { |
| 552 | match, err := contains(sysctl, allowlist) |
| 553 | if err != nil { |
| 554 | return err |
| 555 | } |
| 556 | if !match { |
| 557 | return fmt.Errorf("Sysctl %s is not allowed. Only the following sysctls are allowed: %+v", sysctl, allowlist) |
| 558 | } |
| 559 | } |
| 560 | return nil |
| 561 | } |
| 562 | |
| 563 | // Validate the allowList contains the given sysctl |
| 564 | func contains(sysctl string, allowList []string) (bool, error) { |
| 565 | for _, allowListElement := range allowList { |
| 566 | match, err := regexp.MatchString(allowListElement, sysctl) |
| 567 | if err != nil { |
| 568 | return false, err |
| 569 | } |
| 570 | if match { |
| 571 | return true, nil |
| 572 | } |
| 573 | } |
| 574 | return false, nil |
| 575 | } |
| 576 | |
| 577 | // Read the systctl allowlist from file. Return info if the file is present and the read allowList if it is |
| 578 | func readAllowlist() (bool, []string, error) { |
| 579 | if _, err := os.Stat(filepath.Join(defaultAllowlistDir, defaultAllowlistFile)); os.IsNotExist(err) { |
| 580 | return false, nil, nil |
| 581 | } |
| 582 | dat, err := os.ReadFile(filepath.Join(defaultAllowlistDir, defaultAllowlistFile)) |
| 583 | if err != nil { |
| 584 | return false, nil, err |
| 585 | } |
| 586 | |
| 587 | lines := strings.Split(string(dat), "\n") |
| 588 | allowList := []string{} |
| 589 | for _, line := range lines { |
| 590 | line = strings.TrimSpace(line) |
| 591 | if len(line) > 0 { |
| 592 | allowList = append(allowList, line) |
| 593 | } |
| 594 | } |
| 595 | return true, allowList, nil |
| 596 | } |
| 597 | |
| 598 | type sysctlKey string |
| 599 | |
| 600 | type sysctlCheck struct { |
| 601 | SysCtl map[sysctlKey]string `json:"sysctl"` |
| 602 | } |
| 603 | |
| 604 | var sysctlDuplicatesMap = map[sysctlKey]interface{}{} |
| 605 | |
| 606 | func (d *sysctlKey) UnmarshalText(data []byte) error { |
| 607 | key := sysctlKey(string(data)) |
| 608 | if _, exists := sysctlDuplicatesMap[key]; exists { |
| 609 | return errors.New("duplicated sysctl keys are not allowed") |
| 610 | } |
| 611 | sysctlDuplicatesMap[key] = "" |
| 612 | return nil |
| 613 | } |
| 614 | |
| 615 | func validateSysctlConflictingKeys(data []byte) error { |
| 616 | sysctlCheck := sysctlCheck{} |
| 617 | return json.Unmarshal(data, &sysctlCheck) |
| 618 | } |
| 619 | |
| 620 | func validateArgs(args *skel.CmdArgs) error { |
| 621 | if strings.Contains(args.IfName, string(os.PathSeparator)) { |
| 622 | return fmt.Errorf("Interface name (%s) contains an invalid character %s", args.IfName, string(os.PathSeparator)) |
| 623 | } |
| 624 | return nil |
| 625 | } |
| 626 | |
| 627 | func getSysctlFilename(key, ifName string) (string, error) { |
| 628 | key = strings.ReplaceAll(key, ".", string(os.PathSeparator)) |
| 629 | |
| 630 | // If the key contains `IFNAME` - substitute it with args.IfName |
| 631 | // to allow setting sysctls on a particular interface, on which |
| 632 | // other operations (like mac/mtu setting) are performed |
| 633 | key = strings.Replace(key, "IFNAME", ifName, 1) |
| 634 | |
| 635 | fileName := filepath.Join("/proc/sys", key) |
| 636 | |
| 637 | // Refuse to modify sysctl parameters that don't belong |
| 638 | // to the network subsystem. |
| 639 | if !strings.HasPrefix(fileName, "/proc/sys/net/") { |
| 640 | return "", fmt.Errorf("invalid net sysctl key: %q", key) |
| 641 | } |
| 642 | |
| 643 | return fileName, nil |
| 644 | } |