man-pages/man3/rtnetlink.3.html
2021-03-31 01:06:50 +01:00

216 lines
5.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of RTNETLINK</TITLE>
</HEAD><BODY>
<H1>RTNETLINK</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2014-09-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
rtnetlink - macros to manipulate rtnetlink messages
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/asm/types.h">asm/types.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/linux/netlink.h">linux/netlink.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/linux/rtnetlink.h">linux/rtnetlink.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<P>
<B>rtnetlink_socket = socket(AF_NETLINK, int </B><I>socket_type</I><B>, NETLINK_ROUTE);</B>
<P>
<B>int RTA_OK(struct rtattr *</B><I>rta</I><B>, int </B><I>rtabuflen</I><B>);</B>
<P>
<B>void *RTA_DATA(struct rtattr *</B><I>rta</I><B>);</B>
<P>
<B>unsigned int RTA_PAYLOAD(struct rtattr *</B><I>rta</I><B>);</B>
<P>
<B>struct rtattr *RTA_NEXT(struct rtattr *</B><I>rta</I><B>, unsigned int </B><I>rtabuflen</I><B>);</B>
<P>
<B>unsigned int RTA_LENGTH(unsigned int </B><I>length</I><B>);</B>
<P>
<B>unsigned int RTA_SPACE(unsigned int length</B><I>);</I>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
All
<B><A HREF="/cgi-bin/man/man2html?7+rtnetlink">rtnetlink</A></B>(7)
messages consist of a
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7)
message header and appended attributes.
The attributes should be manipulated only using the macros provided here.
<P>
<B>RTA_OK(</B><I>rta</I><B>, </B><I>attrlen</I><B>)</B>
returns true if
<I>rta</I>
points to a valid routing attribute;
<I>attrlen</I>
is the running length of the attribute buffer.
When not true then you must assume there are no more attributes in the
message, even if
<I>attrlen</I>
is nonzero.
<P>
<B>RTA_DATA(</B><I>rta</I><B>)</B>
returns a pointer to the start of this attribute's data.
<P>
<B>RTA_PAYLOAD(</B><I>rta</I><B>)</B>
returns the length of this attribute's data.
<P>
<B>RTA_NEXT(</B><I>rta</I><B>, </B><I>attrlen</I><B>)</B>
gets the next attribute after
<I>rta</I>.
Calling this macro will update
<I>attrlen</I>.
You should use
<B>RTA_OK</B>
to check the validity of the returned pointer.
<P>
<B>RTA_LENGTH(</B><I>len</I><B>)</B>
returns the length which is required for
<I>len</I>
bytes of data plus the header.
<P>
<B>RTA_SPACE(</B><I>len</I><B>)</B>
returns the amount of space which will be needed in a message with
<I>len</I>
bytes of data.
<A NAME="lbAE">&nbsp;</A>
<H2>CONFORMING TO</H2>
These macros are nonstandard Linux extensions.
<A NAME="lbAF">&nbsp;</A>
<H2>BUGS</H2>
This manual page is incomplete.
<A NAME="lbAG">&nbsp;</A>
<H2>EXAMPLE</H2>
Creating a rtnetlink message to set the MTU of a device:
<P>
#include &lt;<A HREF="file:///usr/include/linux/rtnetlink.h">linux/rtnetlink.h</A>&gt;
<P>
...
<P>
struct {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;nlmsghdr&nbsp;&nbsp;nh;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;ifinfomsg&nbsp;if;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrbuf[512];
} req;
<P>
struct rtattr *rta;
unsigned int mtu = 1000;
<P>
int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
<P>
memset(&amp;req, 0, sizeof(req));
req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.nh.nlmsg_flags = NLM_F_REQUEST;
req.nh.nlmsg_type = RTM_NEWLINK;
req.if.ifi_family = AF_UNSPEC;
req.if.ifi_index = INTERFACE_INDEX;
req.if.ifi_change = 0xffffffff; /* ??? */
rta = (struct rtattr *)(((char *) &amp;req) +
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NLMSG_ALIGN(req.nh.nlmsg_len));
rta-&gt;rta_type = IFLA_MTU;
rta-&gt;rta_len = RTA_LENGTH(sizeof(unsigned int));
req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) +
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RTA_LENGTH(sizeof(mtu));
memcpy(RTA_DATA(rta), &amp;mtu, sizeof(mtu));
send(rtnetlink_sk, &amp;req, req.nh.nlmsg_len, 0);
<A NAME="lbAH">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+netlink">netlink</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+netlink">netlink</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+rtnetlink">rtnetlink</A></B>(7)
<A NAME="lbAI">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">CONFORMING TO</A><DD>
<DT id="5"><A HREF="#lbAF">BUGS</A><DD>
<DT id="6"><A HREF="#lbAG">EXAMPLE</A><DD>
<DT id="7"><A HREF="#lbAH">SEE ALSO</A><DD>
<DT id="8"><A HREF="#lbAI">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:05:54 GMT, March 31, 2021
</BODY>
</HTML>