Skip to content

Commit

Permalink
route: use length from message to parse netmask
Browse files Browse the repository at this point in the history
sizeofSockaddrInet is 16, but first byte of sockaddr specifies the size
of the address. 16 works for most cases, except with Netmasks addresses,
on Darwin where only the significant bits are in the msg.

i.e. ipv4
06 02 00 00 ff ff

The above byte sequence is for a sockaddr that is 6 bytes long
representing an ipv4 for address that is 255.255.0.0.

i.e. ipv6 netmask
0e 1e 00 00 00 00 00 00 ff ff ff ff ff ff 00 00

The above is /48 netmask that should also be parsed as such.

Confirmed by using `route monitor`.

sources:
https://github.com/apple/darwin-xnu/blob/main/bsd/net/route.h
https://github.com/apple/darwin-xnu/blob/main/bsd/sys/socket.h#L603
  • Loading branch information
hurricanehrndz committed Sep 4, 2024
1 parent 4542a42 commit d9c6f39
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions route/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,31 @@ func (a *Inet6Addr) marshal(b []byte) (int, error) {
func parseInetAddr(af int, b []byte) (Addr, error) {
switch af {
case syscall.AF_INET:
if len(b) < sizeofSockaddrInet {
l := int(b[0])
if len(b) < l || l < 4 {
return nil, errInvalidAddr
}
a := &Inet4Addr{}
copy(a.IP[:], b[4:8])
n := 8
if l < 8 {
n = l
}
copy(a.IP[:], b[4:n])
return a, nil
case syscall.AF_INET6:
if len(b) < sizeofSockaddrInet6 {
l := int(b[0])
if len(b) < l || l < 8 {
return nil, errInvalidAddr
}
a := &Inet6Addr{ZoneID: int(nativeEndian.Uint32(b[24:28]))}
copy(a.IP[:], b[8:24])
n := 24
if l < 24 {
n = l
}
a := &Inet6Addr{}
if n >= 28 {
a.ZoneID = int(nativeEndian.Uint32(b[24:28]))
}
copy(a.IP[:], b[8:n])
if a.IP[0] == 0xfe && a.IP[1]&0xc0 == 0x80 || a.IP[0] == 0xff && (a.IP[1]&0x0f == 0x01 || a.IP[1]&0x0f == 0x02) {
// KAME based IPv6 protocol stack usually
// embeds the interface index in the
Expand Down

0 comments on commit d9c6f39

Please sign in to comment.