xref: /netbsd-src/share/man/man4/route.4 (revision 2e0bf311b34bf9507e08ee74157213840e9994d3)
1.\"	$NetBSD: route.4,v 1.34 2021/08/17 22:00:26 andvar Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)route.4	8.6 (Berkeley) 4/19/94
31.\"
32.Dd February 4, 2020
33.Dt ROUTE 4
34.Os
35.Sh NAME
36.Nm route
37.Nd kernel packet forwarding database
38.Sh SYNOPSIS
39.In sys/socket.h
40.In net/if.h
41.In net/route.h
42.Ft int
43.Fn socket PF_ROUTE SOCK_RAW "int family"
44.Sh DESCRIPTION
45.Ux
46provides some packet routing facilities.
47The kernel maintains a routing information database, which
48is used in selecting the appropriate network interface when
49transmitting packets.
50.Pp
51A user process (or possibly multiple co-operating processes)
52maintains this database by sending messages over a special kind
53of socket.
54This supplants fixed size
55.Xr ioctl 2 Ns 's
56used in earlier releases.
57Routing table changes may only be carried out by the super user.
58.Pp
59The operating system may spontaneously emit routing messages in response
60to external events, such as receipt of a redirect, or failure to
61locate a suitable route for a request.
62The message types are described in greater detail below.
63.Pp
64Routing database entries come in two flavors: for a specific
65host, or for all hosts on a generic subnetwork (as specified
66by a bit mask and value under the mask.
67The effect of wildcard or default route may be achieved by using
68a mask of all zeros, and there may be hierarchical routes.
69.Pp
70When the system is booted and addresses are assigned
71to the network interfaces, each protocol family
72installs a routing table entry for each interface when it is ready for traffic.
73Normally the protocol specifies the route
74through each interface as a
75.Dq direct
76connection to the destination host
77or network.
78If the route is direct, the transport layer of a protocol family
79usually requests the packet be sent to the same host specified in
80the packet.
81Otherwise, the interface is requested to address the packet to the
82gateway listed 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
95A wildcard routing entry is specified with a zero
96destination address value, and a mask of all zeroes.
97Wildcard routes will be used
98when the system fails to find other routes matching the
99destination.
100The combination of wildcard routes and routing redirects can provide
101an economical mechanism for routing traffic.
102.Pp
103One opens the channel for passing routing control messages
104by using the socket call shown in the synopsis above:
105.Pp
106The
107.Fa family
108parameter may be
109.Dv AF_UNSPEC
110which will provide
111routing information for all address families, or can be restricted
112to a specific address family by specifying which one is desired.
113There can be more than one routing socket open per system.
114.Pp
115Messages are formed by a header followed by a small
116number of sockaddrs (now variable length particularly
117in the
118.Tn ISO
119case), interpreted by position, and delimited
120by the new length entry in the sockaddr.
121An example of a message with four addresses might be an
122.Tn ISO
123redirect:
124Destination, Netmask, Gateway, and Author of the redirect.
125The interpretation of which address are present is given by a
126bit mask within the header, and the sequence is least significant
127to most significant bit within the vector.
128.Pp
129Any messages sent to the kernel are returned, and copies are sent
130to all interested listeners.
131The exception to this is a new address marked as tentative, where copies
132will be sent once Duplicate Address Detection has completed and
133the tentative flag cleared or the duplicated flag set.
134Also, new address messages will also be emitted when other flags on the address
135change such as deprecated and detached.
136The kernel will provide the process ID for the sender, and the
137sender may use an additional sequence field to distinguish between
138outstanding messages.
139However, message replies may be lost when kernel buffers are exhausted.
140.Pp
141The kernel may reject certain messages, and will indicate this
142by filling in the
143.Fa rtm_errno
144field.
145The routing code returns
146.Er EEXIST
147if
148requested to duplicate an existing entry,
149.Er ESRCH
150if
151requested to delete a non-existent entry,
152or
153.Er ENOBUFS
154if insufficient resources were available
155to install a new route.
156In the current implementation, all routing processes run locally,
157and the values for
158.Fa rtm_errno
159are available through the normal
160.Em errno
161mechanism, even if the routing reply message is lost.
162.Pp
163A process may avoid the expense of reading replies to
164its own messages by issuing a
165.Xr setsockopt 2
166call indicating that the
167.Dv SO_USELOOPBACK
168option
169at the
170.Dv SOL_SOCKET
171level is to be turned off.
172A process may ignore all messages from the routing socket
173by doing a
174.Xr shutdown 2
175system call for further input.
176.Pp
177A process can specify which route message types it's interested in by passing
178an array of route message types to the
179.Xr setsockopt 2
180call with the
181.Dv RO_MSGFILTER
182option at the
183.Dv PF_ROUTE
184level.
185For example, to only get specific messages:
186.Bd -literal -offset indent
187unsigned char rtfilter[] = { RTM_IFINFO, RTM_IFANNOUNCE };
188
189if (setsockopt(routefd, PF_ROUTE, RO_MSGFILTER,
190    &rtfilter, (socklen_t)sizeof(rtfilter)) == -1)
191	err(1, "setsockopt(RO_MSGFILTER)");
192.Ed
193.Pp
194A process can specify which RTM_MISS destination addresses it's interested in
195by passing an array of struct sockaddr to the
196.Xr setsockopt 2
197call with the
198.Dv RO_MISSFILTER
199option at the
200.Dv PF_ROUTE
201level.
202For example, to only get RTM_MISS messages for specific destinations:
203.Bd -literal -offset indent
204char buf[1024] = { '\\0' }, *cp = buf;
205struct sockaddr_in sin = {
206	.sin_family = AF_INET,
207	.sin_len = sizeof(sin),
208};
209
210inet_aton("192.168.0.1", &sin.sin_addr);
211memcpy(cp, &sin, sin.sin_len);
212cp += RT_ROUNDUP(sin.sin_len);
213
214inet_aton("192.168.0.2", &sin.sin_addr);
215memcpy(cp, &sin, sin.sin_len);
216cp += RT_ROUNDUP(sin.sin_len);
217
218if (setsockopt(routefd, PF_ROUTE, RO_MISSFILTER,
219    &sin, (socklen_t)(cp - buf)) == -1)
220	err(1, "setsockopt(RO_MISSFILTER)");
221.Ed
222.Pp
223If a route is in use when it is deleted,
224the routing entry will be marked down and removed from the routing table,
225but the resources associated with it will not
226be reclaimed until all references to it are released.
227User processes can obtain information about the routing
228entry to a specific destination by using a
229.Dv RTM_GET
230message,
231or by reading the
232.Pa /dev/kmem
233device, or by calling
234.Xr sysctl 3 .
235.Pp
236The messages are:
237.Bd -literal
238#define	RTM_ADD		0x1    /* Add Route */
239#define	RTM_DELETE	0x2    /* Delete Route */
240#define	RTM_CHANGE	0x3    /* Change Metrics, Flags, or Gateway */
241#define	RTM_GET		0x4    /* Report Information */
242#define	RTM_LOSING	0x5    /* Kernel Suspects Partitioning */
243#define	RTM_REDIRECT	0x6    /* Told to use different route */
244#define	RTM_MISS	0x7    /* Lookup failed on this address */
245#define RTM_LOCK	0x8	/* fix specified metrics */
246#define RTM_OLDADD	0x9	/* caused by SIOCADDRT */
247#define RTM_OLDDEL	0xa	/* caused by SIOCDELRT */
248#define	RTM_ONEWADDR	0xc    /* Old (pre-8.0) RTM_NEWADDR message */
249// #define RTM_RESOLVE	0xb	/* req to resolve dst to LL addr */
250#define	RTM_ODELADDR	0xd    /* Old (pre-8.0) RTM_DELADDR message */
251#define	RTM_OOIFINFO	0xe    /* Old (pre-1.5) RTM_IFINFO message */
252#define	RTM_OIFINFO	0xf    /* Old (pre-6.0) RTM_IFINFO message */
253#define	RTM_IFANNOUNCE	0x10   /* iface arrival/departure */
254#define	RTM_IEEE80211	0x11	/* IEEE80211 wireless event */
255#define	RTM_SETGATE	0x12	/* set prototype gateway for clones
256				 * (see example in arp_rtrequest).
257				 */
258#define	RTM_LLINFO_UPD	0x13	/* indication to ARP/NDP/etc. that link-layer
259				 * address has changed
260				 */
261#define	RTM_IFINFO	0x14   /* iface/link going up/down etc. */
262#define	RTM_OCHGADDR	0x15   /* Old (pre-8.0) RTM_CHGADDR message */
263#define	RTM_NEWADDR	0x16   /* address being added to iface */
264#define	RTM_DELADDR	0x17   /* address being removed from iface */
265#define	RTM_CHGADDR	0x18   /* address properties changed */
266.Ed
267.Pp
268A message header consists of one of the following:
269.Bd -literal
270struct rt_msghdr {
271    u_short rtm_msglen;        /* to skip over non-understood messages */
272    u_char  rtm_version;       /* future binary compatibility */
273    u_char  rtm_type;          /* message type */
274    u_short rtm_index;         /* index for associated ifp */
275    int     rtm_flags;         /* flags, incl kern & message, e.g. DONE */
276    int     rtm_addrs;         /* bitmask identifying sockaddrs in msg */
277    pid_t   rtm_pid;           /* identify sender */
278    int     rtm_seq;           /* for sender to identify action */
279    int     rtm_errno;         /* why failed */
280    int     rtm_use;           /* from rtentry */
281    u_long  rtm_inits;         /* which metrics we are initializing */
282    struct  rt_metrics rtm_rmx;	/* metrics themselves */
283};
284
285struct if_msghdr {
286    u_short ifm_msglen;        /* to skip over non-understood messages */
287    u_char  ifm_version;       /* future binary compatibility */
288    u_char  ifm_type;          /* message type */
289    int     ifm_addrs;         /* like rtm_addrs */
290    int     ifm_flags;         /* value of if_flags */
291    u_short ifm_index;         /* index for associated ifp */
292    struct  if_data ifm_data;  /* statistics and other data about if */
293};
294
295struct ifa_msghdr {
296    u_short ifam_msglen;       /* to skip over non-understood messages */
297    u_char  ifam_version;      /* future binary compatibility */
298    u_char  ifam_type;         /* message type */
299    u_short ifam_index;        /* index for associated ifp */
300    int     ifam_flags;        /* value of ifa_flags */
301    int     ifam_addrs;        /* like rtm_addrs */
302    pid_t   ifam_pid;          /* identify sender */
303    int     ifam_addrflags;    /* family specific address flags */
304    int     ifam_metric;       /* value of ifa_metric */
305};
306
307struct if_announcemsghdr {
308    u_short ifan_msglen;       /* to skip over non-understood messages */
309    u_char  ifan_version;      /* future binary compatibility */
310    u_char  ifan_type;         /* message type */
311    u_short ifan_index;        /* index for associated ifp */
312    char    ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
313    u_short ifan_what;         /* what type of announcement */
314};
315.Ed
316.Pp
317The
318.Dv RTM_IFINFO
319message uses a
320.Vt if_msghdr
321header, the
322.Dv RTM_NEWADDR ,
323.Dv RTM_CHGADDR ,
324and
325.Dv RTM_DELADDR
326messages use a
327.Vt ifa_msghdr
328header,
329the
330.Dv RTM_IFANNOUNCE
331message uses a
332.Vt if_announcemsghdr
333header,
334and all other messages use the
335.Vt rt_msghdr
336header.
337.Pp
338The metrics structure is:
339.Bd -literal
340struct rt_metrics {
341    u_long rmx_locks;          /* Kernel must leave these values alone */
342    u_long rmx_mtu;            /* MTU for this path */
343    u_long rmx_hopcount;       /* max hops expected */
344    u_long rmx_expire;         /* lifetime for route, e.g. redirect */
345    u_long rmx_recvpipe;       /* inbound delay-bandwidth product */
346    u_long rmx_sendpipe;       /* outbound delay-bandwidth product */
347    u_long rmx_ssthresh;       /* outbound gateway buffer limit */
348    u_long rmx_rtt;            /* estimated round trip time */
349    u_long rmx_rttvar;         /* estimated rtt variance */
350    u_long rmx_pksent;         /* packets sent using this route */
351};
352.Ed
353.Pp
354Flags include the values:
355.Bd -literal
356#define	RTF_UP        0x1       /* route usable */
357#define	RTF_GATEWAY   0x2       /* destination is a gateway */
358#define	RTF_HOST      0x4       /* host entry (net otherwise) */
359#define	RTF_REJECT    0x8       /* host or net unreachable */
360#define	RTF_DYNAMIC   0x10      /* created dynamically (by redirect) */
361#define	RTF_MODIFIED  0x20      /* modified dynamically (by redirect) */
362#define	RTF_DONE      0x40      /* message confirmed */
363#define	RTF_MASK      0x80      /* subnet mask present */
364#define RTF_CONNECTED 0x100     /* hosts on this route are neighbours */
365#define RTF_LLDATA    0x400     /* used by apps to add/del L2 entries */
366#define	RTF_STATIC    0x800     /* manually added */
367#define	RTF_BLACKHOLE 0x1000    /* just discard pkts (during updates) */
368#define	RTF_PROTO2    0x4000    /* protocol specific routing flag */
369#define	RTF_PROTO1    0x8000    /* protocol specific routing flag */
370#define	RTF_SRC       0x10000   /* route has fixed source address */
371#define	RTF_ANNOUNCE  0x20000   /* announce new ARP or NDP entry */
372#define	RTF_LOCAL     0x40000   /* route represents a local address */
373#define	RTF_BROADCAST 0x80000   /* route represents a bcast address */
374.Ed
375.Pp
376Specifiers for metric values in rmx_locks and rtm_inits are:
377.Bd -literal
378#define	RTV_MTU       0x1    /* init or lock _mtu */
379#define	RTV_HOPCOUNT  0x2    /* init or lock _hopcount */
380#define	RTV_EXPIRE    0x4    /* init or lock _expire */
381#define	RTV_RPIPE     0x8    /* init or lock _recvpipe */
382#define	RTV_SPIPE     0x10   /* init or lock _sendpipe */
383#define	RTV_SSTHRESH  0x20   /* init or lock _ssthresh */
384#define	RTV_RTT       0x40   /* init or lock _rtt */
385#define	RTV_RTTVAR    0x80   /* init or lock _rttvar */
386.Ed
387.Pp
388Specifiers for which addresses are present in the messages are:
389.Bd -literal
390#define RTA_DST       0x1    /* destination sockaddr present */
391#define RTA_GATEWAY   0x2    /* gateway sockaddr present */
392#define RTA_NETMASK   0x4    /* netmask sockaddr present */
393#define RTA_GENMASK   0x8    /* cloning mask sockaddr present */
394#define RTA_IFP       0x10   /* interface name sockaddr present */
395#define RTA_IFA       0x20   /* interface addr sockaddr present */
396#define RTA_AUTHOR    0x40   /* sockaddr for author of redirect */
397#define RTA_BRD       0x80   /* for NEWADDR, broadcast or p-p dest addr */
398#define RTA_TAG       0x100  /* route tag */
399.Ed
400.Pp
401Flags for IPv6 addresses:
402.Bd -literal
403#define IN6_IFF_ANYCAST		0x01	/* anycast address */
404#define IN6_IFF_TENTATIVE	0x02	/* tentative address */
405#define IN6_IFF_DUPLICATED	0x04	/* DAD detected duplicate */
406#define IN6_IFF_DETACHED	0x08	/* may be detached from the link */
407#define IN6_IFF_DEPRECATED	0x10	/* deprecated address */
408#define IN6_IFF_NODAD		0x20	/* don't perform DAD on this address
409					 * (used only at first SIOC* call)
410					 */
411#define IN6_IFF_AUTOCONF	0x40	/* autoconfigurable address. */
412#define IN6_IFF_TEMPORARY	0x80	/* temporary (anonymous) address. */
413.Ed
414.Sh SEE ALSO
415.Xr socket 2 ,
416.Xr sysctl 3
417.Sh HISTORY
418Since
419.Nx 8.0 ,
420.Cm RTF_CLONED ,
421.Cm RTF_CLONING ,
422.Cm RTF_LLINFO ,
423.Cm RTF_XRESOLVE
424and
425.Cm RTM_RESOLVE
426were obsolete.
427.Cm RTF_CONNECTED
428and
429.Cm RTF_LLDATA
430appeared in
431.Nx 8.0 .
432.Pp
433.Vt ifa_msghdr
434gained the fields ifam_pid and ifam_addrflags in
435.Nx 8.0 .
436