Update vendored packages

This commit is contained in:
Tobias Schmidt 2017-02-28 17:59:37 -04:00
commit ce117d7a40
119 changed files with 2565 additions and 1524 deletions

View file

@ -2,8 +2,11 @@ package netlink
import (
"errors"
"math/rand"
"sync"
"sync/atomic"
"golang.org/x/net/bpf"
)
// Error messages which can be returned by Validate.
@ -39,6 +42,7 @@ type osConn interface {
Receive() ([]Message, error)
JoinGroup(group uint32) error
LeaveGroup(group uint32) error
SetBPF(filter []bpf.RawInstruction) error
}
// Dial dials a connection to netlink, using the specified protocol number.
@ -56,9 +60,11 @@ func Dial(proto int, config *Config) (*Conn, error) {
// newConn is the internal constructor for Conn, used in tests.
func newConn(c osConn) *Conn {
seq := rand.Uint32()
return &Conn{
c: c,
seq: new(uint32),
seq: &seq,
pid: new(uint32),
}
}
@ -212,6 +218,11 @@ func (c *Conn) LeaveGroup(group uint32) error {
return c.c.LeaveGroup(group)
}
// SetBPF attaches an assembled BPF program to a Conn.
func (c *Conn) SetBPF(filter []bpf.RawInstruction) error {
return c.c.SetBPF(filter)
}
// nextSequence atomically increments Conn's sequence number and returns
// the incremented value.
func (c *Conn) nextSequence() uint32 {

View file

@ -7,10 +7,13 @@ import (
"os"
"syscall"
"unsafe"
"golang.org/x/net/bpf"
"golang.org/x/sys/unix"
)
var (
errInvalidSockaddr = errors.New("expected syscall.SockaddrNetlink but received different syscall.Sockaddr")
errInvalidSockaddr = errors.New("expected unix.SockaddrNetlink but received different unix.Sockaddr")
errInvalidFamily = errors.New("received invalid netlink family")
)
@ -19,24 +22,24 @@ var _ osConn = &conn{}
// A conn is the Linux implementation of a netlink sockets connection.
type conn struct {
s socket
sa *syscall.SockaddrNetlink
sa *unix.SockaddrNetlink
}
// A socket is an interface over socket system calls.
type socket interface {
Bind(sa syscall.Sockaddr) error
Bind(sa unix.Sockaddr) error
Close() error
Recvfrom(p []byte, flags int) (int, syscall.Sockaddr, error)
Sendto(p []byte, flags int, to syscall.Sockaddr) error
Recvmsg(p, oob []byte, flags int) (n int, oobn int, recvflags int, from unix.Sockaddr, err error)
Sendmsg(p, oob []byte, to unix.Sockaddr, flags int) error
SetSockopt(level, name int, v unsafe.Pointer, l uint32) error
}
// dial is the entry point for Dial. dial opens a netlink socket using
// system calls.
func dial(family int, config *Config) (*conn, error) {
fd, err := syscall.Socket(
syscall.AF_NETLINK,
syscall.SOCK_RAW,
fd, err := unix.Socket(
unix.AF_NETLINK,
unix.SOCK_RAW,
family,
)
if err != nil {
@ -53,8 +56,8 @@ func bind(s socket, config *Config) (*conn, error) {
config = &Config{}
}
addr := &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
addr := &unix.SockaddrNetlink{
Family: unix.AF_NETLINK,
Groups: config.Groups,
}
@ -75,9 +78,11 @@ func (c *conn) Send(m Message) error {
return err
}
return c.s.Sendto(b, 0, &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
})
addr := &unix.SockaddrNetlink{
Family: unix.AF_NETLINK,
}
return c.s.Sendmsg(b, nil, addr, 0)
}
// Receive receives one or more Messages from netlink.
@ -85,7 +90,7 @@ func (c *conn) Receive() ([]Message, error) {
b := make([]byte, os.Getpagesize())
for {
// Peek at the buffer to see how many bytes are available
n, _, err := c.s.Recvfrom(b, syscall.MSG_PEEK)
n, _, _, _, err := c.s.Recvmsg(b, nil, unix.MSG_PEEK)
if err != nil {
return nil, err
}
@ -100,16 +105,16 @@ func (c *conn) Receive() ([]Message, error) {
}
// Read out all available messages
n, from, err := c.s.Recvfrom(b, 0)
n, _, _, from, err := c.s.Recvmsg(b, nil, 0)
if err != nil {
return nil, err
}
addr, ok := from.(*syscall.SockaddrNetlink)
addr, ok := from.(*unix.SockaddrNetlink)
if !ok {
return nil, errInvalidSockaddr
}
if addr.Family != syscall.AF_NETLINK {
if addr.Family != unix.AF_NETLINK {
return nil, errInvalidFamily
}
@ -136,16 +141,11 @@ func (c *conn) Close() error {
return c.s.Close()
}
const (
// #define SOL_NETLINK 270
solNetlink = 270
)
// JoinGroup joins a multicast group by ID.
func (c *conn) JoinGroup(group uint32) error {
return c.s.SetSockopt(
solNetlink,
syscall.NETLINK_ADD_MEMBERSHIP,
unix.SOL_NETLINK,
unix.NETLINK_ADD_MEMBERSHIP,
unsafe.Pointer(&group),
uint32(unsafe.Sizeof(group)),
)
@ -154,13 +154,28 @@ func (c *conn) JoinGroup(group uint32) error {
// LeaveGroup leaves a multicast group by ID.
func (c *conn) LeaveGroup(group uint32) error {
return c.s.SetSockopt(
solNetlink,
syscall.NETLINK_DROP_MEMBERSHIP,
unix.SOL_NETLINK,
unix.NETLINK_DROP_MEMBERSHIP,
unsafe.Pointer(&group),
uint32(unsafe.Sizeof(group)),
)
}
// SetBPF attaches an assembled BPF program to a conn.
func (c *conn) SetBPF(filter []bpf.RawInstruction) error {
prog := unix.SockFprog{
Len: uint16(len(filter)),
Filter: (*unix.SockFilter)(unsafe.Pointer(&filter[0])),
}
return c.s.SetSockopt(
unix.SOL_SOCKET,
unix.SO_ATTACH_FILTER,
unsafe.Pointer(&prog),
uint32(unsafe.Sizeof(prog)),
)
}
// sysToHeader converts a syscall.NlMsghdr to a Header.
func sysToHeader(r syscall.NlMsghdr) Header {
// NB: the memory layout of Header and syscall.NlMsgHdr must be
@ -181,13 +196,13 @@ type sysSocket struct {
fd int
}
func (s *sysSocket) Bind(sa syscall.Sockaddr) error { return syscall.Bind(s.fd, sa) }
func (s *sysSocket) Close() error { return syscall.Close(s.fd) }
func (s *sysSocket) Recvfrom(p []byte, flags int) (int, syscall.Sockaddr, error) {
return syscall.Recvfrom(s.fd, p, flags)
func (s *sysSocket) Bind(sa unix.Sockaddr) error { return unix.Bind(s.fd, sa) }
func (s *sysSocket) Close() error { return unix.Close(s.fd) }
func (s *sysSocket) Recvmsg(p, oob []byte, flags int) (int, int, int, unix.Sockaddr, error) {
return unix.Recvmsg(s.fd, p, oob, flags)
}
func (s *sysSocket) Sendto(p []byte, flags int, to syscall.Sockaddr) error {
return syscall.Sendto(s.fd, p, flags, to)
func (s *sysSocket) Sendmsg(p, oob []byte, to unix.Sockaddr, flags int) error {
return unix.Sendmsg(s.fd, p, oob, to, flags)
}
func (s *sysSocket) SetSockopt(level, name int, v unsafe.Pointer, l uint32) error {
return setsockopt(s.fd, level, name, v, l)

View file

@ -5,6 +5,8 @@ package netlink
import (
"fmt"
"runtime"
"golang.org/x/net/bpf"
)
var (
@ -49,6 +51,11 @@ func (c *conn) LeaveGroup(group uint32) error {
return errUnimplemented
}
// SetBPF always returns an error.
func (c *conn) SetBPF(filter []bpf.RawInstruction) error {
return errUnimplemented
}
// newError always returns an error.
func newError(errno int) error {
return errUnimplemented

View file

@ -57,7 +57,6 @@ const (
// HeaderFlagsAtomic requests that netlink send an atomic snapshot of
// its entries. Requires CAP_NET_ADMIN or an effective UID of 0.
// May be obsolete.
HeaderFlagsAtomic HeaderFlags = 0x400
// HeaderFlagsDump requests that netlink return a complete list of
@ -159,8 +158,8 @@ type Header struct {
// A Message is a netlink message. It contains a Header and an arbitrary
// byte payload, which may be decoded using information from the Header.
//
// Data is encoded in the native endianness of the host system. Use this
// package's Uint* and PutUint* functions to encode and decode integers.
// Data is encoded in the native endianness of the host system. For easier
// of encoding and decoding of integers, use package nlenc.
type Message struct {
Header Header
Data []byte

View file

@ -3,14 +3,15 @@
package netlink
import (
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
// setsockopt provides access to the setsockopt syscall.
func setsockopt(fd, level, name int, v unsafe.Pointer, l uint32) error {
_, _, errno := syscall.Syscall6(
syscall.SYS_SETSOCKOPT,
_, _, errno := unix.Syscall6(
unix.SYS_SETSOCKOPT,
uintptr(fd),
uintptr(level),
uintptr(name),