2017-01-09 20:33:55 +01:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-02 12:30:34 +01:00
|
|
|
"io"
|
2017-02-28 22:59:37 +01:00
|
|
|
"math/rand"
|
2017-11-02 12:30:34 +01:00
|
|
|
"os"
|
2017-01-09 20:33:55 +01:00
|
|
|
"sync/atomic"
|
2018-10-11 18:41:41 +02:00
|
|
|
"syscall"
|
2018-08-14 21:15:07 +02:00
|
|
|
"time"
|
2017-02-28 22:59:37 +01:00
|
|
|
|
|
|
|
"golang.org/x/net/bpf"
|
2017-01-09 20:33:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Error messages which can be returned by Validate.
|
|
|
|
var (
|
|
|
|
errMismatchedSequence = errors.New("mismatched sequence in netlink reply")
|
|
|
|
errMismatchedPID = errors.New("mismatched PID in netlink reply")
|
|
|
|
errShortErrorMessage = errors.New("not enough data for netlink error code")
|
|
|
|
)
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
// Errors which can be returned by a Socket that does not implement
|
|
|
|
// all exposed methods of Conn.
|
|
|
|
var (
|
|
|
|
errReadWriteCloserNotSupported = errors.New("raw read/write/closer not supported")
|
|
|
|
errMulticastGroupsNotSupported = errors.New("multicast groups not supported")
|
|
|
|
errBPFFiltersNotSupported = errors.New("BPF filters not supported")
|
2018-08-14 21:15:07 +02:00
|
|
|
errOptionsNotSupported = errors.New("options not supported")
|
2018-10-11 18:41:41 +02:00
|
|
|
errSetBufferNotSupported = errors.New("setting buffer sizes not supported")
|
|
|
|
errSyscallConnNotSupported = errors.New("syscall.RawConn operation not supported")
|
2017-11-02 12:30:34 +01:00
|
|
|
)
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
// A Conn is a connection to netlink. A Conn can be used to send and
|
|
|
|
// receives messages to and from netlink.
|
2018-01-25 18:20:39 +01:00
|
|
|
//
|
|
|
|
// A Conn is safe for concurrent use, but to avoid contention in
|
|
|
|
// high-throughput applications, the caller should almost certainly create a
|
|
|
|
// pool of Conns and distribute them among workers.
|
2017-01-09 20:33:55 +01:00
|
|
|
type Conn struct {
|
2017-11-02 12:30:34 +01:00
|
|
|
// sock is the operating system-specific implementation of
|
2017-01-09 20:33:55 +01:00
|
|
|
// a netlink sockets connection.
|
2017-11-02 12:30:34 +01:00
|
|
|
sock Socket
|
2017-01-09 20:33:55 +01:00
|
|
|
|
|
|
|
// seq is an atomically incremented integer used to provide sequence
|
|
|
|
// numbers when Conn.Send is called.
|
|
|
|
seq *uint32
|
2017-01-12 18:41:35 +01:00
|
|
|
|
2017-03-10 18:32:29 +01:00
|
|
|
// pid is the PID assigned by netlink.
|
|
|
|
pid uint32
|
2018-08-14 21:15:07 +02:00
|
|
|
|
|
|
|
// d provides debugging capabilities for a Conn if not nil.
|
|
|
|
d *debugger
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
// A Socket is an operating-system specific implementation of netlink
|
2017-01-09 20:33:55 +01:00
|
|
|
// sockets used by Conn.
|
2017-11-02 12:30:34 +01:00
|
|
|
type Socket interface {
|
2017-01-09 20:33:55 +01:00
|
|
|
Close() error
|
|
|
|
Send(m Message) error
|
2018-08-14 21:15:07 +02:00
|
|
|
SendMessages(m []Message) error
|
2017-01-09 20:33:55 +01:00
|
|
|
Receive() ([]Message, error)
|
|
|
|
}
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
// Dial dials a connection to netlink, using the specified netlink family.
|
2017-01-09 20:33:55 +01:00
|
|
|
// Config specifies optional configuration for Conn. If config is nil, a default
|
|
|
|
// configuration will be used.
|
2017-11-02 12:30:34 +01:00
|
|
|
func Dial(family int, config *Config) (*Conn, error) {
|
|
|
|
// Use OS-specific dial() to create Socket
|
|
|
|
c, pid, err := dial(family, config)
|
2017-01-09 20:33:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
return NewConn(c, pid), nil
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
// NewConn creates a Conn using the specified Socket and PID for netlink
|
|
|
|
// communications.
|
|
|
|
//
|
|
|
|
// NewConn is primarily useful for tests. Most applications should use
|
|
|
|
// Dial instead.
|
2018-10-11 18:41:41 +02:00
|
|
|
func NewConn(sock Socket, pid uint32) *Conn {
|
2018-08-14 21:15:07 +02:00
|
|
|
// Seed the sequence number using a random number generator.
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
seq := r.Uint32()
|
|
|
|
|
|
|
|
// Configure a debugger if arguments are set.
|
|
|
|
var d *debugger
|
|
|
|
if len(debugArgs) > 0 {
|
|
|
|
d = newDebugger(debugArgs)
|
|
|
|
}
|
2017-02-28 22:59:37 +01:00
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
return &Conn{
|
2018-10-11 18:41:41 +02:00
|
|
|
sock: sock,
|
2017-11-02 12:30:34 +01:00
|
|
|
seq: &seq,
|
|
|
|
pid: pid,
|
2018-08-14 21:15:07 +02:00
|
|
|
d: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// debug executes fn with the debugger if the debugger is not nil.
|
|
|
|
func (c *Conn) debug(fn func(d *debugger)) {
|
|
|
|
if c.d == nil {
|
|
|
|
return
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
2018-08-14 21:15:07 +02:00
|
|
|
|
|
|
|
fn(c.d)
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection.
|
|
|
|
func (c *Conn) Close() error {
|
2017-11-02 12:30:34 +01:00
|
|
|
return c.sock.Close()
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute sends a single Message to netlink using Conn.Send, receives one or more
|
|
|
|
// replies using Conn.Receive, and then checks the validity of the replies against
|
|
|
|
// the request using Validate.
|
|
|
|
//
|
|
|
|
// See the documentation of Conn.Send, Conn.Receive, and Validate for details about
|
|
|
|
// each function.
|
2018-10-11 18:41:41 +02:00
|
|
|
func (c *Conn) Execute(message Message) ([]Message, error) {
|
|
|
|
req, err := c.Send(message)
|
2017-01-09 20:33:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
replies, err := c.Receive()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Validate(req, replies); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return replies, nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
func (c *Conn) fixMsg(m *Message, ml int) {
|
|
|
|
if m.Header.Length == 0 {
|
|
|
|
m.Header.Length = uint32(nlmsgAlign(ml))
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Header.Sequence == 0 {
|
|
|
|
m.Header.Sequence = c.nextSequence()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Header.PID == 0 {
|
|
|
|
m.Header.PID = c.pid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMessages sends multiple Messages to netlink. The handling of
|
2018-10-11 18:41:41 +02:00
|
|
|
// a Header's Length, Sequence and PID fields is the same as when
|
|
|
|
// calling Send.
|
2018-08-14 21:15:07 +02:00
|
|
|
func (c *Conn) SendMessages(messages []Message) ([]Message, error) {
|
|
|
|
for idx, m := range messages {
|
|
|
|
ml := nlmsgLength(len(m.Data))
|
|
|
|
|
|
|
|
// TODO(mdlayher): fine-tune this limit.
|
|
|
|
if ml > (1024 * 32) {
|
|
|
|
return nil, errors.New("netlink message data too large")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.fixMsg(&messages[idx], ml)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.debug(func(d *debugger) {
|
|
|
|
for _, m := range messages {
|
|
|
|
d.debugf(1, "send msgs: %+v", m)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := c.sock.SendMessages(messages); err != nil {
|
|
|
|
c.debug(func(d *debugger) {
|
|
|
|
d.debugf(1, "send msgs: err: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
// Send sends a single Message to netlink. In most cases, a Header's Length,
|
2017-01-09 20:33:55 +01:00
|
|
|
// Sequence, and PID fields should be set to 0, so they can be populated
|
|
|
|
// automatically before the Message is sent. On success, Send returns a copy
|
|
|
|
// of the Message with all parameters populated, for later validation.
|
|
|
|
//
|
2018-10-11 18:41:41 +02:00
|
|
|
// If Header.Length is 0, it will be automatically populated using the
|
2017-01-09 20:33:55 +01:00
|
|
|
// correct length for the Message, including its payload.
|
|
|
|
//
|
2018-10-11 18:41:41 +02:00
|
|
|
// If Header.Sequence is 0, it will be automatically populated using the
|
2017-01-09 20:33:55 +01:00
|
|
|
// next sequence number for this connection.
|
|
|
|
//
|
2018-10-11 18:41:41 +02:00
|
|
|
// If Header.PID is 0, it will be automatically populated using a PID
|
2017-01-12 18:41:35 +01:00
|
|
|
// assigned by netlink.
|
2018-10-11 18:41:41 +02:00
|
|
|
func (c *Conn) Send(message Message) (Message, error) {
|
|
|
|
ml := nlmsgLength(len(message.Data))
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2017-01-09 20:37:59 +01:00
|
|
|
// TODO(mdlayher): fine-tune this limit.
|
|
|
|
if ml > (1024 * 32) {
|
2017-01-09 20:33:55 +01:00
|
|
|
return Message{}, errors.New("netlink message data too large")
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
c.fixMsg(&message, ml)
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
c.debug(func(d *debugger) {
|
2018-10-11 18:41:41 +02:00
|
|
|
d.debugf(1, "send: %+v", message)
|
2018-08-14 21:15:07 +02:00
|
|
|
})
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
if err := c.sock.Send(message); err != nil {
|
2018-08-14 21:15:07 +02:00
|
|
|
c.debug(func(d *debugger) {
|
|
|
|
d.debugf(1, "send: err: %v", err)
|
|
|
|
})
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
return message, nil
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Receive receives one or more messages from netlink. Multi-part messages are
|
|
|
|
// handled transparently and returned as a single slice of Messages, with the
|
2017-01-12 18:41:35 +01:00
|
|
|
// final empty "multi-part done" message removed.
|
|
|
|
//
|
|
|
|
// If any of the messages indicate a netlink error, that error will be returned.
|
2017-01-09 20:33:55 +01:00
|
|
|
func (c *Conn) Receive() ([]Message, error) {
|
|
|
|
msgs, err := c.receive()
|
|
|
|
if err != nil {
|
2018-08-14 21:15:07 +02:00
|
|
|
c.debug(func(d *debugger) {
|
|
|
|
d.debugf(1, "recv: err: %v", err)
|
|
|
|
})
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
c.debug(func(d *debugger) {
|
|
|
|
for _, m := range msgs {
|
|
|
|
d.debugf(1, "recv: %+v", m)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
// When using nltest, it's possible for zero messages to be returned by receive.
|
|
|
|
if len(msgs) == 0 {
|
|
|
|
return msgs, nil
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
// Trim the final message with multi-part done indicator if
|
2017-11-02 12:30:34 +01:00
|
|
|
// present.
|
2017-01-09 20:33:55 +01:00
|
|
|
if m := msgs[len(msgs)-1]; m.Header.Flags&HeaderFlagsMulti != 0 && m.Header.Type == HeaderTypeDone {
|
|
|
|
return msgs[:len(msgs)-1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// receive is the internal implementation of Conn.Receive, which can be called
|
|
|
|
// recursively to handle multi-part messages.
|
|
|
|
func (c *Conn) receive() ([]Message, error) {
|
2018-08-14 21:15:07 +02:00
|
|
|
var res []Message
|
|
|
|
for {
|
|
|
|
msgs, err := c.sock.Receive()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
// If this message is multi-part, we will need to perform an recursive call
|
|
|
|
// to continue draining the socket
|
|
|
|
var multi bool
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
for _, m := range msgs {
|
|
|
|
if err := checkMessage(m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
// Does this message indicate a multi-part message?
|
|
|
|
if m.Header.Flags&HeaderFlagsMulti == 0 {
|
|
|
|
// No, check the next messages.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does this message indicate the last message in a series of
|
|
|
|
// multi-part messages from a single read?
|
|
|
|
multi = m.Header.Type != HeaderTypeDone
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
res = append(res, msgs...)
|
2017-01-09 20:33:55 +01:00
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
if !multi {
|
|
|
|
// No more messages coming.
|
|
|
|
return res, nil
|
|
|
|
}
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 12:30:34 +01:00
|
|
|
// An fder is a Socket that supports retrieving its raw file descriptor.
|
|
|
|
type fder interface {
|
|
|
|
Socket
|
|
|
|
FD() int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.ReadWriteCloser = &fileReadWriteCloser{}
|
|
|
|
|
|
|
|
// A fileReadWriteCloser is a limited *os.File which only allows access to its
|
|
|
|
// Read and Write methods.
|
|
|
|
type fileReadWriteCloser struct {
|
|
|
|
f *os.File
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements io.ReadWriteCloser.
|
|
|
|
func (rwc *fileReadWriteCloser) Read(b []byte) (int, error) { return rwc.f.Read(b) }
|
|
|
|
|
|
|
|
// Write implements io.ReadWriteCloser.
|
|
|
|
func (rwc *fileReadWriteCloser) Write(b []byte) (int, error) { return rwc.f.Write(b) }
|
|
|
|
|
|
|
|
// Close implements io.ReadWriteCloser.
|
|
|
|
func (rwc *fileReadWriteCloser) Close() error { return rwc.f.Close() }
|
|
|
|
|
|
|
|
// ReadWriteCloser returns a raw io.ReadWriteCloser backed by the connection
|
|
|
|
// of the Conn.
|
|
|
|
//
|
|
|
|
// ReadWriteCloser is intended for advanced use cases, such as those that do
|
|
|
|
// not involve standard netlink message passing.
|
|
|
|
//
|
|
|
|
// Once invoked, it is the caller's responsibility to ensure that operations
|
|
|
|
// performed using Conn and the raw io.ReadWriteCloser do not conflict with
|
|
|
|
// each other. In almost all scenarios, only one of the two should be used.
|
|
|
|
func (c *Conn) ReadWriteCloser() (io.ReadWriteCloser, error) {
|
|
|
|
fc, ok := c.sock.(fder)
|
|
|
|
if !ok {
|
|
|
|
return nil, errReadWriteCloserNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return &fileReadWriteCloser{
|
|
|
|
// Backing the io.ReadWriteCloser with an *os.File enables easy reading
|
|
|
|
// and writing without more system call boilerplate.
|
|
|
|
f: os.NewFile(uintptr(fc.FD()), "netlink"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A groupJoinLeaver is a Socket that supports joining and leaving
|
|
|
|
// netlink multicast groups.
|
|
|
|
type groupJoinLeaver interface {
|
|
|
|
Socket
|
|
|
|
JoinGroup(group uint32) error
|
|
|
|
LeaveGroup(group uint32) error
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
// JoinGroup joins a netlink multicast group by its ID.
|
|
|
|
func (c *Conn) JoinGroup(group uint32) error {
|
2017-11-02 12:30:34 +01:00
|
|
|
gc, ok := c.sock.(groupJoinLeaver)
|
|
|
|
if !ok {
|
|
|
|
return errMulticastGroupsNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return gc.JoinGroup(group)
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveGroup leaves a netlink multicast group by its ID.
|
|
|
|
func (c *Conn) LeaveGroup(group uint32) error {
|
2017-11-02 12:30:34 +01:00
|
|
|
gc, ok := c.sock.(groupJoinLeaver)
|
|
|
|
if !ok {
|
|
|
|
return errMulticastGroupsNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return gc.LeaveGroup(group)
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
// A bpfSetter is a Socket that supports setting and removing BPF filters.
|
2017-11-02 12:30:34 +01:00
|
|
|
type bpfSetter interface {
|
|
|
|
Socket
|
|
|
|
bpf.Setter
|
2018-08-14 21:15:07 +02:00
|
|
|
RemoveBPF() error
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-02 12:30:34 +01:00
|
|
|
bc, ok := c.sock.(bpfSetter)
|
|
|
|
if !ok {
|
|
|
|
return errBPFFiltersNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return bc.SetBPF(filter)
|
2017-02-28 22:59:37 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 21:15:07 +02:00
|
|
|
// RemoveBPF removes a BPF filter from a Conn.
|
|
|
|
func (c *Conn) RemoveBPF() error {
|
|
|
|
s, ok := c.sock.(bpfSetter)
|
|
|
|
if !ok {
|
|
|
|
return errBPFFiltersNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.RemoveBPF()
|
|
|
|
}
|
|
|
|
|
|
|
|
// A ConnOption is a boolean option that may be set for a Conn.
|
|
|
|
type ConnOption int
|
|
|
|
|
|
|
|
// Possible ConnOption values. These constants are equivalent to the Linux
|
|
|
|
// setsockopt boolean options for netlink sockets.
|
|
|
|
const (
|
|
|
|
PacketInfo ConnOption = iota
|
|
|
|
BroadcastError
|
|
|
|
NoENOBUFS
|
|
|
|
ListenAllNSID
|
|
|
|
CapAcknowledge
|
|
|
|
)
|
|
|
|
|
|
|
|
// An optionSetter is a Socket that supports setting netlink options.
|
|
|
|
type optionSetter interface {
|
|
|
|
Socket
|
|
|
|
SetOption(option ConnOption, enable bool) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOption enables or disables a netlink socket option for the Conn.
|
|
|
|
func (c *Conn) SetOption(option ConnOption, enable bool) error {
|
|
|
|
fc, ok := c.sock.(optionSetter)
|
|
|
|
if !ok {
|
|
|
|
return errOptionsNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return fc.SetOption(option, enable)
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
// A bufferSetter is a Socket that supports setting connection buffer sizes.
|
|
|
|
type bufferSetter interface {
|
|
|
|
Socket
|
|
|
|
SetReadBuffer(bytes int) error
|
|
|
|
SetWriteBuffer(bytes int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadBuffer sets the size of the operating system's receive buffer
|
|
|
|
// associated with the Conn.
|
|
|
|
func (c *Conn) SetReadBuffer(bytes int) error {
|
|
|
|
conn, ok := c.sock.(bufferSetter)
|
|
|
|
if !ok {
|
|
|
|
return errSetBufferNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.SetReadBuffer(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteBuffer sets the size of the operating system's transmit buffer
|
|
|
|
// associated with the Conn.
|
|
|
|
func (c *Conn) SetWriteBuffer(bytes int) error {
|
|
|
|
conn, ok := c.sock.(bufferSetter)
|
|
|
|
if !ok {
|
|
|
|
return errSetBufferNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.SetWriteBuffer(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ syscall.Conn = &Conn{}
|
|
|
|
|
|
|
|
// TODO(mdlayher): mutex or similar to enforce syscall.RawConn contract of
|
|
|
|
// FD remaining valid for duration of calls?
|
|
|
|
|
|
|
|
// SyscallConn returns a raw network connection. This implements the
|
|
|
|
// syscall.Conn interface.
|
|
|
|
//
|
|
|
|
// Only the Control method of the returned syscall.RawConn is currently
|
|
|
|
// implemented.
|
|
|
|
//
|
|
|
|
// SyscallConn is intended for advanced use cases, such as getting and setting
|
|
|
|
// arbitrary socket options using the netlink socket's file descriptor.
|
|
|
|
//
|
|
|
|
// Once invoked, it is the caller's responsibility to ensure that operations
|
|
|
|
// performed using Conn and the syscall.RawConn do not conflict with
|
|
|
|
// each other.
|
|
|
|
func (c *Conn) SyscallConn() (syscall.RawConn, error) {
|
|
|
|
conn, ok := c.sock.(fder)
|
|
|
|
if !ok {
|
|
|
|
return nil, errSyscallConnNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rawConn{
|
|
|
|
fd: uintptr(conn.FD()),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ syscall.RawConn = &rawConn{}
|
|
|
|
|
|
|
|
// A rawConn is a syscall.RawConn.
|
|
|
|
type rawConn struct {
|
|
|
|
fd uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawConn) Control(f func(fd uintptr)) error {
|
|
|
|
f(rc.fd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(mdlayher): implement Read and Write?
|
|
|
|
|
|
|
|
func (rc *rawConn) Read(_ func(fd uintptr) (done bool)) error { return errSyscallConnNotSupported }
|
|
|
|
func (rc *rawConn) Write(_ func(fd uintptr) (done bool)) error { return errSyscallConnNotSupported }
|
|
|
|
|
2017-01-09 20:33:55 +01:00
|
|
|
// nextSequence atomically increments Conn's sequence number and returns
|
|
|
|
// the incremented value.
|
|
|
|
func (c *Conn) nextSequence() uint32 {
|
|
|
|
return atomic.AddUint32(c.seq, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates one or more reply Messages against a request Message,
|
|
|
|
// ensuring that they contain matching sequence numbers and PIDs.
|
|
|
|
func Validate(request Message, replies []Message) error {
|
|
|
|
for _, m := range replies {
|
2017-01-12 18:41:35 +01:00
|
|
|
// Check for mismatched sequence, unless:
|
|
|
|
// - request had no sequence, meaning we are probably validating
|
|
|
|
// a multicast reply
|
|
|
|
if m.Header.Sequence != request.Header.Sequence && request.Header.Sequence != 0 {
|
2017-01-09 20:33:55 +01:00
|
|
|
return errMismatchedSequence
|
|
|
|
}
|
2017-01-12 18:41:35 +01:00
|
|
|
|
|
|
|
// Check for mismatched PID, unless:
|
|
|
|
// - request had no PID, meaning we are either:
|
|
|
|
// - validating a multicast reply
|
|
|
|
// - netlink has not yet assigned us a PID
|
|
|
|
// - response had no PID, meaning it's from the kernel as a multicast reply
|
|
|
|
if m.Header.PID != request.Header.PID && request.Header.PID != 0 && m.Header.PID != 0 {
|
2017-01-09 20:33:55 +01:00
|
|
|
return errMismatchedPID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains options for a Conn.
|
|
|
|
type Config struct {
|
|
|
|
// Groups is a bitmask which specifies multicast groups. If set to 0,
|
|
|
|
// no multicast group subscriptions will be made.
|
|
|
|
Groups uint32
|
2018-01-25 18:20:39 +01:00
|
|
|
|
2018-10-11 18:41:41 +02:00
|
|
|
// Network namespace the Conn needs to operate in. If set to 0,
|
|
|
|
// no network namespace will be entered.
|
|
|
|
NetNS int
|
2017-01-09 20:33:55 +01:00
|
|
|
}
|