2017-01-09 20:33:55 +01:00
|
|
|
//+build linux
|
|
|
|
|
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2017-02-28 22:59:37 +01:00
|
|
|
|
|
|
|
"golang.org/x/net/bpf"
|
|
|
|
"golang.org/x/sys/unix"
|
2017-01-09 20:33:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-28 22:59:37 +01:00
|
|
|
errInvalidSockaddr = errors.New("expected unix.SockaddrNetlink but received different unix.Sockaddr")
|
2017-01-09 20:33:55 +01:00
|
|
|
errInvalidFamily = errors.New("received invalid netlink family")
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ osConn = &conn{}
|
|
|
|
|
|
|
|
// A conn is the Linux implementation of a netlink sockets connection.
|
|
|
|
type conn struct {
|
|
|
|
s socket
|
2017-02-28 22:59:37 +01:00
|
|
|
sa *unix.SockaddrNetlink
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A socket is an interface over socket system calls.
|
|
|
|
type socket interface {
|
2017-02-28 22:59:37 +01:00
|
|
|
Bind(sa unix.Sockaddr) error
|
2017-01-09 20:33:55 +01:00
|
|
|
Close() error
|
2017-02-28 22:59:37 +01:00
|
|
|
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
|
2017-01-09 20:33:55 +01:00
|
|
|
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) {
|
2017-02-28 22:59:37 +01:00
|
|
|
fd, err := unix.Socket(
|
|
|
|
unix.AF_NETLINK,
|
|
|
|
unix.SOCK_RAW,
|
2017-01-09 20:33:55 +01:00
|
|
|
family,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bind(&sysSocket{fd: fd}, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind binds a connection to netlink using the input socket, which may be
|
|
|
|
// a system call implementation or a mocked one for tests.
|
|
|
|
func bind(s socket, config *Config) (*conn, error) {
|
|
|
|
if config == nil {
|
|
|
|
config = &Config{}
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:59:37 +01:00
|
|
|
addr := &unix.SockaddrNetlink{
|
|
|
|
Family: unix.AF_NETLINK,
|
2017-01-09 20:33:55 +01:00
|
|
|
Groups: config.Groups,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.Bind(addr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &conn{
|
|
|
|
s: s,
|
|
|
|
sa: addr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends a single Message to netlink.
|
|
|
|
func (c *conn) Send(m Message) error {
|
|
|
|
b, err := m.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:59:37 +01:00
|
|
|
addr := &unix.SockaddrNetlink{
|
|
|
|
Family: unix.AF_NETLINK,
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.s.Sendmsg(b, nil, addr, 0)
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receive receives one or more Messages from netlink.
|
|
|
|
func (c *conn) Receive() ([]Message, error) {
|
|
|
|
b := make([]byte, os.Getpagesize())
|
|
|
|
for {
|
|
|
|
// Peek at the buffer to see how many bytes are available
|
2017-02-28 22:59:37 +01:00
|
|
|
n, _, _, _, err := c.s.Recvmsg(b, nil, unix.MSG_PEEK)
|
2017-01-09 20:33:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break when we can read all messages
|
|
|
|
if n < len(b) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double in size if not enough bytes
|
|
|
|
b = make([]byte, len(b)*2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read out all available messages
|
2017-02-28 22:59:37 +01:00
|
|
|
n, _, _, from, err := c.s.Recvmsg(b, nil, 0)
|
2017-01-09 20:33:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:59:37 +01:00
|
|
|
addr, ok := from.(*unix.SockaddrNetlink)
|
2017-01-09 20:33:55 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidSockaddr
|
|
|
|
}
|
2017-02-28 22:59:37 +01:00
|
|
|
if addr.Family != unix.AF_NETLINK {
|
2017-01-09 20:33:55 +01:00
|
|
|
return nil, errInvalidFamily
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := syscall.ParseNetlinkMessage(b[:n])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs := make([]Message, 0, len(raw))
|
|
|
|
for _, r := range raw {
|
|
|
|
m := Message{
|
|
|
|
Header: sysToHeader(r.Header),
|
|
|
|
Data: r.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection.
|
|
|
|
func (c *conn) Close() error {
|
|
|
|
return c.s.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinGroup joins a multicast group by ID.
|
|
|
|
func (c *conn) JoinGroup(group uint32) error {
|
|
|
|
return c.s.SetSockopt(
|
2017-02-28 22:59:37 +01:00
|
|
|
unix.SOL_NETLINK,
|
|
|
|
unix.NETLINK_ADD_MEMBERSHIP,
|
2017-01-09 20:33:55 +01:00
|
|
|
unsafe.Pointer(&group),
|
|
|
|
uint32(unsafe.Sizeof(group)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveGroup leaves a multicast group by ID.
|
|
|
|
func (c *conn) LeaveGroup(group uint32) error {
|
|
|
|
return c.s.SetSockopt(
|
2017-02-28 22:59:37 +01:00
|
|
|
unix.SOL_NETLINK,
|
|
|
|
unix.NETLINK_DROP_MEMBERSHIP,
|
2017-01-09 20:33:55 +01:00
|
|
|
unsafe.Pointer(&group),
|
|
|
|
uint32(unsafe.Sizeof(group)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:59:37 +01:00
|
|
|
// 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)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
// 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
|
|
|
|
// exactly the same for this unsafe cast to work
|
|
|
|
return *(*Header)(unsafe.Pointer(&r))
|
|
|
|
}
|
|
|
|
|
|
|
|
// newError converts an error number from netlink into the appropriate
|
|
|
|
// system call error for Linux.
|
|
|
|
func newError(errno int) error {
|
|
|
|
return syscall.Errno(errno)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ socket = &sysSocket{}
|
|
|
|
|
|
|
|
// A sysSocket is a socket which uses system calls for socket operations.
|
|
|
|
type sysSocket struct {
|
|
|
|
fd int
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:59:37 +01:00
|
|
|
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)
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
2017-02-28 22:59:37 +01:00
|
|
|
func (s *sysSocket) Sendmsg(p, oob []byte, to unix.Sockaddr, flags int) error {
|
|
|
|
return unix.Sendmsg(s.fd, p, oob, to, flags)
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
func (s *sysSocket) SetSockopt(level, name int, v unsafe.Pointer, l uint32) error {
|
|
|
|
return setsockopt(s.fd, level, name, v, l)
|
|
|
|
}
|