mirror of https://github.com/vishvananda/netlink
Don't auto-set broadcast unless subnet larger than /31
Since [vishvananda/netlink#248](https://github.com/vishvananda/netlink/pull/248), adding an address automatically sets the broadcast if the broadcast address was not specified. This is undesirable when adding an IP with a prefixlen of /31 or /32. (Additional details in the issues linked below.)
This changes the behavior so that the broadcast is only automatically set if the prefixlen is /30 or larger.
Issue reported in:
- https://github.com/vishvananda/netlink/issues/329
- https://github.com/vishvananda/netlink/issues/471
See also:
- [RFC 3021](http://tools.ietf.org/html/rfc3021)
Alternatives to this PR:
A. https://github.com/vishvananda/netlink/issues/472 - Adds `AddrAddWithoutCalculatedBroadcast`.
B. 9a85a619d2
- Breaking change to make auto-setting the broadcast address an opt-in feature.
C. already works - Suppress setting the broadcast when addr's broadcast address is set to `0.0.0.0`. (This works today, but I'm not sure the behavior can be relied upon as a public API.)
This commit is contained in:
parent
e934999cd7
commit
aad0baef28
|
@ -15,39 +15,62 @@ import (
|
|||
const IFA_FLAGS = 0x8
|
||||
|
||||
// AddrAdd will add an IP address to a link device.
|
||||
//
|
||||
// Equivalent to: `ip addr add $addr dev $link`
|
||||
//
|
||||
// If `addr` is an IPv4 address and the broadcast address is not given, it
|
||||
// will be automatically computed based on the IP mask if /30 or larger.
|
||||
func AddrAdd(link Link, addr *Addr) error {
|
||||
return pkgHandle.AddrAdd(link, addr)
|
||||
}
|
||||
|
||||
// AddrAdd will add an IP address to a link device.
|
||||
//
|
||||
// Equivalent to: `ip addr add $addr dev $link`
|
||||
//
|
||||
// If `addr` is an IPv4 address and the broadcast address is not given, it
|
||||
// will be automatically computed based on the IP mask if /30 or larger.
|
||||
func (h *Handle) AddrAdd(link Link, addr *Addr) error {
|
||||
req := h.newNetlinkRequest(unix.RTM_NEWADDR, unix.NLM_F_CREATE|unix.NLM_F_EXCL|unix.NLM_F_ACK)
|
||||
return h.addrHandle(link, addr, req)
|
||||
}
|
||||
|
||||
// AddrReplace will replace (or, if not present, add) an IP address on a link device.
|
||||
//
|
||||
// Equivalent to: `ip addr replace $addr dev $link`
|
||||
//
|
||||
// If `addr` is an IPv4 address and the broadcast address is not given, it
|
||||
// will be automatically computed based on the IP mask if /30 or larger.
|
||||
func AddrReplace(link Link, addr *Addr) error {
|
||||
return pkgHandle.AddrReplace(link, addr)
|
||||
}
|
||||
|
||||
// AddrReplace will replace (or, if not present, add) an IP address on a link device.
|
||||
//
|
||||
// Equivalent to: `ip addr replace $addr dev $link`
|
||||
//
|
||||
// If `addr` is an IPv4 address and the broadcast address is not given, it
|
||||
// will be automatically computed based on the IP mask if /30 or larger.
|
||||
func (h *Handle) AddrReplace(link Link, addr *Addr) error {
|
||||
req := h.newNetlinkRequest(unix.RTM_NEWADDR, unix.NLM_F_CREATE|unix.NLM_F_REPLACE|unix.NLM_F_ACK)
|
||||
return h.addrHandle(link, addr, req)
|
||||
}
|
||||
|
||||
// AddrDel will delete an IP address from a link device.
|
||||
//
|
||||
// Equivalent to: `ip addr del $addr dev $link`
|
||||
//
|
||||
// If `addr` is an IPv4 address and the broadcast address is not given, it
|
||||
// will be automatically computed based on the IP mask if /30 or larger.
|
||||
func AddrDel(link Link, addr *Addr) error {
|
||||
return pkgHandle.AddrDel(link, addr)
|
||||
}
|
||||
|
||||
// AddrDel will delete an IP address from a link device.
|
||||
// Equivalent to: `ip addr del $addr dev $link`
|
||||
//
|
||||
// If `addr` is an IPv4 address and the broadcast address is not given, it
|
||||
// will be automatically computed based on the IP mask if /30 or larger.
|
||||
func (h *Handle) AddrDel(link Link, addr *Addr) error {
|
||||
req := h.newNetlinkRequest(unix.RTM_DELADDR, unix.NLM_F_ACK)
|
||||
return h.addrHandle(link, addr, req)
|
||||
|
@ -108,14 +131,20 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error
|
|||
}
|
||||
|
||||
if family == FAMILY_V4 {
|
||||
if addr.Broadcast == nil {
|
||||
// Automatically set the broadcast address if it is unset and the
|
||||
// subnet is large enough to sensibly have one (/30 or larger).
|
||||
// See: RFC 3021
|
||||
if addr.Broadcast == nil && prefixlen < 31 {
|
||||
calcBroadcast := make(net.IP, masklen/8)
|
||||
for i := range localAddrData {
|
||||
calcBroadcast[i] = localAddrData[i] | ^mask[i]
|
||||
}
|
||||
addr.Broadcast = calcBroadcast
|
||||
}
|
||||
|
||||
if addr.Broadcast != nil {
|
||||
req.AddData(nl.NewRtAttr(unix.IFA_BROADCAST, addr.Broadcast))
|
||||
}
|
||||
|
||||
if addr.Label != "" {
|
||||
labelData := nl.NewRtAttr(unix.IFA_LABEL, nl.ZeroTerminated(addr.Label))
|
||||
|
|
Loading…
Reference in New Issue