2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-04-08 21:06:40 +01:00
|
|
|
|
2024-11-05 04:49:40 +00:00
|
|
|
//go:build linux && !android
|
|
|
|
|
2023-04-18 22:26:58 +01:00
|
|
|
package netmon
|
2022-04-08 21:06:40 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
"net/netip"
|
2022-04-08 21:06:40 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jsimonetti/rtnetlink"
|
|
|
|
"github.com/mdlayher/netlink"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newAddrMsg(iface uint32, addr string, typ netlink.HeaderType) netlink.Message {
|
|
|
|
ip := net.ParseIP(addr)
|
|
|
|
if ip == nil {
|
|
|
|
panic("newAddrMsg: invalid addr: " + addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrMsg := rtnetlink.AddressMessage{
|
|
|
|
Index: iface,
|
|
|
|
Attributes: &rtnetlink.AddressAttributes{
|
|
|
|
Address: ip,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := addrMsg.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return netlink.Message{
|
|
|
|
Header: netlink.Header{Type: typ},
|
|
|
|
Data: b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See issue #4282 and nlConn.addrCache.
|
|
|
|
func TestIgnoreDuplicateNEWADDR(t *testing.T) {
|
|
|
|
mustReceive := func(c *nlConn) message {
|
|
|
|
msg, err := c.Receive()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("mustReceive: unwanted error: %s", err)
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("suppress duplicate NEWADDRs", func(t *testing.T) {
|
|
|
|
c := nlConn{
|
|
|
|
buffered: []netlink.Message{
|
|
|
|
newAddrMsg(1, "192.168.0.5", unix.RTM_NEWADDR),
|
|
|
|
newAddrMsg(1, "192.168.0.5", unix.RTM_NEWADDR),
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
addrCache: make(map[uint32]map[netip.Addr]bool),
|
2022-04-08 21:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := mustReceive(&c)
|
|
|
|
if _, ok := msg.(*newAddrMessage); !ok {
|
|
|
|
t.Fatalf("want newAddrMessage, got %T %v", msg, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = mustReceive(&c)
|
|
|
|
if _, ok := msg.(ignoreMessage); !ok {
|
|
|
|
t.Fatalf("want ignoreMessage, got %T %v", msg, msg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("do not suppress after DELADDR", func(t *testing.T) {
|
|
|
|
c := nlConn{
|
|
|
|
buffered: []netlink.Message{
|
|
|
|
newAddrMsg(1, "192.168.0.5", unix.RTM_NEWADDR),
|
|
|
|
newAddrMsg(1, "192.168.0.5", unix.RTM_DELADDR),
|
|
|
|
newAddrMsg(1, "192.168.0.5", unix.RTM_NEWADDR),
|
|
|
|
},
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 05:14:09 +01:00
|
|
|
addrCache: make(map[uint32]map[netip.Addr]bool),
|
2022-04-08 21:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := mustReceive(&c)
|
|
|
|
if _, ok := msg.(*newAddrMessage); !ok {
|
|
|
|
t.Fatalf("want newAddrMessage, got %T %v", msg, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = mustReceive(&c)
|
|
|
|
if m, ok := msg.(*newAddrMessage); !ok {
|
|
|
|
t.Fatalf("want newAddrMessage, got %T %v", msg, msg)
|
|
|
|
} else {
|
|
|
|
if !m.Delete {
|
|
|
|
t.Fatalf("want delete, got %#v", m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = mustReceive(&c)
|
|
|
|
if _, ok := msg.(*newAddrMessage); !ok {
|
|
|
|
t.Fatalf("want newAddrMessage, got %T %v", msg, msg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|