xref: /openbsd-src/share/man/man4/route.4 (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1.\"	$OpenBSD: route.4,v 1.52 2019/04/30 16:34:19 jmc Exp $
2.\"	$NetBSD: route.4,v 1.3 1994/11/30 16:22:31 jtc Exp $
3.\"
4.\" Copyright (c) 1990, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)route.4	8.6 (Berkeley) 4/19/94
32.\"
33.Dd $Mdocdate: April 30 2019 $
34.Dt ROUTE 4
35.Os
36.Sh NAME
37.Nm route
38.Nd kernel packet forwarding database
39.Sh SYNOPSIS
40.In sys/socket.h
41.In net/if.h
42.In net/route.h
43.Ft int
44.Fn socket AF_ROUTE SOCK_RAW family
45.Sh DESCRIPTION
46.Ox
47provides some packet routing facilities.
48The kernel maintains a routing information database, which
49is used in selecting the appropriate network interface when
50transmitting packets.
51.Pp
52A user process (or possibly multiple co-operating processes)
53maintains this database by sending messages over a special kind
54of socket.
55This supplants fixed size
56.Xr ioctl 2 Ns 's
57used in earlier releases.
58Routing table changes may only be carried out by the super user.
59.Pp
60The operating system may spontaneously emit routing messages in response
61to external events, such as receipt of a redirect, or failure to
62locate a suitable route for a request.
63The message types are described in greater detail below.
64.Pp
65Routing database entries come in two flavors: for a specific
66host, or for all hosts on a generic subnetwork (as specified
67by a bit mask and value under the mask).
68The effect of wildcard or default route may be achieved by using
69a mask of all zeros, and there may be hierarchical routes.
70.Pp
71When the system is booted and addresses are assigned
72to the network interfaces, each protocol family
73installs a routing table entry for each interface when it is ready for traffic.
74Normally the protocol specifies the route
75through each interface as a
76.Dq direct
77connection to the destination host
78or network.
79If the route is direct, the transport layer of a protocol family usually
80requests the packet be sent to the same host specified in the packet.
81Otherwise, the interface is requested to address the packet to the gateway
82listed in the routing entry (i.e., the packet is forwarded).
83.Pp
84When routing a packet,
85the kernel will attempt to find
86the most specific route matching the destination.
87(If there are two different mask and value-under-the-mask pairs
88that match, the more specific is the one with more bits in the mask.
89A route to a host is regarded as being supplied with a mask of
90as many ones as there are bits in the destination.)
91If no entry is found, the destination is declared to be unreachable,
92and a routing\-miss message is generated if there are any
93listeners on the routing control socket described below.
94.Pp
95If there are two identical destinations, the route priority acts as a
96tie-breaker.
97If there are multiple routes to the same destination,
98the one with the lowest priority wins.
99The kernel assigns certain default priorities based on the type of
100route, as given in the table below.
101For connected and static routes, this default priority is added to the
102interface's priority.
103.Pp
104A wildcard routing entry is specified with a zero
105destination address value and a mask of all zeroes.
106Wildcard routes will be used
107when the system fails to find other routes matching the
108destination.
109The combination of wildcard routes and routing redirects can provide
110an economical mechanism for routing traffic.
111Routes created by redirects from wildcard routes and other routes
112will be marked
113.Em cloned ,
114until their
115.Dq parent
116from which they were created has disappeared.
117.Pp
118Route labels can be attached to routes and may contain arbitrary
119information about the route.
120Labels are sent over the routing socket (see below) as
121.Vt sockaddr_rtlabel
122structures.
123.Ss The Routing Socket
124One opens the channel for passing routing control messages
125by using the
126.Xr socket 2
127call shown in the
128.Sx SYNOPSIS
129above.
130.Pp
131The
132.Fa family
133parameter may be
134.Dv AF_UNSPEC ,
135which will provide
136routing information for all address families, or can be restricted
137to a specific address family by specifying which one is desired.
138There can be more than one routing socket open per system.
139.Pp
140Messages are formed by a header followed by a small
141number of
142.Vt sockaddr
143structures (which are variable length),
144interpreted by position, and delimited
145by the length entry in the
146.Vt sockaddr .
147An example of a message with four addresses might be an
148IPv4 route addition: the destination, netmask, gateway, and label,
149since both netmasks and labels are sent over the routing socket as
150.Vt sockaddr
151structures.
152The interpretation of which addresses are present is given by a
153bit mask within the header, and the sequence is least significant
154to most significant bit within the vector.
155.Pp
156Any messages sent to the kernel are returned, and copies are sent
157to all interested listeners.
158The kernel will provide the process ID
159of the sender, and the sender may use an additional sequence
160field to distinguish between outstanding messages.
161However, message replies may be lost when kernel buffers are exhausted.
162.Pp
163The kernel may reject certain messages, and will indicate this
164by filling in the
165.Va rtm_errno
166field.
167The routing code returns
168.Er EEXIST
169if
170requested to duplicate an existing entry,
171.Er ESRCH
172if
173requested to delete a non-existent entry,
174or
175.Er ENOBUFS
176if insufficient resources were available
177to install a new route.
178In the current implementation, all routing processes run locally,
179and the values for
180.Va rtm_errno
181are available through the normal
182.Va errno
183mechanism, even if the routing reply message is lost.
184.Pp
185A process may avoid the expense of reading replies to
186its own messages by issuing a
187.Xr setsockopt 2
188call indicating that the
189.Dv SO_USELOOPBACK
190option at the
191.Dv SOL_SOCKET
192level is to be turned off.
193A process may ignore all messages from the routing socket
194by doing a
195.Xr shutdown 2
196system call for further input.
197.Pp
198A process can specify an alternate routing table by using the
199.Dv ROUTE_TABLEFILTER
200.Xr setsockopt 2 .
201A value of
202.Dv RTABLE_ANY
203specifies all routing tables.
204For example, to receive messages for routing table 5:
205.Bd -literal -offset indent
206unsigned int rdomain = 5;
207
208if (setsockopt(routefd, AF_ROUTE, ROUTE_TABLEFILTER,
209    &rdomain, sizeof(rdomain)) == -1)
210	err(1, "setsockopt(ROUTE_TABLEFILTER)");
211.Ed
212.Pp
213A process can specify which route message types it's interested in
214by using
215.Dv ROUTE_FILTER(int type)
216and issuing a setsockopt call with the
217.Dv ROUTE_MSGFILTER
218option at the
219.Dv AF_ROUTE
220level.
221For example, to only get interface specific messages:
222.Bd -literal -offset indent
223unsigned int rtfilter;
224
225rtfilter = ROUTE_FILTER(RTM_IFINFO) |
226    ROUTE_FILTER(RTM_IFANNOUNCE);
227
228if (setsockopt(routefd, AF_ROUTE, ROUTE_MSGFILTER,
229    &rtfilter, sizeof(rtfilter)) == -1)
230	err(1, "setsockopt(ROUTE_MSGFILTER)");
231.Ed
232.Pp
233Similarly, a process can specify that it is only interested in messages
234relating to routes where the priority is no more than a certain value
235by issuing a setsockopt call with the
236.Dv ROUTE_PRIOFILTER
237option.
238For example, to select only local, directly connected and static routes:
239.Bd -literal -offset indent
240unsigned int maxprio = RTP_STATIC;
241
242if (setsockopt(routefd, AF_ROUTE, ROUTE_PRIOFILTER,
243    &maxprio, sizeof(maxprio)) == -1)
244	err(1, "setsockopt(ROUTE_PRIOFILTER)");
245.Ed
246.Pp
247The predefined constants for the routing priorities are:
248.Bd -literal
249#define RTP_NONE	0	/* unset priority use sane default */
250#define RTP_LOCAL	1	/* local address routes (must be the highest) */
251#define RTP_CONNECTED	4	/* directly connected routes */
252#define RTP_STATIC	8	/* static routes base priority */
253#define RTP_EIGRP	28	/* EIGRP routes */
254#define RTP_OSPF	32	/* OSPF routes */
255#define RTP_ISIS	36	/* IS-IS routes */
256#define RTP_RIP		40	/* RIP routes */
257#define RTP_BGP		48	/* BGP routes */
258#define RTP_DEFAULT	56	/* routes that have nothing set */
259#define RTP_PROPOSAL_STATIC	57
260#define RTP_PROPOSAL_DHCLIENT	58
261#define RTP_PROPOSAL_SLAAC	59
262#define RTP_MAX		63	/* maximum priority */
263#define RTP_ANY		64	/* any of the above */
264#define RTP_MASK	0x7f
265#define RTP_DOWN	0x80	/* route/link is down */
266.Ed
267.Pp
268If a route is in use when it is deleted,
269the routing entry will be marked down and removed from the routing table,
270but the resources associated with it will not
271be reclaimed until all references to it are released.
272User processes can obtain information about the routing
273entry to a specific destination by using a
274.Dv RTM_GET
275message or via the
276.Dv PF_ROUTE
277.Xr sysctl 2 .
278.Pp
279Messages include:
280.Bd -literal
281#define RTM_ADD		0x1	/* Add Route */
282#define RTM_DELETE	0x2	/* Delete Route */
283#define RTM_CHANGE	0x3	/* Change Metrics or flags */
284#define RTM_GET		0x4	/* Report Metrics */
285#define RTM_LOSING	0x5	/* Kernel Suspects Partitioning */
286#define RTM_REDIRECT	0x6	/* Told to use different route */
287#define RTM_MISS	0x7	/* Lookup failed on this address */
288#define RTM_RESOLVE	0xb	/* req to resolve dst to LL addr */
289#define RTM_NEWADDR	0xc	/* address being added to iface */
290#define RTM_DELADDR	0xd	/* address being removed from iface */
291#define RTM_IFINFO	0xe	/* iface going up/down etc. */
292#define RTM_IFANNOUNCE	0xf	/* iface arrival/departure */
293#define RTM_DESYNC	0x10	/* route socket buffer overflow */
294#define RTM_INVALIDATE	0x11	/* Invalidate cache of L2 route */
295.Ed
296.Pp
297A message header consists of one of the following:
298.Bd -literal
299struct rt_msghdr {
300	u_short	rtm_msglen;	/* to skip over non-understood messages */
301	u_char	rtm_version;	/* future binary compatibility */
302	u_char	rtm_type;	/* message type */
303	u_short	rtm_hdrlen;	/* sizeof(rt_msghdr) to skip over the header */
304	u_short	rtm_index;	/* index for associated ifp */
305	u_short rtm_tableid;	/* routing table id */
306	u_char	rtm_priority;	/* routing priority */
307	u_char	rtm_mpls;	/* MPLS additional infos */
308	int	rtm_addrs;	/* bitmask identifying sockaddrs in msg */
309	int	rtm_flags;	/* flags, incl. kern & message, e.g. DONE */
310	int	rtm_fmask;	/* bitmask used in RTM_CHANGE message */
311	pid_t	rtm_pid;	/* identify sender */
312	int	rtm_seq;	/* for sender to identify action */
313	int	rtm_errno;	/* why failed */
314	u_int	rtm_inits;	/* which metrics we are initializing */
315	struct	rt_metrics rtm_rmx; /* metrics themselves */
316};
317
318struct if_msghdr {
319	u_short	ifm_msglen;	/* to skip over non-understood messages */
320	u_char	ifm_version;	/* future binary compatibility */
321	u_char	ifm_type;	/* message type */
322	u_short ifm_hdrlen;	/* sizeof(if_msghdr) to skip over the header */
323	u_short	ifm_index;	/* index for associated ifp */
324	u_short	ifm_tableid;	/* routing table id */
325	u_char	ifm_pad1;
326	u_char	ifm_pad2;
327	int	ifm_addrs;	/* like rtm_addrs */
328	int	ifm_flags;	/* value of if_flags */
329	int	ifm_xflags;
330	struct	if_data ifm_data;/* statistics and other data about if */
331};
332
333struct ifa_msghdr {
334	u_short	ifam_msglen;	/* to skip over non-understood messages */
335	u_char	ifam_version;	/* future binary compatibility */
336	u_char	ifam_type;	/* message type */
337	u_short ifam_hdrlen;	/* sizeof(ifa_msghdr) to skip over the header */
338	u_short	ifam_index;	/* index for associated ifp */
339	u_short	ifam_tableid;	/* routing table id */
340	u_char	ifam_pad1;
341	u_char	ifam_pad2;
342	int	ifam_addrs;	/* like rtm_addrs */
343	int	ifam_flags;	/* value of ifa_flags */
344	int	ifam_metric;	/* value of ifa_metric */
345};
346
347struct if_announcemsghdr {
348	u_short	ifan_msglen;	/* to skip over non-understood messages */
349	u_char	ifan_version;	/* future binary compatibility */
350	u_char	ifan_type;	/* message type */
351	u_short ifan_hdrlen;	/* sizeof(ifa_msghdr) to skip over the header */
352	u_short	ifan_index;	/* index for associated ifp */
353	u_short	ifan_what;	/* what type of announcement */
354	char	ifan_name[IFNAMSIZ];	/* if name, e.g. "en0" */
355};
356.Ed
357.Pp
358The
359.Dv RTM_IFINFO
360message uses an
361.Vt if_msghdr
362header, the
363.Dv RTM_NEWADDR
364and
365.Dv RTM_DELADDR
366messages use an
367.Vt ifa_msghdr
368header,
369the
370.Dv RTM_IFANNOUNCE
371message uses an
372.Vt if_announcemsghdr
373header,
374.Dv RTM_INVALIDATE
375is used only internally in the kernel and should never appear in a route
376message,
377and all other messages use the
378.Vt rt_msghdr
379header.
380.Pp
381The metrics structure is:
382.Bd -literal
383struct rt_metrics {
384	u_int64_t	rmx_pksent;	/* packets sent using this route */
385	int64_t		rmx_expire;	/* lifetime for route, e.g. redirect */
386	u_int		rmx_locks;	/* Kernel must leave these values */
387	u_int		rmx_mtu;	/* MTU for this path */
388	u_int		rmx_refcnt;	/* # references hold */
389	u_int		rmx_hopcount;	/* max hops expected */
390	u_int		rmx_recvpipe;	/* inbound delay-bandwidth product */
391	u_int		rmx_sendpipe;	/* outbound delay-bandwidth product */
392	u_int		rmx_ssthresh;	/* outbound gateway buffer limit */
393	u_int		rmx_rtt;	/* estimated round trip time */
394	u_int		rmx_rttvar;	/* estimated rtt variance */
395	u_int		rmx_pad;
396};
397.Ed
398.Pp
399Only
400.Va rmx_mtu , rmx_expire , rmx_pksent ,
401and
402.Va rmx_locks
403are used by the kernel routing table.
404All other values will be ignored when inserting them into the kernel and are
405set to zero in routing messages sent by the kernel.
406They are left for compatibility reasons with other systems.
407.Pp
408Flags include the values:
409.Bd -literal
410#define	RTF_UP        0x1       /* route usable */
411#define	RTF_GATEWAY   0x2       /* destination is a gateway */
412#define	RTF_HOST      0x4       /* host entry (net otherwise) */
413#define	RTF_REJECT    0x8       /* host or net unreachable */
414#define	RTF_DYNAMIC   0x10      /* created dynamically (by redirect) */
415#define	RTF_MODIFIED  0x20      /* modified dynamically (by redirect) */
416#define	RTF_DONE      0x40      /* message confirmed */
417#define	RTF_CLONING   0x100     /* generate new routes on use */
418#define	RTF_MULTICAST 0x200     /* route associated to a mcast addr.  */
419#define	RTF_LLINFO    0x400     /* generated by ARP or NDP */
420#define	RTF_STATIC    0x800     /* manually added */
421#define	RTF_BLACKHOLE 0x1000    /* just discard pkts (during updates) */
422#define	RTF_PROTO3    0x2000    /* protocol specific routing flag */
423#define	RTF_PROTO2    0x4000    /* protocol specific routing flag */
424#define	RTF_PROTO1    0x8000    /* protocol specific routing flag */
425#define	RTF_CLONED    0x10000   /* this is a cloned route */
426#define RTF_MPATH     0x40000   /* multipath route or operation */
427#define RTF_MPLS      0x100000  /* MPLS additional infos */
428#define RTF_LOCAL     0x200000	/* route to a local address */
429#define RTF_BROADCAST 0x400000	/* route associated to a bcast addr. */
430#define RTF_CONNECTED 0x800000	/* interface route */
431.Ed
432.Pp
433The following flags (defined as
434.Dv RTF_FMASK )
435can be changed by an RTM_CHANGE request:
436.Dv RTF_LLINFO ,
437.Dv RTF_PROTO1 ,
438.Dv RTF_PROTO2 ,
439.Dv RTF_PROTO3 ,
440.Dv RTF_BLACKHOLE ,
441.Dv RTF_REJECT ,
442.Dv RTF_STATIC
443and
444.Dv RTF_MPLS .
445.Pp
446Specifiers for metric values in
447.Va rmx_locks
448and
449.Va rtm_inits
450are:
451.Bd -literal
452#define RTV_MTU		0x1	/* init or lock _mtu */
453#define RTV_HOPCOUNT	0x2	/* init or lock _hopcount */
454#define RTV_EXPIRE	0x4	/* init or lock _expire */
455#define RTV_RPIPE	0x8	/* init or lock _recvpipe */
456#define RTV_SPIPE	0x10	/* init or lock _sendpipe */
457#define RTV_SSTHRESH	0x20	/* init or lock _ssthresh */
458#define RTV_RTT		0x40	/* init or lock _rtt */
459#define RTV_RTTVAR	0x80	/* init or lock _rttvar */
460.Ed
461.Pp
462Only
463.Dv RTV_MTU
464and
465.Dv RTV_EXPIRE
466should be used; all other flags are ignored.
467.Pp
468Specifiers for which addresses are present in the messages are:
469.Bd -literal
470#define RTA_DST		0x1	/* destination sockaddr present */
471#define RTA_GATEWAY	0x2	/* gateway sockaddr present */
472#define RTA_NETMASK	0x4	/* netmask sockaddr present */
473#define RTA_IFP		0x10	/* interface name sockaddr present */
474#define RTA_IFA		0x20	/* interface addr sockaddr present */
475#define RTA_AUTHOR	0x40	/* sockaddr for author of redirect */
476#define RTA_BRD		0x80	/* for NEWADDR, bcast or p-p dest addr */
477#define RTA_SRC		0x100	/* source sockaddr present */
478#define RTA_SRCMASK	0x200	/* source netmask present */
479#define RTA_LABEL	0x400	/* route label present */
480.Ed
481.Sh SEE ALSO
482.Xr netstat 1 ,
483.Xr socket 2 ,
484.Xr sysctl 2 ,
485.Xr rtable 4 ,
486.Xr mygate 5 ,
487.Xr route 8 ,
488.Xr route 9
489.Sh HISTORY
490A
491.Dv PF_ROUTE
492protocol family first appeared in
493.Bx 4.3 Reno .
494