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

Code before:
}

func (n *bridgeNetwork) newIptablesNetwork() error {
	config4, err := makeNetworkConfigFam(n.config.HostIPv4, n.bridge.bridgeIPv4, n.gwMode(iptables.IPv4))
	if err != nil {
		return err
	}
	config6, err := makeNetworkConfigFam(n.config.HostIPv6, n.bridge.bridgeIPv6, n.gwMode(iptables.IPv6))
	if err != nil {
		return err
	}
	iptn, err := newIptablesNetwork(networkConfig{
		IfName:     n.config.BridgeName,
		Internal:   n.config.Internal,
		ICC:        n.config.EnableICC,
		Masquerade: n.config.EnableIPMasquerade,
		Config4:    config4,
		Config6:    config6,
		Hairpin:    !n.driver.config.EnableUserlandProxy || n.driver.config.UserlandProxyPath == "",
		Enable4:    n.driver.config.EnableIPTables,
		Enable6:    n.driver.config.EnableIP6Tables,
	})
	if err != nil {
		return err
	}
	n.iptablesNetwork = iptn
	return nil
}

func makeNetworkConfigFam(hostIP net.IP, bridgePrefix *net.IPNet, gwm gwMode) (networkConfigFam, error) {
	c := networkConfigFam{
		Routed:      gwm.routed(),
		Unprotected: gwm.unprotected(),
	}
	if hostIP != nil {
		var ok bool
		c.HostIP, ok = netip.AddrFromSlice(hostIP)
		if !ok {
			return networkConfigFam{}, fmt.Errorf("invalid host address %q", hostIP)
		}
	}
	if bridgePrefix != nil {
		p, ok := netiputil.ToPrefix(bridgePrefix)
		if !ok {
			return networkConfigFam{}, fmt.Errorf("invalid bridge prefix %s", bridgePrefix)
		}
		c.Prefix = p.Masked()
	}
	return c, nil
}


Review comment (suggestion):
If you add an extra `Reload` method, then you don't need to re-assign `n.iptablesNetwork` in the firewalld reload code, and you can make this simpler:

```suggestion
    return newIptablesNetwork(networkConfig{
		IfName:     n.config.BridgeName,
		Internal:   n.config.Internal,
		ICC:        n.config.EnableICC,
		Masquerade: n.config.EnableIPMasquerade,
		Config4:    config4,
		Config6:    config6,
		Hairpin:    !n.driver.config.EnableUserlandProxy || n.driver.config.UserlandProxyPath == "",
		Enable4:    n.driver.config.EnableIPTables,
		Enable6:    n.driver.config.EnableIP6Tables,
	})
```

And let the caller assign `n.iptablesNetwork`.

Code after:
}

func (n *bridgeNetwork) newIptablesNetwork() (*iptablesNetwork, error) {
	config4, err := makeNetworkConfigFam(n.config.HostIPv4, n.bridge.bridgeIPv4, n.gwMode(iptables.IPv4))
	if err != nil {
		return nil, err
	}
	config6, err := makeNetworkConfigFam(n.config.HostIPv6, n.bridge.bridgeIPv6, n.gwMode(iptables.IPv6))
	if err != nil {
		return nil, err
	}
	return newIptablesNetwork(networkConfig{
		IfName:     n.config.BridgeName,
		Internal:   n.config.Internal,
		ICC:        n.config.EnableICC,
		Masquerade: n.config.EnableIPMasquerade,
		Config4:    config4,
		Config6:    config6,
		Hairpin:    !n.driver.config.EnableUserlandProxy || n.driver.config.UserlandProxyPath == "",
		Enable4:    n.driver.config.EnableIPTables,
		Enable6:    n.driver.config.EnableIP6Tables,
	})
}

func makeNetworkConfigFam(hostIP net.IP, bridgePrefix *net.IPNet, gwm gwMode) (networkConfigFam, error) {
	c := networkConfigFam{
		Routed:      gwm.routed(),
		Unprotected: gwm.unprotected(),
	}
	if hostIP != nil {
		var ok bool
		c.HostIP, ok = netip.AddrFromSlice(hostIP)
		if !ok {
			return networkConfigFam{}, fmt.Errorf("invalid host address %q", hostIP)
		}
		c.HostIP = c.HostIP.Unmap()
	}
	if bridgePrefix != nil {
		p, ok := netiputil.ToPrefix(bridgePrefix)
		if !ok {
			return networkConfigFam{}, fmt.Errorf("invalid bridge prefix %s", bridgePrefix)
		}
		c.Prefix = p.Masked()
	}
	return c, nil
}

func (n *bridgeNetwork) getNetworkBridgeName() string {
	n.Lock()
	config := n.config
	n.Unlock()