2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-10-23 21:04:37 +02:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// +build darwin dragonfly freebsd netbsd openbsd
|
2011-10-23 21:04:37 +02:00
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
freebsdConfArch string // "machine $arch" line in kern.conftxt on freebsd
|
|
|
|
minRoutingSockaddrLen = rsaAlignOf(0)
|
|
|
|
)
|
2011-10-23 21:04:37 +02:00
|
|
|
|
|
|
|
// Round the length of a raw sockaddr up to align it properly.
|
|
|
|
func rsaAlignOf(salen int) int {
|
|
|
|
salign := sizeofPtr
|
2013-11-06 20:49:01 +01:00
|
|
|
if darwin64Bit {
|
2015-10-31 01:59:47 +01:00
|
|
|
// Darwin kernels require 32-bit aligned access to
|
|
|
|
// routing facilities.
|
2011-10-23 21:04:37 +02:00
|
|
|
salign = 4
|
2013-11-06 20:49:01 +01:00
|
|
|
} else if netbsd32Bit {
|
2015-10-31 01:59:47 +01:00
|
|
|
// NetBSD 6 and beyond kernels require 64-bit aligned
|
|
|
|
// access to routing facilities.
|
2013-11-06 20:49:01 +01:00
|
|
|
salign = 8
|
2015-10-31 01:59:47 +01:00
|
|
|
} else if runtime.GOOS == "freebsd" {
|
|
|
|
// In the case of kern.supported_archs="amd64 i386",
|
|
|
|
// we need to know the underlying kernel's
|
|
|
|
// architecture because the alignment for routing
|
|
|
|
// facilities are set at the build time of the kernel.
|
|
|
|
if freebsdConfArch == "amd64" {
|
|
|
|
salign = 8
|
|
|
|
}
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
if salen == 0 {
|
|
|
|
return salign
|
|
|
|
}
|
|
|
|
return (salen + salign - 1) & ^(salign - 1)
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
// parseSockaddrLink parses b as a datalink socket address.
|
|
|
|
func parseSockaddrLink(b []byte) (*SockaddrDatalink, error) {
|
2016-02-03 22:58:02 +01:00
|
|
|
if len(b) < 8 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sa, _, err := parseLinkLayerAddr(b[4:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rsa := (*RawSockaddrDatalink)(unsafe.Pointer(&b[0]))
|
|
|
|
sa.Len = rsa.Len
|
|
|
|
sa.Family = rsa.Family
|
|
|
|
sa.Index = rsa.Index
|
|
|
|
return sa, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseLinkLayerAddr parses b as a datalink socket address in
|
|
|
|
// conventional BSD kernel form.
|
|
|
|
func parseLinkLayerAddr(b []byte) (*SockaddrDatalink, int, error) {
|
|
|
|
// The encoding looks like the following:
|
|
|
|
// +----------------------------+
|
|
|
|
// | Type (1 octet) |
|
|
|
|
// +----------------------------+
|
|
|
|
// | Name length (1 octet) |
|
|
|
|
// +----------------------------+
|
|
|
|
// | Address length (1 octet) |
|
|
|
|
// +----------------------------+
|
|
|
|
// | Selector length (1 octet) |
|
|
|
|
// +----------------------------+
|
|
|
|
// | Data (variable) |
|
|
|
|
// +----------------------------+
|
|
|
|
type linkLayerAddr struct {
|
|
|
|
Type byte
|
|
|
|
Nlen byte
|
|
|
|
Alen byte
|
|
|
|
Slen byte
|
|
|
|
}
|
|
|
|
lla := (*linkLayerAddr)(unsafe.Pointer(&b[0]))
|
2016-02-03 22:58:02 +01:00
|
|
|
l := 4 + int(lla.Nlen) + int(lla.Alen) + int(lla.Slen)
|
2015-10-31 01:59:47 +01:00
|
|
|
if len(b) < l {
|
|
|
|
return nil, 0, EINVAL
|
|
|
|
}
|
|
|
|
b = b[4:]
|
|
|
|
sa := &SockaddrDatalink{Type: lla.Type, Nlen: lla.Nlen, Alen: lla.Alen, Slen: lla.Slen}
|
2016-02-03 22:58:02 +01:00
|
|
|
for i := 0; len(sa.Data) > i && i < l-4; i++ {
|
2015-10-31 01:59:47 +01:00
|
|
|
sa.Data[i] = int8(b[i])
|
|
|
|
}
|
2016-02-03 22:58:02 +01:00
|
|
|
return sa, rsaAlignOf(l), nil
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseSockaddrInet parses b as an internet socket address.
|
|
|
|
func parseSockaddrInet(b []byte, family byte) (Sockaddr, error) {
|
|
|
|
switch family {
|
|
|
|
case AF_INET:
|
|
|
|
if len(b) < SizeofSockaddrInet4 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
rsa := (*RawSockaddrAny)(unsafe.Pointer(&b[0]))
|
|
|
|
return anyToSockaddr(rsa)
|
|
|
|
case AF_INET6:
|
|
|
|
if len(b) < SizeofSockaddrInet6 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
rsa := (*RawSockaddrAny)(unsafe.Pointer(&b[0]))
|
|
|
|
return anyToSockaddr(rsa)
|
|
|
|
default:
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
offsetofInet4 = int(unsafe.Offsetof(RawSockaddrInet4{}.Addr))
|
|
|
|
offsetofInet6 = int(unsafe.Offsetof(RawSockaddrInet6{}.Addr))
|
|
|
|
)
|
|
|
|
|
|
|
|
// parseNetworkLayerAddr parses b as an internet socket address in
|
|
|
|
// conventional BSD kernel form.
|
|
|
|
func parseNetworkLayerAddr(b []byte, family byte) (Sockaddr, error) {
|
|
|
|
// The encoding looks similar to the NLRI encoding.
|
|
|
|
// +----------------------------+
|
|
|
|
// | Length (1 octet) |
|
|
|
|
// +----------------------------+
|
|
|
|
// | Address prefix (variable) |
|
|
|
|
// +----------------------------+
|
|
|
|
//
|
|
|
|
// The differences between the kernel form and the NLRI
|
|
|
|
// encoding are:
|
|
|
|
//
|
|
|
|
// - The length field of the kernel form indicates the prefix
|
|
|
|
// length in bytes, not in bits
|
|
|
|
//
|
|
|
|
// - In the kernel form, zero value of the length field
|
|
|
|
// doesn't mean 0.0.0.0/0 or ::/0
|
|
|
|
//
|
|
|
|
// - The kernel form appends leading bytes to the prefix field
|
|
|
|
// to make the <length, prefix> tuple to be conformed with
|
2016-07-22 20:15:38 +02:00
|
|
|
// the routing message boundary
|
2015-10-31 01:59:47 +01:00
|
|
|
l := int(rsaAlignOf(int(b[0])))
|
|
|
|
if len(b) < l {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
// Don't reorder case expressions.
|
|
|
|
// The case expressions for IPv6 must come first.
|
|
|
|
switch {
|
|
|
|
case b[0] == SizeofSockaddrInet6:
|
|
|
|
sa := &SockaddrInet6{}
|
|
|
|
copy(sa.Addr[:], b[offsetofInet6:])
|
|
|
|
return sa, nil
|
|
|
|
case family == AF_INET6:
|
|
|
|
sa := &SockaddrInet6{}
|
|
|
|
if l-1 < offsetofInet6 {
|
|
|
|
copy(sa.Addr[:], b[1:l])
|
|
|
|
} else {
|
|
|
|
copy(sa.Addr[:], b[l-offsetofInet6:l])
|
|
|
|
}
|
|
|
|
return sa, nil
|
|
|
|
case b[0] == SizeofSockaddrInet4:
|
|
|
|
sa := &SockaddrInet4{}
|
|
|
|
copy(sa.Addr[:], b[offsetofInet4:])
|
|
|
|
return sa, nil
|
|
|
|
default: // an old fashion, AF_UNSPEC or unknown means AF_INET
|
|
|
|
sa := &SockaddrInet4{}
|
|
|
|
if l-1 < offsetofInet4 {
|
|
|
|
copy(sa.Addr[:], b[1:l])
|
|
|
|
} else {
|
|
|
|
copy(sa.Addr[:], b[l-offsetofInet4:l])
|
|
|
|
}
|
|
|
|
return sa, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 21:04:37 +02:00
|
|
|
// RouteRIB returns routing information base, as known as RIB,
|
|
|
|
// which consists of network facility information, states and
|
|
|
|
// parameters.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2011-12-13 00:40:51 +01:00
|
|
|
func RouteRIB(facility, param int) ([]byte, error) {
|
2011-10-23 21:04:37 +02:00
|
|
|
mib := []_C_int{CTL_NET, AF_ROUTE, 0, 0, _C_int(facility), _C_int(param)}
|
|
|
|
// Find size.
|
|
|
|
n := uintptr(0)
|
2011-12-13 00:40:51 +01:00
|
|
|
if err := sysctl(mib, nil, &n, nil, 0); err != nil {
|
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
if n == 0 {
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
tab := make([]byte, n)
|
|
|
|
if err := sysctl(mib, &tab[0], &n, nil, 0); err != nil {
|
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
return tab[:n], nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RoutingMessage represents a routing message.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2011-10-23 21:04:37 +02:00
|
|
|
type RoutingMessage interface {
|
2015-10-31 01:59:47 +01:00
|
|
|
sockaddr() ([]Sockaddr, error)
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const anyMessageLen = int(unsafe.Sizeof(anyMessage{}))
|
|
|
|
|
|
|
|
type anyMessage struct {
|
|
|
|
Msglen uint16
|
|
|
|
Version uint8
|
|
|
|
Type uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteMessage represents a routing message containing routing
|
|
|
|
// entries.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2011-10-23 21:04:37 +02:00
|
|
|
type RouteMessage struct {
|
|
|
|
Header RtMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (m *RouteMessage) sockaddr() ([]Sockaddr, error) {
|
|
|
|
var sas [RTAX_MAX]Sockaddr
|
2013-07-16 08:54:42 +02:00
|
|
|
b := m.Data[:]
|
2015-10-31 01:59:47 +01:00
|
|
|
family := uint8(AF_UNSPEC)
|
|
|
|
for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
|
|
|
|
if m.Header.Addrs&(1<<i) == 0 {
|
2011-10-23 21:04:37 +02:00
|
|
|
continue
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
|
2015-10-31 01:59:47 +01:00
|
|
|
switch rsa.Family {
|
|
|
|
case AF_LINK:
|
|
|
|
sa, err := parseSockaddrLink(b)
|
2012-01-13 06:11:45 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sas[i] = sa
|
|
|
|
b = b[rsaAlignOf(int(rsa.Len)):]
|
|
|
|
case AF_INET, AF_INET6:
|
|
|
|
sa, err := parseSockaddrInet(b, rsa.Family)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
sas[i] = sa
|
2015-10-31 01:59:47 +01:00
|
|
|
b = b[rsaAlignOf(int(rsa.Len)):]
|
|
|
|
family = rsa.Family
|
|
|
|
default:
|
|
|
|
sa, err := parseNetworkLayerAddr(b, family)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sas[i] = sa
|
|
|
|
b = b[rsaAlignOf(int(b[0])):]
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return sas[:], nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceMessage represents a routing message containing
|
|
|
|
// network interface entries.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2011-10-23 21:04:37 +02:00
|
|
|
type InterfaceMessage struct {
|
|
|
|
Header IfMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (m *InterfaceMessage) sockaddr() ([]Sockaddr, error) {
|
|
|
|
var sas [RTAX_MAX]Sockaddr
|
2011-10-23 21:04:37 +02:00
|
|
|
if m.Header.Addrs&RTA_IFP == 0 {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sa, err := parseSockaddrLink(m.Data[:])
|
2012-01-13 06:11:45 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sas[RTAX_IFP] = sa
|
|
|
|
return sas[:], nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceAddrMessage represents a routing message containing
|
|
|
|
// network interface address entries.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2011-10-23 21:04:37 +02:00
|
|
|
type InterfaceAddrMessage struct {
|
|
|
|
Header IfaMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (m *InterfaceAddrMessage) sockaddr() ([]Sockaddr, error) {
|
|
|
|
var sas [RTAX_MAX]Sockaddr
|
2013-07-16 08:54:42 +02:00
|
|
|
b := m.Data[:]
|
2015-10-31 01:59:47 +01:00
|
|
|
family := uint8(AF_UNSPEC)
|
|
|
|
for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
|
2015-01-15 01:27:56 +01:00
|
|
|
if m.Header.Addrs&(1<<i) == 0 {
|
2011-10-23 21:04:37 +02:00
|
|
|
continue
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
|
2015-10-31 01:59:47 +01:00
|
|
|
switch rsa.Family {
|
|
|
|
case AF_LINK:
|
|
|
|
sa, err := parseSockaddrLink(b)
|
2012-01-13 06:11:45 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, err
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sas[i] = sa
|
|
|
|
b = b[rsaAlignOf(int(rsa.Len)):]
|
|
|
|
case AF_INET, AF_INET6:
|
|
|
|
sa, err := parseSockaddrInet(b, rsa.Family)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2012-01-13 06:11:45 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sas[i] = sa
|
|
|
|
b = b[rsaAlignOf(int(rsa.Len)):]
|
|
|
|
family = rsa.Family
|
|
|
|
default:
|
|
|
|
sa, err := parseNetworkLayerAddr(b, family)
|
2012-01-13 06:11:45 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
return nil, err
|
2012-01-13 06:11:45 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
sas[i] = sa
|
|
|
|
b = b[rsaAlignOf(int(b[0])):]
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
return sas[:], nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// ParseRoutingMessage parses b as routing messages and returns the
|
|
|
|
// slice containing the RoutingMessage interfaces.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2013-07-16 08:54:42 +02:00
|
|
|
func ParseRoutingMessage(b []byte) (msgs []RoutingMessage, err error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
nmsgs, nskips := 0, 0
|
2013-07-16 08:54:42 +02:00
|
|
|
for len(b) >= anyMessageLen {
|
2015-10-31 01:59:47 +01:00
|
|
|
nmsgs++
|
2013-07-16 08:54:42 +02:00
|
|
|
any := (*anyMessage)(unsafe.Pointer(&b[0]))
|
2011-10-23 21:04:37 +02:00
|
|
|
if any.Version != RTM_VERSION {
|
2014-06-05 01:15:33 +02:00
|
|
|
b = b[any.Msglen:]
|
|
|
|
continue
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
if m := any.toRoutingMessage(b); m == nil {
|
|
|
|
nskips++
|
|
|
|
} else {
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
b = b[any.Msglen:]
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
2014-06-05 01:15:33 +02:00
|
|
|
// We failed to parse any of the messages - version mismatch?
|
2015-10-31 01:59:47 +01:00
|
|
|
if nmsgs != len(msgs)+nskips {
|
2014-06-05 01:15:33 +02:00
|
|
|
return nil, EINVAL
|
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
return msgs, nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|
|
|
|
|
2016-02-03 22:58:02 +01:00
|
|
|
// ParseRoutingSockaddr parses msg's payload as raw sockaddrs and
|
2011-10-23 21:04:37 +02:00
|
|
|
// returns the slice containing the Sockaddr interfaces.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// Deprecated: Use golang.org/x/net/route instead.
|
2015-10-31 01:59:47 +01:00
|
|
|
func ParseRoutingSockaddr(msg RoutingMessage) ([]Sockaddr, error) {
|
|
|
|
sas, err := msg.sockaddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sas, nil
|
2011-10-23 21:04:37 +02:00
|
|
|
}
|