xref: /dragonfly/share/man/man4/route.4 (revision 40657594)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     From: @(#)route.4	8.6 (Berkeley) 4/19/94
29.\" $FreeBSD: src/share/man/man4/route.4,v 1.9.2.6 2002/03/17 09:12:44 schweikh Exp $
30.\" $DragonFly: src/share/man/man4/route.4,v 1.3 2004/03/11 12:28:55 hmp Exp $
31.\"
32.Dd February 8, 2019
33.Dt ROUTE 4
34.Os
35.Sh NAME
36.Nm route
37.Nd kernel packet forwarding database
38.Sh SYNOPSIS
39.In sys/types.h
40.In sys/time.h
41.In sys/socket.h
42.In net/if.h
43.In net/route.h
44.Ft int
45.Fn socket PF_ROUTE SOCK_RAW "int family"
46.Sh DESCRIPTION
47.Dx
48provides some packet routing facilities.
49The kernel maintains a routing information database, which
50is used in selecting the appropriate network interface when
51transmitting packets.
52.Pp
53A user process (or possibly multiple co-operating processes)
54maintains this database by sending messages over a special kind
55of socket.
56This supplants fixed size
57.Xr ioctl 2 Ns 's
58used in earlier releases.
59Routing table changes may only be carried out by the super user.
60.Pp
61The operating system may spontaneously emit routing messages in response
62to external events, such as receipt of a re-direct, or failure to
63locate a suitable route for a request.
64The message types are described in greater detail below.
65.Pp
66Routing database entries come in two flavors: for a specific
67host, or for all hosts on a generic subnetwork (as specified
68by a bit mask and value under the mask.
69The effect of wildcard or default route may be achieved by using
70a mask of all zeros, and there may be hierarchical routes.
71.Pp
72When the system is booted and addresses are assigned
73to the network interfaces, each protocol family
74installs a routing table entry for each interface when it is ready for traffic.
75Normally the protocol specifies the route
76through each interface as a
77.Dq direct
78connection to the destination host
79or network.  If the route is direct, the transport layer of
80a protocol family usually requests the packet be sent to the
81same host specified in the packet.  Otherwise, the interface
82is requested to address the packet to the gateway listed in the routing entry
83(i.e. the packet is forwarded).
84.Pp
85When routing a packet,
86the kernel will attempt to find
87the most specific route matching the destination.
88(If there are two different mask and value-under-the-mask pairs
89that match, the more specific is the one with more bits in the mask.
90A route to a host is regarded as being supplied with a mask of
91as many ones as there are bits in the destination).
92If no entry is found, the destination is declared to be unreachable,
93and a routing\-miss message is generated if there are any
94listeners on the routing control socket described below.
95.Pp
96A wildcard routing entry is specified with a zero
97destination address value, and a mask of all zeroes.
98Wildcard routes will be used
99when the system fails to find other routes matching the
100destination.  The combination of wildcard
101routes and routing redirects can provide an economical
102mechanism for routing traffic.
103.Pp
104One opens the channel for passing routing control messages
105by using the socket call shown in the synopsis above:
106.Pp
107The
108.Fa family
109parameter may be
110.Dv AF_UNSPEC
111which will provide
112routing information for all address families, or can be restricted
113to a specific address family by specifying which one is desired.
114There can be more than one routing socket open per system.
115.Pp
116Messages are formed by a header followed by a small
117number of sockaddrs (now variable length particularly
118in the
119.Tn ISO
120case), interpreted by position, and delimited
121by the new length entry in the sockaddr.
122An example of a message with four addresses might be an
123.Tn ISO
124redirect:
125Destination, Netmask, Gateway, and Author of the redirect.
126The interpretation of which address are present is given by a
127bit mask within the header, and the sequence is least significant
128to most significant bit within the vector.
129.Pp
130Any messages sent to the kernel are returned, and copies are sent
131to all interested listeners.  The kernel will provide the process
132ID for the sender, and the sender may use an additional sequence
133field to distinguish between outstanding messages.  However,
134message replies may be lost when kernel buffers are exhausted.
135.Pp
136The kernel may reject certain messages, and will indicate this
137by filling in the
138.Ar rtm_errno
139field.
140The routing code returns
141.Er EEXIST
142if
143requested to duplicate an existing entry,
144.Er ESRCH
145if
146requested to delete a non-existent entry,
147or
148.Er ENOBUFS
149if insufficient resources were available
150to install a new route.
151In the current implementation, all routing processes run locally,
152and the values for
153.Ar rtm_errno
154are available through the normal
155.Em errno
156mechanism, even if the routing reply message is lost.
157.Pp
158A process may avoid the expense of reading replies to
159its own messages by issuing a
160.Xr setsockopt 2
161call indicating that the
162.Dv SO_USELOOPBACK
163option
164at the
165.Dv SOL_SOCKET
166level is to be turned off.
167A process may ignore all messages from the routing socket
168by doing a
169.Xr shutdown 2
170system call for further input.
171.Pp
172A process can specify which route message types it's interested in by passing
173an array of route message types to the
174.Xr setsockopt 2
175call with the
176.Dv ROUTE_MSGFILTER
177option at the
178.Dv PF_ROUTE
179level.
180For example, to only get specific messages:
181.Bd -literal -offset indent
182unsigned int rtfilter;
183
184rtfilter = ROUTE_FILTER(RTM_IFINFO) | ROUTE_FILTER(RTM_IFANNOUNCE);
185
186if (setsockopt(routefd, PF_ROUTE, ROUTE_MSGFILTER,
187    &rtfilter, sizeof(rtfilter)) == -1)
188	err(1, "setsockopt(ROUTE_MSGFILTER)");
189.Ed
190.Pp
191A process can specify which RTM_MISS destination addresses it's interested in
192by passing an array of struct sockaddr to the
193.Xr setsockopt 2
194call with the
195.Dv RO_MISSFILTER
196option at the
197.Dv PF_ROUTE
198level.
199For example, to only get RTM_MISS messages for specific destinations:
200.Bd -literal -offset indent
201char buf[1024] = { '\\0' }, *cp = buf;
202struct sockaddr_in sin = {
203	.sin_family = AF_INET,
204	.sin_len = sizeof(sin),
205};
206
207inet_aton("192.168.0.1", &sin.sin_addr);
208memcpy(cp, &sin, sin.sin_len);
209cp += RT_ROUNDUP(sin.sin_len);
210
211inet_aton("192.168.0.2", &sin.sin_addr);
212memcpy(cp, &sin, sin.sin_len);
213cp += RT_ROUNDUP(sin.sin_len);
214
215if (setsockopt(routefd, PF_ROUTE, RO_MISSFILTER,
216    &sin, (socklen_t)(cp - buf)) == -1)
217	err(1, "setsockopt(RO_MISSFILTER)");
218.Ed
219.Pp
220If a route is in use when it is deleted,
221the routing entry will be marked down and removed from the routing table,
222but the resources associated with it will not
223be reclaimed until all references to it are released.
224User processes can obtain information about the routing
225entry to a specific destination by using a
226.Dv RTM_GET
227message, or by calling
228.Xr sysctl 3 .
229.Pp
230Messages include:
231.Bd -literal
232#define	RTM_ADD		0x1    /* Add Route */
233#define	RTM_DELETE	0x2    /* Delete Route */
234#define	RTM_CHANGE	0x3    /* Change Metrics, Flags, or Gateway */
235#define	RTM_GET		0x4    /* Report Information */
236#define	RTM_LOSING	0x5    /* Kernel Suspects Partitioning */
237#define	RTM_REDIRECT	0x6    /* Told to use different route */
238#define	RTM_MISS	0x7    /* Lookup failed on this address */
239#define	RTM_LOCK	0x8    /* fix specified metrics */
240#define	RTM_RESOLVE	0xb    /* request to resolve dst to LL addr */
241#define	RTM_NEWADDR	0xc    /* address being added to iface */
242#define	RTM_DELADDR	0xd    /* address being removed from iface */
243#define	RTM_IFINFO	0xe    /* iface going up/down etc. */
244#define	RTM_NEWMADDR	0xf    /* mcast group membership being added to if */
245#define	RTM_DELMADDR	0x10   /* mcast group membership being deleted */
246#define	RTM_IFANNOUNCE	0x11   /* iface arrival/departure */
247.Ed
248.Pp
249A message header consists of one of the following:
250.Bd -literal
251struct rt_msghdr {
252    u_short rtm_msglen;         /* to skip over non-understood messages */
253    u_char  rtm_version;        /* future binary compatibility */
254    u_char  rtm_type;           /* message type */
255    u_short rtm_index;          /* index for associated ifp */
256    int     rtm_flags;          /* flags, incl. kern & message, e.g. DONE */
257    int     rtm_addrs;          /* bitmask identifying sockaddrs in msg */
258    pid_t   rtm_pid;            /* identify sender */
259    int     rtm_seq;            /* for sender to identify action */
260    int     rtm_errno;          /* why failed */
261    int     rtm_use;            /* from rtentry */
262    u_long  rtm_inits;          /* which metrics we are initializing */
263    struct  rt_metrics rtm_rmx;	/* metrics themselves */
264};
265
266struct if_msghdr {
267    u_short ifm_msglen;         /* to skip over non-understood messages */
268    u_char  ifm_version;        /* future binary compatibility */
269    u_char  ifm_type;           /* message type */
270    u_short ifm_index;          /* index for associated ifp */
271    int     ifm_flags;          /* value of if_flags */
272    int     ifm_addrs;          /* like rtm_addrs */
273    struct  if_data ifm_data;   /* statistics and other data about if */
274};
275
276struct ifa_msghdr {
277    u_short ifam_msglen;        /* to skip over non-understood messages */
278    u_char  ifam_version;       /* future binary compatibility */
279    u_char  ifam_type;          /* message type */
280    u_short ifam_index;         /* index for associated ifp */
281    int     ifam_flags;         /* value of ifa_flags */
282    int     ifam_addrs;         /* like rtm_addrs */
283    int     ifam_addrflags;	/* family specific address flags */
284    int     ifam_metric;        /* value of ifa_metric */
285};
286
287struct ifma_msghdr {
288    u_short ifmam_msglen;       /* to skip over non-understood messages */
289    u_char  ifmam_version;      /* future binary compatibility */
290    u_char  ifmam_type;         /* message type */
291    u_short ifmam_index;        /* index for associated ifp */
292    int     ifmam_flags;        /* value of ifa_flags */
293    int     ifmam_addrs;        /* like rtm_addrs */
294};
295
296struct if_announcemsghdr {
297	u_short	ifan_msglen;	/* to skip over non-understood messages */
298	u_char	ifan_version;	/* future binary compatibility */
299	u_char	ifan_type;	/* message type */
300	u_short	ifan_index;	/* index for associated ifp */
301	char	ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */
302	u_short	ifan_what;	/* what type of announcement */
303};
304.Ed
305.Pp
306The
307.Dv RTM_IFINFO
308message uses a
309.Ar if_msghdr
310header, the
311.Dv RTM_NEWADDR
312and
313.Dv RTM_DELADDR
314messages use a
315.Ar ifa_msghdr
316header, the
317.Dv RTM_NEWMADDR
318and
319.Dv RTM_DELMADDR
320messages use a
321.Vt ifma_msghdr
322header, the
323.Dv RTM_IFANNOUNCE
324message uses a
325.Vt if_announcemsghdr
326header,
327and all other messages use the
328.Ar rt_msghdr
329header.
330.Pp
331The
332.Dq Li "struct rt_metrics"
333and the flag bits are as defined in
334.Xr rtentry 9 .
335.Pp
336Specifiers for metric values in rmx_locks and rtm_inits are:
337.Bd -literal
338#define	RTV_MTU       0x1    /* init or lock _mtu */
339#define	RTV_HOPCOUNT  0x2    /* init or lock _hopcount */
340#define	RTV_EXPIRE    0x4    /* init or lock _expire */
341#define	RTV_RPIPE     0x8    /* init or lock _recvpipe */
342#define	RTV_SPIPE     0x10   /* init or lock _sendpipe */
343#define	RTV_SSTHRESH  0x20   /* init or lock _ssthresh */
344#define	RTV_RTT       0x40   /* init or lock _rtt */
345#define	RTV_RTTVAR    0x80   /* init or lock _rttvar */
346.Ed
347.Pp
348Specifiers for which addresses are present in the messages are:
349.Bd -literal
350#define RTA_DST       0x1    /* destination sockaddr present */
351#define RTA_GATEWAY   0x2    /* gateway sockaddr present */
352#define RTA_NETMASK   0x4    /* netmask sockaddr present */
353#define RTA_GENMASK   0x8    /* cloning mask sockaddr present */
354#define RTA_IFP       0x10   /* interface name sockaddr present */
355#define RTA_IFA       0x20   /* interface addr sockaddr present */
356#define RTA_AUTHOR    0x40   /* sockaddr for author of redirect */
357#define RTA_BRD       0x80   /* for NEWADDR, broadcast or p-p dest addr */
358.Ed
359.Sh SEE ALSO
360.Xr sysctl 3 ,
361.Xr route 8 ,
362.Xr rtentry 9
363.Sh HISTORY
364A
365.Dv PF_ROUTE
366protocol family first appeared in
367.Bx 4.3 reno .
368