Repository: moby/moby
File: libnetwork/drivers/bridge/setup_bridgenetfiltering.go (Go)

Code before:
		log.G(context.TODO()).Warnf("failed to check IPv6 forwarding: %v", err)
		return nil
	} else if enabled {
		return enableBridgeNetFiltering("/proc/sys/net/bridge/bridge-nf-call-ip6tables")
	}
	return nil
}

func loadBridgeNetFilterModule(fullPath string) error {
	// br_netfilter implicitly loads bridge module upon modprobe
	return modprobe.LoadModules(context.TODO(), func() error {
		_, err := os.Stat(fullPath)
		return err
	}, "br_netfilter")
}

// Enable bridge net filtering if not already enabled. See GitHub issue #11404
func enableBridgeNetFiltering(nfParam string) (retErr error) {
	defer func() {
		if retErr != nil {
			if ignore, err := strconv.ParseBool(os.Getenv("DOCKER_IGNORE_BR_NETFILTER_ERROR")); err == nil && ignore {
				log.G(context.TODO()).WithError(retErr).Warnf("Continuing without enabling br_netfilter")
				retErr = nil
				return
			}
			retErr = fmt.Errorf("%w: DOCKER_IGNORE_BR_NETFILTER_ERROR=1 to ignore", retErr)
		}
	}()

	if err := loadBridgeNetFilterModule(nfParam); err != nil {
		return fmt.Errorf("cannot restrict inter-container communication or run without the userland proxy: %w", err)
	}
	enabled, err := getKernelBoolParam(nfParam)
	if err != nil {
		var pathErr *os.PathError
		if errors.As(err, &pathErr) && errors.Is(pathErr, syscall.ENOENT) {
			if isRunningInContainer() {
				log.G(context.TODO()).WithError(err).Warnf("running inside docker container, ignoring missing kernel params")
				return nil
			}
			err = errors.New("ensure that the br_netfilter kernel module is loaded")
		}
		return fmt.Errorf("cannot restrict inter-container communication or run without the userland proxy: %v", err)
	}
	if !enabled {
		return os.WriteFile(nfParam, []byte{'1', '\n'}, 0o644)
	}
	return nil
}

// Gets the value of the kernel parameters located at the given path

Review comment (suggestion):
Given that we document  it like this, I guess we can even just check for;

```go
if os.Getenv("DOCKER_IGNORE_BR_NETFILTER_ERROR") == "1" {
```

(but just "any non-empty value" would still work))

Code after:
		return nil
	} else if enabled {
		return enableBridgeNetFiltering("/proc/sys/net/bridge/bridge-nf-call-ip6tables")
	}
	return nil
}

func loadBridgeNetFilterModule(fullPath string) error {
	// br_netfilter implicitly loads bridge module upon modprobe
	return modprobe.LoadModules(context.TODO(), func() error {
		_, err := os.Stat(fullPath)
		return err
	}, "br_netfilter")
}

// Enable bridge net filtering if not already enabled. See GitHub issue #11404
func enableBridgeNetFiltering(nfParam string) (retErr error) {
	defer func() {
		if retErr != nil {
			if os.Getenv("DOCKER_IGNORE_BR_NETFILTER_ERROR") == "1" {
				log.G(context.TODO()).WithError(retErr).Warnf("Continuing without enabling br_netfilter")
				retErr = nil
				return
			}
			retErr = fmt.Errorf("%w: set environment variable DOCKER_IGNORE_BR_NETFILTER_ERROR=1 to ignore", retErr)
		}
	}()

	if err := loadBridgeNetFilterModule(nfParam); err != nil {
		return fmt.Errorf("cannot restrict inter-container communication or run without the userland proxy: %w", err)
	}
	enabled, err := getKernelBoolParam(nfParam)
	if err != nil {
		var pathErr *os.PathError
		if errors.As(err, &pathErr) && errors.Is(pathErr, syscall.ENOENT) {
			if isRunningInContainer() {
				log.G(context.TODO()).WithError(err).Warnf("running inside docker container, ignoring missing kernel params")
				return nil
			}
			err = errors.New("ensure that the br_netfilter kernel module is loaded")
		}
		return fmt.Errorf("cannot restrict inter-container communication or run without the userland proxy: %v", err)
	}
	if !enabled {
		return os.WriteFile(nfParam, []byte{'1', '\n'}, 0o644)
	}
	return nil
}

// Gets the value of the kernel parameters located at the given path
func getKernelBoolParam(path string) (bool, error) {