1.\" $OpenBSD: route.4,v 1.7 2001/03/01 16:11:19 aaron 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. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)route.4 8.6 (Berkeley) 4/19/94 36.\" 37.Dd April 19, 1994 38.Dt ROUTE 4 39.Os 40.Sh NAME 41.Nm route 42.Nd kernel packet forwarding database 43.Sh SYNOPSIS 44.Fd #include <sys/socket.h> 45.Fd #include <net/if.h> 46.Fd #include <net/route.h> 47.Ft int 48.Fn socket PF_ROUTE SOCK_RAW "int family" 49.Sh DESCRIPTION 50.Tn UNIX 51provides some packet routing facilities. 52The kernel maintains a routing information database, which 53is used in selecting the appropriate network interface when 54transmitting packets. 55.Pp 56A user process (or possibly multiple co-operating processes) 57maintains this database by sending messages over a special kind 58of socket. 59This supplants fixed size 60.Xr ioctl 2 Ns 's 61used in earlier releases. 62Routing table changes may only be carried out by the super user. 63.Pp 64The operating system may spontaneously emit routing messages in response 65to external events, such as receipt of a re-direct, or failure to 66locate a suitable route for a request. 67The message types are described in greater detail below. 68.Pp 69Routing database entries come in two flavors: for a specific 70host, or for all hosts on a generic subnetwork (as specified 71by a bit mask and value under the mask. 72The effect of wildcard or default route may be achieved by using 73a mask of all zeros, and there may be hierarchical routes. 74.Pp 75When the system is booted and addresses are assigned 76to the network interfaces, each protocol family 77installs a routing table entry for each interface when it is ready for traffic. 78Normally the protocol specifies the route 79through each interface as a 80.Dq direct 81connection to the destination host 82or network. If the route is direct, the transport layer of 83a protocol family usually requests the packet be sent to the 84same host specified in the packet. Otherwise, the interface 85is requested to address the packet to the gateway listed in the routing entry 86(i.e. the packet is forwarded). 87.Pp 88When routing a packet, 89the kernel will attempt to find 90the most specific route matching the destination. 91(If there are two different mask and value-under-the-mask pairs 92that match, the more specific is the one with more bits in the mask. 93A route to a host is regarded as being supplied with a mask of 94as many ones as there are bits in the destination). 95If no entry is found, the destination is declared to be unreachable, 96and a routing\-miss message is generated if there are any 97listers on the routing control socket described below. 98.Pp 99A wildcard routing entry is specified with a zero 100destination address value, and a mask of all zeroes. 101Wildcard routes will be used 102when the system fails to find other routes matching the 103destination. The combination of wildcard 104routes and routing redirects can provide an economical 105mechanism for routing traffic. 106.Pp 107One opens the channel for passing routing control messages 108by using the socket call shown in the synopsis above: 109.Pp 110The 111.Fa family 112parameter may be 113.Dv AF_UNSPEC 114which will provide 115routing information for all address families, or can be restricted 116to a specific address family by specifying which one is desired. 117There can be more than one routing socket open per system. 118.Pp 119Messages are formed by a header followed by a small 120number of sockaddrs (now variable length particularly 121in the 122.Tn ISO 123case), interpreted by position, and delimited 124by the new length entry in the sockaddr. 125An example of a message with four addresses might be an 126.Tn ISO 127redirect: 128Destination, Netmask, Gateway, and Author of the redirect. 129The interpretation of which address are present is given by a 130bit mask within the header, and the sequence is least significant 131to most significant bit within the vector. 132.Pp 133Any messages sent to the kernel are returned, and copies are sent 134to all interested listeners. The kernel will provide the process 135id. for the sender, and the sender may use an additional sequence 136field to distinguish between outstanding messages. However, 137message replies may be lost when kernel buffers are exhausted. 138.Pp 139The kernel may reject certain messages, and will indicate this 140by filling in the 141.Ar rtm_errno 142field. 143The routing code returns 144.Dv EEXIST 145if 146requested to duplicate an existing entry, 147.Dv ESRCH 148if 149requested to delete a non-existent entry, 150or 151.Dv ENOBUFS 152if insufficient resources were available 153to install a new route. 154In the current implementation, all routing process run locally, 155and the values for 156.Ar rtm_errno 157are available through the normal 158.Va errno 159mechanism, even if the routing reply message is lost. 160.Pp 161A process may avoid the expense of reading replies to 162its own messages by issuing a 163.Xr setsockopt 2 164call indicating that the 165.Dv SO_USELOOPBACK 166option 167at the 168.Dv SOL_SOCKET 169level is to be turned off. 170A process may ignore all messages from the routing socket 171by doing a 172.Xr shutdown 2 173system call for further input. 174.Pp 175If a route is in use when it is deleted, 176the routing entry will be marked down and removed from the routing table, 177but the resources associated with it will not 178be reclaimed until all references to it are released. 179User processes can obtain information about the routing 180entry to a specific destination by using a 181.Dv RTM_GET 182message. 183.Pp 184Messages include: 185.Bd -literal 186#define RTM_ADD 0x1 /* Add Route */ 187#define RTM_DELETE 0x2 /* Delete Route */ 188#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 189#define RTM_GET 0x4 /* Report Information */ 190#define RTM_LOOSING 0x5 /* Kernel Suspects Partitioning */ 191#define RTM_REDIRECT 0x6 /* Told to use different route */ 192#define RTM_MISS 0x7 /* Lookup failed on this address */ 193#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr */ 194.Ed 195.Pp 196A message header consists of: 197.Bd -literal 198struct rt_msghdr { 199 u_short rmt_msglen; /* to skip over non-understood messages */ 200 u_char rtm_version; /* future binary compatibility */ 201 u_char rtm_type; /* message type */ 202 u_short rmt_index; /* index for associated ifp */ 203 pid_t rmt_pid; /* identify sender */ 204 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 205 int rtm_seq; /* for sender to identify action */ 206 int rtm_errno; /* why failed */ 207 int rtm_flags; /* flags, incl kern & message, e.g. DONE */ 208 int rtm_use; /* from rtentry */ 209 u_long rtm_inits; /* which values we are initializing */ 210 struct rt_metrics rtm_rmx; /* metrics themselves */ 211}; 212.Ed 213.Pp 214where 215.Bd -literal 216struct rt_metrics { 217 u_long rmx_locks; /* Kernel must leave these values alone */ 218 u_long rmx_mtu; /* MTU for this path */ 219 u_long rmx_hopcount; /* max hops expected */ 220 u_long rmx_expire; /* lifetime for route, e.g. redirect */ 221 u_long rmx_recvpipe; /* inbound delay-bandwidth product */ 222 u_long rmx_sendpipe; /* outbound delay-bandwidth product */ 223 u_long rmx_ssthresh; /* outbound gateway buffer limit */ 224 u_long rmx_rtt; /* estimated round trip time */ 225 u_long rmx_rttvar; /* estimated rtt variance */ 226}; 227.Ed 228.Pp 229Flags include the values: 230.Bd -literal 231#define RTF_UP 0x1 /* route usable */ 232#define RTF_GATEWAY 0x2 /* destination is a gateway */ 233#define RTF_HOST 0x4 /* host entry (net otherwise) */ 234#define RTF_REJECT 0x8 /* host or net unreachable */ 235#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 236#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 237#define RTF_DONE 0x40 /* message confirmed */ 238#define RTF_MASK 0x80 /* subnet mask present */ 239#define RTF_CLONING 0x100 /* generate new routes on use */ 240#define RTF_XRESOLVE 0x200 /* external daemon resolves name */ 241#define RTF_LLINFO 0x400 /* generated by ARP or ESIS */ 242#define RTF_STATIC 0x800 /* manually added */ 243#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 244#define RTF_PROTO2 0x4000 /* protocol specific routing flag #1 */ 245#define RTF_PROTO1 0x8000 /* protocol specific routing flag #2 */ 246.Ed 247.Pp 248Specifiers for metric values in rmx_locks and rtm_inits are: 249.Bd -literal 250#define RTV_SSTHRESH 0x1 /* init or lock _ssthresh */ 251#define RTV_RPIPE 0x2 /* init or lock _recvpipe */ 252#define RTV_SPIPE 0x4 /* init or lock _sendpipe */ 253#define RTV_HOPCOUNT 0x8 /* init or lock _hopcount */ 254#define RTV_RTT 0x10 /* init or lock _rtt */ 255#define RTV_RTTVAR 0x20 /* init or lock _rttvar */ 256#define RTV_MTU 0x40 /* init or lock _mtu */ 257.Ed 258.Pp 259Specifiers for which addresses are present in the messages are: 260.Bd -literal 261#define RTA_DST 0x1 /* destination sockaddr present */ 262#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 263#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 264#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 265#define RTA_IFP 0x10 /* interface name sockaddr present */ 266#define RTA_IFA 0x20 /* interface addr sockaddr present */ 267#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 268.Ed 269