Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ipv6 link-local address #2660

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion p2p/net/reuseport/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d *dialer) DialContext(ctx context.Context, network, addr string) (net.Con
return nil, err
}
ip := tcpAddr.IP
if !ip.IsLoopback() && !ip.IsGlobalUnicast() {
if !ip.IsLoopback() && !ip.IsGlobalUnicast() && !ip.IsLinkLocalUnicast() {
return nil, fmt.Errorf("undialable IP: %s", ip)
}

Expand Down
15 changes: 13 additions & 2 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,19 @@ func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) (goodAdd
}
return true
},
// TODO: Consider allowing link-local addresses
func(addr ma.Multiaddr) bool { return !manet.IsIP6LinkLocal(addr) },
func(addr ma.Multiaddr) bool {
if manet.IsIP6LinkLocal(addr) {
var hasZone bool
ma.ForEach(addr, func(c ma.Component) bool {
if c.Protocol().Code == ma.P_IP6ZONE {
hasZone = true
}
return false
})
return hasZone
}
return true
},
func(addr ma.Multiaddr) bool {
if s.gater != nil && !s.gater.InterceptAddrDial(p, addr) {
addrErrs = append(addrErrs, TransportError{Address: addr, Cause: ErrGaterDisallowedConnection})
Expand Down
29 changes: 29 additions & 0 deletions p2p/transport/quic/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"

ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/quic-go/quic-go"
quicproxy "github.com/quic-go/quic-go/integrationtests/tools/proxy"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -122,6 +123,34 @@ func testHandshake(t *testing.T, tc *connTestCase) {
defer ln.Close()
handshake(t, ln)
})

ifaces, _ := net.Interfaces()
var linkLocalAddr ma.Multiaddr
findInterface:
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err == nil {
for _, a := range addrs {
addr, err := manet.FromNetAddr(a)
if err == nil {
if manet.IsIP6LinkLocal(addr) {
linkLocalAddr = ma.StringCast("/ip6zone/" + iface.Name + addr.String())
break findInterface
}
}
}
}
}

if linkLocalAddr == nil {
t.Fail()
return
}
t.Run("on IPv6 link-local address", func(t *testing.T) {
ln := runServer(t, serverTransport, linkLocalAddr.String() + "/udp/0/quic-v1")
defer ln.Close()
handshake(t, ln)
})
}

func TestResourceManagerSuccess(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion p2p/transport/quic/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ loop:
}

// Don't use mafmt.QUIC as we don't want to dial DNS addresses. Just /ip{4,6}/udp/quic-v1
var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_UDP), mafmt.Base(ma.P_QUIC_V1))
var dialMatcher = mafmt.Or(
mafmt.And(mafmt.IP, mafmt.Base(ma.P_UDP), mafmt.Base(ma.P_QUIC_V1)),
mafmt.And(mafmt.Base(ma.P_IP6ZONE), mafmt.IP, mafmt.Base(ma.P_UDP), mafmt.Base(ma.P_QUIC_V1)),
)

// CanDial determines if we can dial to an address
func (t *transport) CanDial(addr ma.Multiaddr) bool {
Expand Down
1 change: 1 addition & 0 deletions p2p/transport/quic/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestCanDial(t *testing.T) {
valid := []string{
"/ip4/127.0.0.1/udp/1234/quic-v1",
"/ip4/5.5.5.5/udp/0/quic-v1",
"/ip6zone/eth0/ip6/fe80::fc54:ff:fe43:e553/udp/1234/quic-v1",
}
for _, s := range invalid {
invalidAddr, err := ma.NewMultiaddr(s)
Expand Down
5 changes: 4 additions & 1 deletion p2p/transport/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ func NewTCPTransport(upgrader transport.Upgrader, rcmgr network.ResourceManager,
return tr, nil
}

var dialMatcher = mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP))
var dialMatcher = mafmt.Or(
mafmt.And(mafmt.IP, mafmt.Base(ma.P_TCP)),
mafmt.And(mafmt.Base(ma.P_IP6ZONE), mafmt.IP, mafmt.Base(ma.P_TCP)),
)

// CanDial returns true if this transport believes it can dial the given
// multiaddr.
Expand Down
64 changes: 64 additions & 0 deletions p2p/transport/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tcp
import (
"context"
"errors"
"net"
"testing"

"github.com/libp2p/go-libp2p/core/crypto"
Expand Down Expand Up @@ -46,6 +47,69 @@ func TestTcpTransport(t *testing.T) {
envReuseportVal = true
}

func TestTcpTransportCanDialToLinkLocalAddress(t *testing.T) {
addr := ma.StringCast("/ip6zone/eth0/ip6/fe80::fc54:ff:fe43:e553/tcp/1234")

var u transport.Upgrader
tpt, err := NewTCPTransport(u, nil)
require.NoError(t, err)

if !tpt.CanDial(addr) {
t.Fatal("should be able to dial ip6zone")
}
}

func TestTcpTransportWithLinkLocalAddress(t *testing.T) {
ifaces, err := net.Interfaces()
if err != nil {
t.Error(err)
return
}

var targetAddr ma.Multiaddr

findInterface:
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err == nil {
for _, a := range addrs {
addr, err := manet.FromNetAddr(a)
if err == nil {
if manet.IsIP6LinkLocal(addr) {
targetAddr = ma.StringCast("/ip6zone/" + iface.Name + addr.String())
break findInterface
}
}
}
}
}

if targetAddr == nil {
t.Fail()
return
}

for i := 0; i < 2; i++ {
peerA, ia := makeInsecureMuxer(t)
_, ib := makeInsecureMuxer(t)

ua, err := tptu.New(ia, muxers, nil, nil, nil)
require.NoError(t, err)
ta, err := NewTCPTransport(ua, nil)
require.NoError(t, err)
ub, err := tptu.New(ib, muxers, nil, nil, nil)
require.NoError(t, err)
tb, err := NewTCPTransport(ub, nil)
require.NoError(t, err)

zero := targetAddr.String() + "/tcp/0"
ttransport.SubtestTransport(t, ta, tb, zero, peerA)

envReuseportVal = false
}
envReuseportVal = true
}

func TestTcpTransportWithMetrics(t *testing.T) {
peerA, ia := makeInsecureMuxer(t)
_, ib := makeInsecureMuxer(t)
Expand Down