Skip to content

Commit

Permalink
Merge pull request #148 from telekom-mms/bugfix/avoid-4in6-addresses
Browse files Browse the repository at this point in the history
Avoid IPv4-mapped IPv6 addresses
  • Loading branch information
hwipl authored Feb 11, 2025
2 parents 0e8d546 + 6af007f commit 92b05fd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion internal/addrmon/addrmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (a *AddrMon) start() {
continue
}
ones, _ := e.LinkAddress.Mask.Size()
addr := netip.PrefixFrom(ip, ones)
// unmap to make sure we don't get an IPv4-mapped IPv6 address
addr := netip.PrefixFrom(ip.Unmap(), ones)
u := &Update{
Address: addr,
Index: e.LinkIndex,
Expand Down
8 changes: 7 additions & 1 deletion internal/addrmon/addrmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ func TestAddrMonStartStop(t *testing.T) {
t.Error(err)
}
for i := 0; i < 3; i++ {
log.Println(<-addrMon.Updates())
update := <-addrMon.Updates()
log.Println(update)

// make sure IPv4 address is not IPv4-mapped IPv6 address
if update.Address.Addr().Is4In6() {
t.Errorf("address is IPv4-mapped IPv6 address: %s", update.Address)
}
}
addrMon.Stop()

Expand Down
3 changes: 2 additions & 1 deletion internal/daemoncfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ func GetVPNConfig(vpnconf *vpnconfig.Config) *VPNConfig {
// convert gateway
gateway := netip.Addr{}
if g, ok := netip.AddrFromSlice(vpnconf.Gateway); ok {
gateway = g
// unmap to make sure we don't get an IPv4-mapped IPv6 address
gateway = g.Unmap()
}

// convert ipv4 address
Expand Down
2 changes: 1 addition & 1 deletion internal/daemoncfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func TestGetVPNConfig(t *testing.T) {

// convert and check
got := GetVPNConfig(c)
if got.Gateway.Unmap().String() != "192.168.0.1" ||
if got.Gateway.String() != "192.168.0.1" ||
got.PID != c.PID ||
got.Timeout != c.Timeout ||
got.Device.Name != c.Device.Name ||
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (p *Proxy) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
log.Error("DNS-Proxy received invalid A record in reply")
return
}
addr, ok := netip.AddrFromSlice(rr.A)
addr, ok := netip.AddrFromSlice(rr.A.To4())
if !ok {
log.WithField("A", rr.A).
Error("DNS-Proxy received invalid IP in A record in reply")
Expand Down
10 changes: 8 additions & 2 deletions internal/dnsproxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,20 @@ func TestProxyHandleRequest(t *testing.T) {
}

// reports should contain the IPv4 and the IPv6 address of example.com
wantIPv4 := netip.MustParseAddr("127.0.0.1")
wantIPv6 := netip.MustParseAddr("::1")
for _, r := range reports {
if r.Name != "example.com." {
t.Errorf("invalid domain name: %s", r.Name)
}
if r.IP != netip.MustParseAddr("127.0.0.1") &&
r.IP != netip.MustParseAddr("::1") {
if r.IP != wantIPv4 && r.IP != wantIPv6 {
t.Errorf("invalid IP: %s", r.IP)
}

// make sure IPv4 address is not IPv4-mapped IPv6 address
if r.IP == wantIPv4 && r.IP.Is4In6() {
t.Errorf("address is IPv4-mapped IPv6 address: %s", r.IP)
}
}
}

Expand Down

0 comments on commit 92b05fd

Please sign in to comment.