780 lines
19 KiB
HTML
780 lines
19 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of NETLINK</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>NETLINK</H1>
|
|
Section: Linux Programmer's Manual (7)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
netlink - communication between kernel and user space (AF_NETLINK)
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/asm/types.h">asm/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/linux/netlink.h">linux/netlink.h</A>></B>
|
|
|
|
<B>netlink_socket = socket(AF_NETLINK, </B><I>socket_type</I><B>, </B><I>netlink_family</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
Netlink is used to transfer information between the kernel and
|
|
user-space processes.
|
|
It consists of a standard sockets-based interface for user space
|
|
processes and an internal kernel API for kernel modules.
|
|
The internal kernel interface is not documented in this manual page.
|
|
There is also an obsolete netlink interface
|
|
via netlink character devices; this interface is not documented here
|
|
and is provided only for backward compatibility.
|
|
<P>
|
|
|
|
Netlink is a datagram-oriented service.
|
|
Both
|
|
<B>SOCK_RAW</B>
|
|
|
|
and
|
|
<B>SOCK_DGRAM</B>
|
|
|
|
are valid values for
|
|
<I>socket_type</I>.
|
|
|
|
However, the netlink protocol does not distinguish between datagram
|
|
and raw sockets.
|
|
<P>
|
|
|
|
<I>netlink_family</I>
|
|
|
|
selects the kernel module or netlink group to communicate with.
|
|
The currently assigned netlink families are:
|
|
<DL COMPACT>
|
|
<DT id="1"><B>NETLINK_ROUTE</B>
|
|
|
|
<DD>
|
|
Receives routing and link updates and may be used to modify the routing
|
|
tables (both IPv4 and IPv6), IP addresses, link parameters,
|
|
neighbor setups, queueing disciplines, traffic classes and
|
|
packet classifiers (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+rtnetlink">rtnetlink</A></B>(7)).
|
|
|
|
<DT id="2"><B>NETLINK_W1</B> (Linux 2.6.13 to 2.16.17)
|
|
|
|
<DD>
|
|
Messages from 1-wire subsystem.
|
|
<DT id="3"><B>NETLINK_USERSOCK</B>
|
|
|
|
<DD>
|
|
Reserved for user-mode socket protocols.
|
|
<DT id="4"><B>NETLINK_FIREWALL</B> (up to and including Linux 3.4)
|
|
|
|
<DD>
|
|
|
|
Transport IPv4 packets from netfilter to user space.
|
|
Used by
|
|
<I>ip_queue</I>
|
|
|
|
kernel module.
|
|
After a long period of being declared obsolete (in favor of the more advanced
|
|
<I>nfnetlink_queue</I>
|
|
|
|
feature),
|
|
<B>NETLINK_FIREWALL</B>
|
|
|
|
was removed in Linux 3.5.
|
|
<DT id="5"><B>NETLINK_INET_DIAG</B> (since Linux 2.6.14)
|
|
|
|
<DD>
|
|
Query information about sockets of various protocol families from the kernel
|
|
(see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sock_diag">sock_diag</A></B>(7)).
|
|
|
|
<DT id="6"><B>NETLINK_SOCK_DIAG</B> (since Linux 3.3)
|
|
|
|
<DD>
|
|
|
|
A synonym for
|
|
<B>NETLINK_INET_DIAG</B>.
|
|
|
|
<DT id="7"><B>NETLINK_NFLOG</B> (up to and including Linux 3.16)
|
|
|
|
<DD>
|
|
Netfilter/iptables ULOG.
|
|
<DT id="8"><B>NETLINK_XFRM</B>
|
|
|
|
<DD>
|
|
|
|
IPsec.
|
|
<DT id="9"><B>NETLINK_SELINUX</B> (since Linux 2.6.4)
|
|
|
|
<DD>
|
|
SELinux event notifications.
|
|
<DT id="10"><B>NETLINK_ISCSI</B> (since Linux 2.6.15)
|
|
|
|
<DD>
|
|
|
|
Open-iSCSI.
|
|
<DT id="11"><B>NETLINK_AUDIT</B> (since Linux 2.6.6)
|
|
|
|
<DD>
|
|
|
|
Auditing.
|
|
<DT id="12"><B>NETLINK_FIB_LOOKUP</B> (since Linux 2.6.13)
|
|
|
|
<DD>
|
|
|
|
Access to FIB lookup from user space.
|
|
<DT id="13"><B>NETLINK_CONNECTOR</B> (since Linux 2.6.14)
|
|
|
|
<DD>
|
|
Kernel connector.
|
|
See
|
|
<I>Documentation/connector/*</I>
|
|
|
|
in the Linux kernel source tree for further information.
|
|
<DT id="14"><B>NETLINK_NETFILTER</B> (since Linux 2.6.14)
|
|
|
|
<DD>
|
|
|
|
Netfilter subsystem.
|
|
<DT id="15"><B>NETLINK_SCSITRANSPORT</B> (since Linux 2.6.19)
|
|
|
|
<DD>
|
|
|
|
|
|
SCSI Transports.
|
|
<DT id="16"><B>NETLINK_RDMA</B> (since Linux 3.0)
|
|
|
|
<DD>
|
|
|
|
|
|
Infiniband RDMA.
|
|
<DT id="17"><B>NETLINK_IP6_FW</B> (up to and including Linux 3.4)
|
|
|
|
<DD>
|
|
Transport IPv6 packets from netfilter to user space.
|
|
Used by
|
|
<I>ip6_queue</I>
|
|
|
|
kernel module.
|
|
<DT id="18"><B>NETLINK_DNRTMSG</B>
|
|
|
|
<DD>
|
|
DECnet routing messages.
|
|
<DT id="19"><B>NETLINK_KOBJECT_UEVENT</B> (since Linux 2.6.10)
|
|
|
|
<DD>
|
|
|
|
Kernel messages to user space.
|
|
<DT id="20"><B>NETLINK_GENERIC</B> (since Linux 2.6.15)
|
|
|
|
<DD>
|
|
Generic netlink family for simplified netlink usage.
|
|
<DT id="21"><B>NETLINK_CRYPTO</B> (since Linux 3.2)
|
|
|
|
<DD>
|
|
|
|
|
|
Netlink interface to request information about ciphers registered
|
|
with the kernel crypto API as well as allow configuration of the
|
|
kernel crypto API.
|
|
</DL>
|
|
<P>
|
|
|
|
Netlink messages consist of a byte stream with one or multiple
|
|
<I>nlmsghdr</I>
|
|
|
|
headers and associated payload.
|
|
The byte stream should be accessed only with the standard
|
|
<B>NLMSG_*</B>
|
|
|
|
macros.
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3)
|
|
|
|
for further information.
|
|
<P>
|
|
|
|
In multipart messages (multiple
|
|
<I>nlmsghdr</I>
|
|
|
|
headers with associated payload in one byte stream) the first and all
|
|
following headers have the
|
|
<B>NLM_F_MULTI</B>
|
|
|
|
flag set, except for the last header which has the type
|
|
<B>NLMSG_DONE</B>.
|
|
|
|
<P>
|
|
|
|
After each
|
|
<I>nlmsghdr</I>
|
|
|
|
the payload follows.
|
|
<P>
|
|
|
|
|
|
|
|
struct nlmsghdr {
|
|
<BR> __u32 nlmsg_len; /* Length of message including header */
|
|
<BR> __u16 nlmsg_type; /* Type of message content */
|
|
<BR> __u16 nlmsg_flags; /* Additional flags */
|
|
<BR> __u32 nlmsg_seq; /* Sequence number */
|
|
<BR> __u32 nlmsg_pid; /* Sender port ID */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
<I>nlmsg_type</I>
|
|
|
|
can be one of the standard message types:
|
|
<B>NLMSG_NOOP</B>
|
|
|
|
message is to be ignored,
|
|
<B>NLMSG_ERROR</B>
|
|
|
|
message signals an error and the payload contains an
|
|
<I>nlmsgerr</I>
|
|
|
|
structure,
|
|
<B>NLMSG_DONE</B>
|
|
|
|
message terminates a multipart message.
|
|
<P>
|
|
|
|
|
|
|
|
struct nlmsgerr {
|
|
<BR> int error; /* Negative errno or 0 for acknowledgements */
|
|
<BR> struct nlmsghdr msg; /* Message header that caused the error */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
A netlink family usually specifies more message types, see the
|
|
appropriate manual pages for that, for example,
|
|
<B><A HREF="/cgi-bin/man/man2html?7+rtnetlink">rtnetlink</A></B>(7)
|
|
|
|
for
|
|
<B>NETLINK_ROUTE</B>.
|
|
|
|
<TABLE>
|
|
<TR VALIGN=top><TD COLSPAN=2>Standard flag bits in <I>nlmsg_flags</I><BR></TD></TR>
|
|
<TR VALIGN=top><TD COLSPAN=2><HR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_REQUEST</B></TD><TD>Must be set on all request messages.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_MULTI</B></TD><TD>
|
|
The message is part of a multipart message terminated by
|
|
<B>NLMSG_DONE</B>.
|
|
|
|
<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_ACK</B></TD><TD>Request for an acknowledgment on success.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_ECHO</B></TD><TD>Echo this request.<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
|
|
<TABLE>
|
|
<TR VALIGN=top><TD COLSPAN=2>Additional flag bits for GET requests<BR></TD></TR>
|
|
<TR VALIGN=top><TD COLSPAN=2><HR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_ROOT</B></TD><TD>Return the complete table instead of a single entry.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_MATCH</B></TD><TD>
|
|
Return all entries matching criteria passed in message content.
|
|
Not implemented yet.
|
|
<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_ATOMIC</B></TD><TD>Return an atomic snapshot of the table.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_DUMP</B></TD><TD>
|
|
Convenience macro; equivalent to
|
|
<BR>
|
|
|
|
(NLM_F_ROOT|NLM_F_MATCH).
|
|
<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
|
|
<P>
|
|
|
|
Note that
|
|
<B>NLM_F_ATOMIC</B>
|
|
|
|
requires the
|
|
<B>CAP_NET_ADMIN</B>
|
|
|
|
capability or an effective UID of 0.
|
|
<TABLE>
|
|
<TR VALIGN=top><TD COLSPAN=2>Additional flag bits for NEW requests<BR></TD></TR>
|
|
<TR VALIGN=top><TD COLSPAN=2><HR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_REPLACE</B></TD><TD>Replace existing matching object.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_EXCL</B></TD><TD>Don't replace if the object already exists.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_CREATE</B></TD><TD>Create object if it doesn't already exist.<BR></TD></TR>
|
|
<TR VALIGN=top><TD><B>NLM_F_APPEND</B></TD><TD>Add to the end of the object list.<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
|
|
<I>nlmsg_seq</I>
|
|
|
|
and
|
|
<I>nlmsg_pid</I>
|
|
|
|
are used to track messages.
|
|
<I>nlmsg_pid</I>
|
|
|
|
shows the origin of the message.
|
|
Note that there isn't a 1:1 relationship between
|
|
<I>nlmsg_pid</I>
|
|
|
|
and the PID of the process if the message originated from a netlink
|
|
socket.
|
|
See the
|
|
<B>ADDRESS FORMATS</B>
|
|
|
|
section for further information.
|
|
<P>
|
|
|
|
Both
|
|
<I>nlmsg_seq</I>
|
|
|
|
and
|
|
<I>nlmsg_pid</I>
|
|
|
|
|
|
are opaque to netlink core.
|
|
<P>
|
|
|
|
Netlink is not a reliable protocol.
|
|
It tries its best to deliver a message to its destination(s),
|
|
but may drop messages when an out-of-memory condition or
|
|
other error occurs.
|
|
For reliable transfer the sender can request an
|
|
acknowledgement from the receiver by setting the
|
|
<B>NLM_F_ACK</B>
|
|
|
|
flag.
|
|
An acknowledgment is an
|
|
<B>NLMSG_ERROR</B>
|
|
|
|
packet with the error field set to 0.
|
|
The application must generate acknowledgements for
|
|
received messages itself.
|
|
The kernel tries to send an
|
|
<B>NLMSG_ERROR</B>
|
|
|
|
message for every failed packet.
|
|
A user process should follow this convention too.
|
|
<P>
|
|
|
|
However, reliable transmissions from kernel to user are impossible
|
|
in any case.
|
|
The kernel can't send a netlink message if the socket buffer is full:
|
|
the message will be dropped and the kernel and the user-space process will
|
|
no longer have the same view of kernel state.
|
|
It is up to the application to detect when this happens (via the
|
|
<B>ENOBUFS</B>
|
|
|
|
error returned by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2))
|
|
|
|
and resynchronize.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Address formats</H3>
|
|
|
|
The
|
|
<I>sockaddr_nl</I>
|
|
|
|
structure describes a netlink client in user space or in the kernel.
|
|
A
|
|
<I>sockaddr_nl</I>
|
|
|
|
can be either unicast (only sent to one peer) or sent to
|
|
netlink multicast groups
|
|
(<I>nl_groups</I>
|
|
|
|
not equal 0).
|
|
<P>
|
|
|
|
|
|
|
|
struct sockaddr_nl {
|
|
<BR> sa_family_t nl_family; /* AF_NETLINK */
|
|
<BR> unsigned short nl_pad; /* Zero */
|
|
<BR> pid_t nl_pid; /* Port ID */
|
|
<BR> __u32 nl_groups; /* Multicast groups mask */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
<I>nl_pid</I>
|
|
|
|
is the unicast address of netlink socket.
|
|
It's always 0 if the destination is in the kernel.
|
|
For a user-space process,
|
|
<I>nl_pid</I>
|
|
|
|
is usually the PID of the process owning the destination socket.
|
|
However,
|
|
<I>nl_pid</I>
|
|
|
|
identifies a netlink socket, not a process.
|
|
If a process owns several netlink
|
|
sockets, then
|
|
<I>nl_pid</I>
|
|
|
|
can be equal to the process ID only for at most one socket.
|
|
There are two ways to assign
|
|
<I>nl_pid</I>
|
|
|
|
to a netlink socket.
|
|
If the application sets
|
|
<I>nl_pid</I>
|
|
|
|
before calling
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2),
|
|
|
|
then it is up to the application to make sure that
|
|
<I>nl_pid</I>
|
|
|
|
is unique.
|
|
If the application sets it to 0, the kernel takes care of assigning it.
|
|
The kernel assigns the process ID to the first netlink socket the process
|
|
opens and assigns a unique
|
|
<I>nl_pid</I>
|
|
|
|
to every netlink socket that the process subsequently creates.
|
|
<P>
|
|
|
|
<I>nl_groups</I>
|
|
|
|
is a bit mask with every bit representing a netlink group number.
|
|
Each netlink family has a set of 32 multicast groups.
|
|
When
|
|
<B><A HREF="/cgi-bin/man/man2html?2+bind">bind</A></B>(2)
|
|
|
|
is called on the socket, the
|
|
<I>nl_groups</I>
|
|
|
|
field in the
|
|
<I>sockaddr_nl</I>
|
|
|
|
should be set to a bit mask of the groups which it wishes to listen to.
|
|
The default value for this field is zero which means that no multicasts
|
|
will be received.
|
|
A socket may multicast messages to any of the multicast groups by setting
|
|
<I>nl_groups</I>
|
|
|
|
to a bit mask of the groups it wishes to send to when it calls
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
|
|
|
|
or does a
|
|
<B><A HREF="/cgi-bin/man/man2html?2+connect">connect</A></B>(2).
|
|
|
|
Only processes with an effective UID of 0 or the
|
|
<B>CAP_NET_ADMIN</B>
|
|
|
|
capability may send or listen to a netlink multicast group.
|
|
Since Linux 2.6.13,
|
|
|
|
messages can't be broadcast to multiple groups.
|
|
Any replies to a message received for a multicast group should be
|
|
sent back to the sending PID and the multicast group.
|
|
Some Linux kernel subsystems may additionally allow other users
|
|
to send and/or receive messages.
|
|
As at Linux 3.0, the
|
|
<B>NETLINK_KOBJECT_UEVENT</B>,
|
|
|
|
<B>NETLINK_GENERIC</B>,
|
|
|
|
<B>NETLINK_ROUTE</B>,
|
|
|
|
and
|
|
<B>NETLINK_SELINUX</B>
|
|
|
|
groups allow other users to receive messages.
|
|
No groups allow other users to send messages.
|
|
<P>
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H3>Socket options</H3>
|
|
|
|
To set or get a netlink socket option, call
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getsockopt">getsockopt</A></B>(2)
|
|
|
|
to read or
|
|
<B><A HREF="/cgi-bin/man/man2html?2+setsockopt">setsockopt</A></B>(2)
|
|
|
|
to write the option with the option level argument set to
|
|
<B>SOL_NETLINK</B>.
|
|
|
|
Unless otherwise noted,
|
|
<I>optval</I>
|
|
|
|
is a pointer to an
|
|
<I>int</I>.
|
|
|
|
<DL COMPACT>
|
|
<DT id="22"><B>NETLINK_PKTINFO</B> (since Linux 2.6.14)
|
|
|
|
<DD>
|
|
|
|
|
|
Enable
|
|
<B>nl_pktinfo</B>
|
|
|
|
control messages for received packets to get the extended
|
|
destination group number.
|
|
<DT id="23"><B>NETLINK_ADD_MEMBERSHIP</B>, <B>NETLINK_DROP_MEMBERSHIP</B> (since Linux 2.6.14)
|
|
|
|
<DD>
|
|
|
|
|
|
Join/leave a group specified by
|
|
<I>optval</I>.
|
|
|
|
<DT id="24"><B>NETLINK_LIST_MEMBERSHIPS</B> (since Linux 4.2)
|
|
|
|
<DD>
|
|
|
|
|
|
Retrieve all groups a socket is a member of.
|
|
<I>optval</I>
|
|
|
|
is a pointer to
|
|
<B>__u32</B>
|
|
|
|
and
|
|
<I>optlen</I>
|
|
|
|
is the size of the array.
|
|
The array is filled with the full membership set of the
|
|
socket, and the required array size is returned in
|
|
<I>optlen</I>.
|
|
|
|
<DT id="25"><B>NETLINK_BROADCAST_ERROR</B> (since Linux 2.6.30)
|
|
|
|
<DD>
|
|
|
|
|
|
When not set,
|
|
<B>netlink_broadcast()</B>
|
|
|
|
only reports
|
|
<B>ESRCH</B>
|
|
|
|
errors and silently ignore
|
|
<B>ENOBUFS</B>
|
|
|
|
errors.
|
|
<DT id="26"><B>NETLINK_NO_ENOBUFS</B> (since Linux 2.6.30)
|
|
|
|
<DD>
|
|
|
|
|
|
This flag can be used by unicast and broadcast listeners to avoid receiving
|
|
<B>ENOBUFS</B>
|
|
|
|
errors.
|
|
<DT id="27"><B>NETLINK_LISTEN_ALL_NSID</B> (since Linux 4.2)
|
|
|
|
<DD>
|
|
|
|
|
|
When set, this socket will receive netlink notifications from
|
|
all network namespaces that have an
|
|
<I>nsid</I>
|
|
|
|
assigned into the network namespace where the socket has been opened.
|
|
The
|
|
<I>nsid</I>
|
|
|
|
is sent to user space via an ancillary data.
|
|
<DT id="28"><B>NETLINK_CAP_ACK</B> (since Linux 4.2)
|
|
|
|
<DD>
|
|
|
|
|
|
The kernel may fail to allocate the necessary room for the acknowledgment
|
|
message back to user space.
|
|
This option trims off the payload of the original netlink message.
|
|
The netlink message header is still included, so the user can guess from the
|
|
sequence number which message triggered the acknowledgment.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
The socket interface to netlink first appeared Linux 2.2.
|
|
<P>
|
|
|
|
Linux 2.0 supported a more primitive device-based netlink interface
|
|
(which is still available as a compatibility option).
|
|
This obsolete interface is not described here.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
It is often better to use netlink via
|
|
<I>libnetlink</I>
|
|
|
|
or
|
|
<I>libnl</I>
|
|
|
|
than via the low-level kernel interface.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
This manual page is not complete.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following example creates a
|
|
<B>NETLINK_ROUTE</B>
|
|
|
|
netlink socket which will listen to the
|
|
<B>RTMGRP_LINK</B>
|
|
|
|
(network interface create/delete/up/down events) and
|
|
<B>RTMGRP_IPV4_IFADDR</B>
|
|
|
|
(IPv4 addresses add/delete events) multicast groups.
|
|
<P>
|
|
|
|
|
|
|
|
struct sockaddr_nl sa;
|
|
<P>
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.nl_family = AF_NETLINK;
|
|
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
|
|
<P>
|
|
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
bind(fd, (struct sockaddr *) &sa, sizeof(sa));
|
|
|
|
|
|
<P>
|
|
|
|
The next example demonstrates how to send a netlink message to the
|
|
kernel (pid 0).
|
|
Note that the application must take care of message sequence numbers
|
|
in order to reliably track acknowledgements.
|
|
<P>
|
|
|
|
|
|
|
|
struct nlmsghdr *nh; /* The nlmsghdr with payload to send */
|
|
struct sockaddr_nl sa;
|
|
struct iovec iov = { nh, nh->nlmsg_len };
|
|
struct msghdr msg;
|
|
<P>
|
|
msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.nl_family = AF_NETLINK;
|
|
nh->nlmsg_pid = 0;
|
|
nh->nlmsg_seq = ++sequence_number;
|
|
/* Request an ack from kernel by setting NLM_F_ACK */
|
|
nh->nlmsg_flags |= NLM_F_ACK;
|
|
<P>
|
|
sendmsg(fd, &msg, 0);
|
|
|
|
|
|
<P>
|
|
|
|
And the last example is about reading netlink message.
|
|
<P>
|
|
|
|
|
|
|
|
int len;
|
|
/* 8192 to avoid message truncation on platforms with
|
|
<BR> page size > 4096 */
|
|
struct nlmsghdr buf[8192/sizeof(struct nlmsghdr)];
|
|
struct iovec iov = { buf, sizeof(buf) };
|
|
struct sockaddr_nl sa;
|
|
struct msghdr msg;
|
|
struct nlmsghdr *nh;
|
|
<P>
|
|
msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
|
|
len = recvmsg(fd, &msg, 0);
|
|
<P>
|
|
for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
|
|
<BR> nh = NLMSG_NEXT (nh, len)) {
|
|
<BR> /* The end of multipart message */
|
|
<BR> if (nh->nlmsg_type == NLMSG_DONE)
|
|
<BR> return;
|
|
<P>
|
|
<BR> if (nh->nlmsg_type == NLMSG_ERROR)
|
|
<BR> /* Do some error handling */
|
|
<BR> ...
|
|
<P>
|
|
<BR> /* Continue with parsing payload */
|
|
<BR> ...
|
|
}
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+cmsg">cmsg</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+capabilities">capabilities</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+rtnetlink">rtnetlink</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+sock_diag">sock_diag</A></B>(7)
|
|
|
|
<P>
|
|
|
|
|
|
information about libnetlink
|
|
|
|
<P>
|
|
|
|
|
|
information about libnl
|
|
|
|
<P>
|
|
|
|
RFC 3549 "Linux Netlink as an IP Services Protocol"
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="29"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="30"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="31"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="32"><A HREF="#lbAE">Address formats</A><DD>
|
|
<DT id="33"><A HREF="#lbAF">Socket options</A><DD>
|
|
</DL>
|
|
<DT id="34"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="35"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="36"><A HREF="#lbAI">BUGS</A><DD>
|
|
<DT id="37"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DT id="38"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="39"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:06:09 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|