Skip to content

Commit

Permalink
feat: Handle OobData
Browse files Browse the repository at this point in the history
Adds OobData []byte to the Message struct.
During the Recieve() function, this field is populated with the raw
bytes of any control messages recieved from the recvmsg call.

An example of how to process the OobData field has been included.

This feature requires the user to set EnableControlMessages in the
Config. This is to avoid allocating an additional page per recvmsg
call unless it's absolutely necessary.

Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
  • Loading branch information
dave-tucker committed Mar 12, 2024
1 parent 657f7da commit 5cfa69a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 11 deletions.
5 changes: 5 additions & 0 deletions align.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ const sizeofAttribute = 4

// #define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr)))
var nlaHeaderLen = nlaAlign(sizeofAttribute)

// #define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
func cmsgAlign(len int) int {
return ((len) + int(unsafe.Sizeof(int(0))-1)) & ^(int(unsafe.Sizeof(int(0)) - 1))
}
6 changes: 6 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,10 @@ type Config struct {
// When possible, setting Strict to true is recommended for applications
// running on modern Linux kernels.
Strict bool

// EnableControlMessages enables the use of control messages for
// netlink. This option is intended for advanced use cases where the
// caller needs to receive control messages that contain ancillary data.
// For example, when using options like `ListenAllNSID`.
EnableControlMessages bool
}
27 changes: 18 additions & 9 deletions conn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var _ Socket = &conn{}

// A conn is the Linux implementation of a netlink sockets connection.
type conn struct {
s *socket.Conn
s *socket.Conn
enableControlMessages bool
}

// dial is the entry point for Dial. dial opens a netlink socket using
Expand Down Expand Up @@ -70,7 +71,7 @@ func newConn(s *socket.Conn, config *Config) (*conn, uint32, error) {
return nil, 0, err
}

c := &conn{s: s}
c := &conn{s: s, enableControlMessages: config.EnableControlMessages}
if config.Strict {
// The caller has requested the strict option set. Historically we have
// recommended checking for ENOPROTOOPT if the kernel does not support
Expand Down Expand Up @@ -124,9 +125,6 @@ func (c *conn) Receive() ([]Message, error) {
b := make([]byte, os.Getpagesize())
for {
// Peek at the buffer to see how many bytes are available.
//
// TODO(mdlayher): deal with OOB message data if available, such as
// when PacketInfo ConnOption is true.
n, _, _, _, err := c.s.Recvmsg(context.Background(), b, nil, unix.MSG_PEEK)
if err != nil {
return nil, err
Expand All @@ -141,12 +139,23 @@ func (c *conn) Receive() ([]Message, error) {
b = make([]byte, len(b)*2)
}

// Only allocate a buffer for control messages if they are enabled.
var oob []byte
if c.enableControlMessages {
oob = make([]byte, os.Getpagesize())
}

// Read out all available messages
n, _, _, _, err := c.s.Recvmsg(context.Background(), b, nil, 0)
n, oobn, _, _, err := c.s.Recvmsg(context.Background(), b, oob, 0)
if err != nil {
return nil, err
}

var rawOob []byte
if c.enableControlMessages {
rawOob = oob[:cmsgAlign(oobn)]
}

raw, err := syscall.ParseNetlinkMessage(b[:nlmsgAlign(n)])
if err != nil {
return nil, err
Expand All @@ -155,10 +164,10 @@ func (c *conn) Receive() ([]Message, error) {
msgs := make([]Message, 0, len(raw))
for _, r := range raw {
m := Message{
Header: sysToHeader(r.Header),
Data: r.Data,
Header: sysToHeader(r.Header),
Data: r.Data,
OobData: rawOob,
}

msgs = append(msgs, m)
}

Expand Down
57 changes: 57 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nlenc"
"github.com/mdlayher/netlink/nltest"
"golang.org/x/sys/unix"
)

// This example demonstrates using a netlink.Conn to execute requests against
Expand Down Expand Up @@ -108,3 +109,59 @@ func exampleAttributes() []byte {
},
})
}

func ExampleConn_listenMulticastAllNSID() {
const (
// Speak to route netlink using netlink
familyRoute = 0

// Listen for events triggered by addition or deletion of
// network interfaces
rtmGroupLink = 0x1
)

c, err := netlink.Dial(familyRoute, &netlink.Config{
// Groups is a bitmask; more than one group can be specified
// by OR'ing multiple group values together
Groups: rtmGroupLink,
// Enable control messages to receive the netnsid
// since we're going to set NETLINK_LISTEN_ALL_NSID
EnableControlMessages: true,
})
if err != nil {
log.Fatalf("failed to dial netlink: %v", err)
}
defer c.Close()
c.SetOption(netlink.ListenAllNSID, true)

for {
// Listen for netlink messages triggered by multicast groups
msgs, err := c.Receive()
if err != nil {
log.Fatalf("failed to receive messages: %v", err)
}

// Iterate over recieved messages and print them
for _, msg := range msgs {
// If the message contains oob data, parse it and print the netnsid
if msg.OobData != nil {
// ParseSocketControlMessage returns a slice of ControlMessages
cmsg, err := unix.ParseSocketControlMessage(msg.OobData)
if err != nil {
log.Printf("Error parsing oob data: %v", err)
continue
}
// Iterate over the control messages, find the
// one with level SOL_NETLINK and type NETLINK_LISTEN_ALL_NSID
for _, oob := range cmsg {
if oob.Header.Level == unix.SOL_NETLINK && oob.Header.Type == unix.NETLINK_LISTEN_ALL_NSID {

Check failure on line 157 in example_test.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: unix.SOL_NETLINK

Check failure on line 157 in example_test.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: unix.NETLINK_LISTEN_ALL_NSID
netnsid := int(oob.Data[0])
log.Printf("netnsid: %d", netnsid)
break
}
}
}
log.Printf("msg: %+v", msg)
}
}
}
6 changes: 4 additions & 2 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ 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.
// It may also contain out-of-band data, which was sent along with the message.
//
// Data is often populated with netlink attributes. For easy encoding and
// decoding of attributes, see the AttributeDecoder and AttributeEncoder types.
type Message struct {
Header Header
Data []byte
Header Header
Data []byte
OobData []byte
}

// MarshalBinary marshals a Message into a byte slice.
Expand Down

0 comments on commit 5cfa69a

Please sign in to comment.