1RTNETLINK(7) Linux Programmer's Manual RTNETLINK(7)
2
3
4
6 rtnetlink - Linux IPv4 routing socket
7
9 #include <asm/types.h>
10 #include <linux/netlink.h>
11 #include <linux/rtnetlink.h>
12 #include <sys/socket.h>
13
14 rtnetlink_socket = socket(AF_NETLINK, int socket_type, NETLINK_ROUTE);
15
17 Rtnetlink allows the kernel's routing tables to be read and altered.
18 It is used within the kernel to communicate between various subsystems,
19 though this usage is not documented here, and for communication with
20 user-space programs. Network routes, IP addresses, link parameters,
21 neighbor setups, queueing disciplines, traffic classes and packet clas‐
22 sifiers may all be controlled through NETLINK_ROUTE sockets. It is
23 based on netlink messages; see netlink(7) for more information.
24
25 Routing attributes
26 Some rtnetlink messages have optional attributes after the initial
27 header:
28
29 struct rtattr {
30 unsigned short rta_len; /* Length of option */
31 unsigned short rta_type; /* Type of option */
32 /* Data follows */
33 };
34
35 These attributes should be manipulated using only the RTA_* macros or
36 libnetlink, see rtnetlink(3).
37
38 Messages
39 Rtnetlink consists of these message types (in addition to standard
40 netlink messages):
41
42 RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK
43 Create, remove or get information about a specific network
44 interface. These messages contain an ifinfomsg structure fol‐
45 lowed by a series of rtattr structures.
46
47 struct ifinfomsg {
48 unsigned char ifi_family; /* AF_UNSPEC */
49 unsigned short ifi_type; /* Device type */
50 int ifi_index; /* Interface index */
51 unsigned int ifi_flags; /* Device flags */
52 unsigned int ifi_change; /* change mask */
53 };
54
55 ifi_flags contains the device flags, see netdevice(7); ifi_index
56 is the unique interface index (since Linux 3.7, it is possible
57 to feed a nonzero value with the RTM_NEWLINK message, thus cre‐
58 ating a link with the given ifindex); ifi_change is reserved for
59 future use and should be always set to 0xFFFFFFFF.
60
61 Routing attributes
62 rta_type value type description
63 ──────────────────────────────────────────────────────────
64 IFLA_UNSPEC - unspecified.
65 IFLA_ADDRESS hardware address interface L2 address
66
67 IFLA_BROADCAST hardware address L2 broadcast address.
68 IFLA_IFNAME asciiz string Device name.
69 IFLA_MTU unsigned int MTU of the device.
70 IFLA_LINK int Link type.
71 IFLA_QDISC asciiz string Queueing discipline.
72 IFLA_STATS see below Interface Statistics.
73
74 The value type for IFLA_STATS is struct rtnl_link_stats (struct
75 net_device_stats in Linux 2.4 and earlier).
76
77 RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR
78 Add, remove or receive information about an IP address associ‐
79 ated with an interface. In Linux 2.2, an interface can carry
80 multiple IP addresses, this replaces the alias device concept in
81 2.0. In Linux 2.2, these messages support IPv4 and IPv6
82 addresses. They contain an ifaddrmsg structure, optionally fol‐
83 lowed by rtattr routing attributes.
84
85 struct ifaddrmsg {
86 unsigned char ifa_family; /* Address type */
87 unsigned char ifa_prefixlen; /* Prefixlength of address */
88 unsigned char ifa_flags; /* Address flags */
89 unsigned char ifa_scope; /* Address scope */
90 int ifa_index; /* Interface index */
91 };
92
93 ifa_family is the address family type (currently AF_INET or
94 AF_INET6), ifa_prefixlen is the length of the address mask of
95 the address if defined for the family (like for IPv4), ifa_scope
96 is the address scope, ifa_index is the interface index of the
97 interface the address is associated with. ifa_flags is a flag
98 word of IFA_F_SECONDARY for secondary address (old alias inter‐
99 face), IFA_F_PERMANENT for a permanent address set by the user
100 and other undocumented flags.
101
102 Attributes
103 rta_type value type description
104 ─────────────────────────────────────────────────────────────
105 IFA_UNSPEC - unspecified.
106 IFA_ADDRESS raw protocol address interface address
107 IFA_LOCAL raw protocol address local address
108 IFA_LABEL asciiz string name of the interface
109 IFA_BROADCAST raw protocol address broadcast address.
110 IFA_ANYCAST raw protocol address anycast address
111 IFA_CACHEINFO struct ifa_cacheinfo Address information.
112
113 RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE
114 Create, remove or receive information about a network route.
115 These messages contain an rtmsg structure with an optional
116 sequence of rtattr structures following. For RTM_GETROUTE, set‐
117 ting rtm_dst_len and rtm_src_len to 0 means you get all entries
118 for the specified routing table. For the other fields, except
119 rtm_table and rtm_protocol, 0 is the wildcard.
120
121 struct rtmsg {
122 unsigned char rtm_family; /* Address family of route */
123 unsigned char rtm_dst_len; /* Length of destination */
124 unsigned char rtm_src_len; /* Length of source */
125 unsigned char rtm_tos; /* TOS filter */
126
127 unsigned char rtm_table; /* Routing table ID */
128 unsigned char rtm_protocol; /* Routing protocol; see below */
129 unsigned char rtm_scope; /* See below */
130 unsigned char rtm_type; /* See below */
131
132 unsigned int rtm_flags;
133 };
134
135 rtm_type Route type
136 ───────────────────────────────────────────────────────────
137 RTN_UNSPEC unknown route
138 RTN_UNICAST a gateway or direct route
139 RTN_LOCAL a local interface route
140 RTN_BROADCAST a local broadcast route (sent as a
141 broadcast)
142 RTN_ANYCAST a local broadcast route (sent as a uni‐
143 cast)
144 RTN_MULTICAST a multicast route
145 RTN_BLACKHOLE a packet dropping route
146 RTN_UNREACHABLE an unreachable destination
147 RTN_PROHIBIT a packet rejection route
148 RTN_THROW continue routing lookup in another table
149 RTN_NAT a network address translation rule
150 RTN_XRESOLVE refer to an external resolver (not
151 implemented)
152
153 rtm_protocol Route origin.
154 ─────────────────────────────────────────────
155 RTPROT_UNSPEC unknown
156 RTPROT_REDIRECT by an ICMP redirect (cur‐
157 rently unused)
158 RTPROT_KERNEL by the kernel
159 RTPROT_BOOT during boot
160 RTPROT_STATIC by the administrator
161
162 Values larger than RTPROT_STATIC are not interpreted by the ker‐
163 nel, they are just for user information. They may be used to
164 tag the source of a routing information or to distinguish
165 between multiple routing daemons. See <linux/rtnetlink.h> for
166 the routing daemon identifiers which are already assigned.
167
168 rtm_scope is the distance to the destination:
169
170 RT_SCOPE_UNIVERSE global route
171 RT_SCOPE_SITE interior route in the
172 local autonomous system
173 RT_SCOPE_LINK route on this link
174 RT_SCOPE_HOST route on the local host
175 RT_SCOPE_NOWHERE destination doesn't exist
176
177 The values between RT_SCOPE_UNIVERSE and RT_SCOPE_SITE are
178 available to the user.
179
180 The rtm_flags have the following meanings:
181
182 RTM_F_NOTIFY if the route changes, notify the user via
183 rtnetlink
184 RTM_F_CLONED route is cloned from another route
185 RTM_F_EQUALIZE a multipath equalizer (not yet implemented)
186
187 rtm_table specifies the routing table
188
189 RT_TABLE_UNSPEC an unspecified routing table
190 RT_TABLE_DEFAULT the default table
191 RT_TABLE_MAIN the main table
192 RT_TABLE_LOCAL the local table
193
194 The user may assign arbitrary values between RT_TABLE_UNSPEC and
195 RT_TABLE_DEFAULT.
196
197 Attributes
198 rta_type value type description
199 ──────────────────────────────────────────────────────────────
200 RTA_UNSPEC - ignored.
201 RTA_DST protocol address Route destination address.
202 RTA_SRC protocol address Route source address.
203 RTA_IIF int Input interface index.
204 RTA_OIF int Output interface index.
205 RTA_GATEWAY protocol address The gateway of the route
206 RTA_PRIORITY int Priority of route.
207 RTA_PREFSRC
208 RTA_METRICS int Route metric
209 RTA_MULTIPATH
210 RTA_PROTOINFO
211 RTA_FLOW
212 RTA_CACHEINFO
213
214 Fill these values in!
215
216 RTM_NEWNEIGH, RTM_DELNEIGH, RTM_GETNEIGH
217 Add, remove or receive information about a neighbor table entry
218 (e.g., an ARP entry). The message contains an ndmsg structure.
219
220 struct ndmsg {
221 unsigned char ndm_family;
222 int ndm_ifindex; /* Interface index */
223 __u16 ndm_state; /* State */
224 __u8 ndm_flags; /* Flags */
225 __u8 ndm_type;
226 };
227
228 struct nda_cacheinfo {
229 __u32 ndm_confirmed;
230 __u32 ndm_used;
231 __u32 ndm_updated;
232 __u32 ndm_refcnt;
233 };
234
235 ndm_state is a bit mask of the following states:
236
237 NUD_INCOMPLETE a currently resolving cache entry
238 NUD_REACHABLE a confirmed working cache entry
239 NUD_STALE an expired cache entry
240 NUD_DELAY an entry waiting for a timer
241 NUD_PROBE a cache entry that is currently reprobed
242 NUD_FAILED an invalid cache entry
243 NUD_NOARP a device with no destination cache
244 NUD_PERMANENT a static entry
245
246 Valid ndm_flags are:
247
248 NTF_PROXY a proxy arp entry
249 NTF_ROUTER an IPv6 router
250
251 The rtattr struct has the following meanings for the rta_type
252 field:
253
254 NDA_UNSPEC unknown type
255 NDA_DST a neighbor cache n/w layer destination address
256 NDA_LLADDR a neighbor cache link layer address
257 NDA_CACHEINFO cache statistics.
258
259 If the rta_type field is NDA_CACHEINFO, then a struct nda_cache‐
260 info header follows
261
262 RTM_NEWRULE, RTM_DELRULE, RTM_GETRULE
263 Add, delete or retrieve a routing rule. Carries a struct rtmsg
264
265 RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC
266 Add, remove or get a queueing discipline. The message contains
267 a struct tcmsg and may be followed by a series of attributes.
268
269 struct tcmsg {
270 unsigned char tcm_family;
271 int tcm_ifindex; /* interface index */
272 __u32 tcm_handle; /* Qdisc handle */
273 __u32 tcm_parent; /* Parent qdisc */
274 __u32 tcm_info;
275 };
276
277 Attributes
278 rta_type value type Description
279 ────────────────────────────────────────────────────────────────
280 TCA_UNSPEC - unspecified
281 TCA_KIND asciiz string Name of queueing discipline
282 TCA_OPTIONS byte sequence Qdisc-specific options follow
283 TCA_STATS struct tc_stats Qdisc statistics.
284 TCA_XSTATS qdisc-specific Module-specific statistics.
285 TCA_RATE struct tc_estimator Rate limit.
286
287 In addition, various other qdisc-module-specific attributes are
288 allowed. For more information see the appropriate include
289 files.
290
291 RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS
292 Add, remove or get a traffic class. These messages contain a
293 struct tcmsg as described above.
294
295 RTM_NEWTFILTER, RTM_DELTFILTER, RTM_GETTFILTER
296 Add, remove or receive information about a traffic filter.
297 These messages contain a struct tcmsg as described above.
298
300 rtnetlink is a new feature of Linux 2.2.
301
303 This manual page is incomplete.
304
306 cmsg(3), rtnetlink(3), ip(7), netlink(7)
307
309 This page is part of release 4.15 of the Linux man-pages project. A
310 description of the project, information about reporting bugs, and the
311 latest version of this page, can be found at
312 https://www.kernel.org/doc/man-pages/.
313
314
315
316Linux 2017-09-15 RTNETLINK(7)