Debian Codegraph @

upstream/debian-unstable/man3/rtnetlink.3

raw
1 .\" Copyright 1999, Andi Kleen <[email protected]>
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-1-para
5 .\"
6 .TH rtnetlink 3 2026-02-08 "Linux man-pages 6.18"
7 .SH NAME
8 rtnetlink \- macros to manipulate rtnetlink messages
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ,\~ \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <asm/types.h>
15 .B #include <linux/netlink.h>
16 .B #include <linux/rtnetlink.h>
17 .B #include <sys/socket.h>
18 .P
19 .BI "rtnetlink_socket = socket(AF_NETLINK, int " socket_type \
20 ", NETLINK_ROUTE);"
21 .P
22 .BI "int RTA_OK(struct rtattr *" rta ", int " size );
23 .P
24 .BI "void *RTA_DATA(struct rtattr *" rta );
25 .BI "unsigned int RTA_PAYLOAD(struct rtattr *" rta );
26 .P
27 .BI "struct rtattr *RTA_NEXT(struct rtattr *" rta \
28 ", unsigned int " size );
29 .P
30 .BI "unsigned int RTA_LENGTH(unsigned int " size );
31 .BI "unsigned int RTA_SPACE(unsigned int "size );
32 .fi
33 .SH DESCRIPTION
34 All
35 .BR rtnetlink (7)
36 messages consist of a
37 .BR netlink (7)
38 message header and appended attributes.
39 The attributes should be manipulated only using the macros provided here.
40 .P
41 .BI RTA_OK( rta ", " size )
42 returns true if
43 .I rta
44 points to a valid routing attribute;
45 .I size
46 is the running size of the attribute buffer.
47 When not true then you must assume there are no more attributes in the
48 message, even if
49 .I size
50 is nonzero.
51 .P
52 .BI RTA_DATA( rta )
53 returns a pointer to the start of this attribute's data.
54 .P
55 .BI RTA_PAYLOAD( rta )
56 returns the size of this attribute's data.
57 .P
58 .BI RTA_NEXT( rta ", " size )
59 gets the next attribute after
60 .IR rta .
61 Calling this macro will update
62 .IR size .
63 You should use
64 .B RTA_OK
65 to check the validity of the returned pointer.
66 .P
67 .BI RTA_LENGTH( size )
68 returns the size which is required for
69 .I size
70 bytes of data plus the header.
71 .P
72 .BI RTA_SPACE( size )
73 returns the amount of space which will be needed in a message with
74 .I size
75 bytes of data.
76 .SH STANDARDS
77 Linux.
78 .SH BUGS
79 This manual page is incomplete.
80 .SH EXAMPLES
81 .\" FIXME . ? would be better to use libnetlink in the EXAMPLE code here
82 Creating a rtnetlink message to set the MTU of a device:
83 .P
84 .in +4n
85 .EX
86 #include <linux/rtnetlink.h>
87 \&
88 \&...
89 \&
90 struct {
91 struct nlmsghdr nh;
92 struct ifinfomsg if;
93 char attrbuf[512];
94 } req;
95 \&
96 struct rtattr *rta;
97 unsigned int mtu = 1000;
98 \&
99 int rtnetlink_sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
100 \&
101 memset(&req, 0, sizeof(req));
102 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.if));
103 req.nh.nlmsg_flags = NLM_F_REQUEST;
104 req.nh.nlmsg_type = RTM_NEWLINK;
105 req.if.ifi_family = AF_UNSPEC;
106 req.if.ifi_index = INTERFACE_INDEX;
107 req.if.ifi_change = 0xffffffff; /* ??? */
108 rta = (struct rtattr *) ((char *) &req +
109 NLMSG_ALIGN(req.nh.nlmsg_len));
110 rta\->rta_type = IFLA_MTU;
111 rta\->rta_len = RTA_LENGTH(sizeof(mtu));
112 req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) +
113 RTA_LENGTH(sizeof(mtu));
114 memcpy(RTA_DATA(rta), &mtu, sizeof(mtu));
115 send(rtnetlink_sk, &req, req.nh.nlmsg_len, 0);
116 .EE
117 .in
118 .SH SEE ALSO
119 .BR netlink (3),
120 .BR netlink (7),
121 .BR rtnetlink (7)