1.\" $NetBSD: route.4,v 1.15 2003/08/07 10:31:03 agc 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 April 19, 1994 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. If the route is direct, the transport layer of 78a protocol family usually requests the packet be sent to the 79same host specified in the packet. Otherwise, the interface 80is requested to address the packet to the gateway listed in the routing entry 81(i.e. the packet is forwarded). 82.Pp 83When routing a packet, 84the kernel will attempt to find 85the most specific route matching the destination. 86(If there are two different mask and value-under-the-mask pairs 87that match, the more specific is the one with more bits in the mask. 88A route to a host is regarded as being supplied with a mask of 89as many ones as there are bits in the destination). 90If no entry is found, the destination is declared to be unreachable, 91and a routing\-miss message is generated if there are any 92listeners on the routing control socket described below. 93.Pp 94A wildcard routing entry is specified with a zero 95destination address value, and a mask of all zeroes. 96Wildcard routes will be used 97when the system fails to find other routes matching the 98destination. The combination of wildcard 99routes and routing redirects can provide an economical 100mechanism for routing traffic. 101.Pp 102One opens the channel for passing routing control messages 103by using the socket call shown in the synopsis above: 104.Pp 105The 106.Fa family 107parameter may be 108.Dv AF_UNSPEC 109which will provide 110routing information for all address families, or can be restricted 111to a specific address family by specifying which one is desired. 112There can be more than one routing socket open per system. 113.Pp 114Messages are formed by a header followed by a small 115number of sockaddrs (now variable length particularly 116in the 117.Tn ISO 118case), interpreted by position, and delimited 119by the new length entry in the sockaddr. 120An example of a message with four addresses might be an 121.Tn ISO 122redirect: 123Destination, Netmask, Gateway, and Author of the redirect. 124The interpretation of which address are present is given by a 125bit mask within the header, and the sequence is least significant 126to most significant bit within the vector. 127.Pp 128Any messages sent to the kernel are returned, and copies are sent 129to all interested listeners. The kernel will provide the process 130ID for the sender, and the sender may use an additional sequence 131field to distinguish between outstanding messages. However, 132message replies may be lost when kernel buffers are exhausted. 133.Pp 134The kernel may reject certain messages, and will indicate this 135by filling in the 136.Ar rtm_errno 137field. 138The routing code returns 139.Dv EEXIST 140if 141requested to duplicate an existing entry, 142.Dv ESRCH 143if 144requested to delete a non-existent entry, 145or 146.Dv ENOBUFS 147if insufficient resources were available 148to install a new route. 149In the current implementation, all routing processes run locally, 150and the values for 151.Ar rtm_errno 152are available through the normal 153.Em errno 154mechanism, even if the routing reply message is lost. 155.Pp 156A process may avoid the expense of reading replies to 157its own messages by issuing a 158.Xr setsockopt 2 159call indicating that the 160.Dv SO_USELOOPBACK 161option 162at the 163.Dv SOL_SOCKET 164level is to be turned off. 165A process may ignore all messages from the routing socket 166by doing a 167.Xr shutdown 2 168system call for further input. 169.Pp 170If a route is in use when it is deleted, 171the routing entry will be marked down and removed from the routing table, 172but the resources associated with it will not 173be reclaimed until all references to it are released. 174User processes can obtain information about the routing 175entry to a specific destination by using a 176.Dv RTM_GET 177message, 178or by reading the 179.Pa /dev/kmem 180device, or by calling 181.Xr sysctl 3 . 182.Pp 183The messages are: 184.Bd -literal 185#define RTM_ADD 0x1 /* Add Route */ 186#define RTM_DELETE 0x2 /* Delete Route */ 187#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 188#define RTM_GET 0x4 /* Report Information */ 189#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 190#define RTM_REDIRECT 0x6 /* Told to use different route */ 191#define RTM_MISS 0x7 /* Lookup failed on this address */ 192#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr */ 193#define RTM_NEWADDR 0xc /* address being added to iface */ 194#define RTM_DELADDR 0xd /* address being removed from iface */ 195#define RTM_OIFINFO 0xe /* Old (pre-1.5) RTM_IFINFO message */ 196#define RTM_IFINFO 0xf /* iface/link going up/down etc. */ 197#define RTM_IFANNOUNCE 0x10 /* iface arrival/departure */ 198.Ed 199.Pp 200A message header consists of one of the following: 201.Bd -literal 202struct rt_msghdr { 203 u_short rtm_msglen; /* to skip over non-understood messages */ 204 u_char rtm_version; /* future binary compatibility */ 205 u_char rtm_type; /* message type */ 206 u_short rtm_index; /* index for associated ifp */ 207 int rtm_flags; /* flags, incl kern \*[Am] message, e.g. DONE */ 208 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 209 pid_t rtm_pid; /* identify sender */ 210 int rtm_seq; /* for sender to identify action */ 211 int rtm_errno; /* why failed */ 212 int rtm_use; /* from rtentry */ 213 u_long rtm_inits; /* which metrics we are initializing */ 214 struct rt_metrics rtm_rmx; /* metrics themselves */ 215}; 216 217struct if_msghdr { 218 u_short ifm_msglen; /* to skip over non-understood messages */ 219 u_char ifm_version; /* future binary compatibility */ 220 u_char ifm_type; /* message type */ 221 int ifm_addrs; /* like rtm_addrs */ 222 int ifm_flags; /* value of if_flags */ 223 u_short ifm_index; /* index for associated ifp */ 224 struct if_data ifm_data; /* statistics and other data about if */ 225}; 226 227struct ifa_msghdr { 228 u_short ifam_msglen; /* to skip over non-understood messages */ 229 u_char ifam_version; /* future binary compatibility */ 230 u_char ifam_type; /* message type */ 231 int ifam_addrs; /* like rtm_addrs */ 232 int ifam_flags; /* value of ifa_flags */ 233 u_short ifam_index; /* index for associated ifp */ 234 int ifam_metric; /* value of ifa_metric */ 235}; 236 237struct if_announcemsghdr { 238 u_short ifan_msglen; /* to skip over non-understood messages */ 239 u_char ifan_version; /* future binary compatibility */ 240 u_char ifan_type; /* message type */ 241 u_short ifan_index; /* index for associated ifp */ 242 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 243 u_short ifan_what; /* what type of announcement */ 244}; 245.Ed 246.Pp 247The 248.Dv RTM_IFINFO 249message uses a 250.Ar if_msghdr 251header, the 252.Dv RTM_NEWADDR 253and 254.Dv RTM_DELADDR 255messages use a 256.Ar ifa_msghdr 257header, 258the 259.Dv RTM_IFANNOUNCE 260message uses a 261.Ar if_announcemsghdr 262header, 263and all other messages use the 264.Ar rt_msghdr 265header. 266.Pp 267The metrics structure is: 268.Bd -literal 269struct rt_metrics { 270 u_long rmx_locks; /* Kernel must leave these values alone */ 271 u_long rmx_mtu; /* MTU for this path */ 272 u_long rmx_hopcount; /* max hops expected */ 273 u_long rmx_expire; /* lifetime for route, e.g. redirect */ 274 u_long rmx_recvpipe; /* inbound delay-bandwidth product */ 275 u_long rmx_sendpipe; /* outbound delay-bandwidth product */ 276 u_long rmx_ssthresh; /* outbound gateway buffer limit */ 277 u_long rmx_rtt; /* estimated round trip time */ 278 u_long rmx_rttvar; /* estimated rtt variance */ 279 u_long rmx_pksent; /* packets sent using this route */ 280}; 281.Ed 282.Pp 283Flags include the values: 284.Bd -literal 285#define RTF_UP 0x1 /* route usable */ 286#define RTF_GATEWAY 0x2 /* destination is a gateway */ 287#define RTF_HOST 0x4 /* host entry (net otherwise) */ 288#define RTF_REJECT 0x8 /* host or net unreachable */ 289#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 290#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 291#define RTF_DONE 0x40 /* message confirmed */ 292#define RTF_MASK 0x80 /* subnet mask present */ 293#define RTF_CLONING 0x100 /* generate new routes on use */ 294#define RTF_XRESOLVE 0x200 /* external daemon resolves name */ 295#define RTF_LLINFO 0x400 /* generated by ARP or ESIS */ 296#define RTF_STATIC 0x800 /* manually added */ 297#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 298#define RTF_CLONED 0x2000 /* this is a cloned route */ 299#define RTF_PROTO2 0x4000 /* protocol specific routing flag */ 300#define RTF_PROTO1 0x8000 /* protocol specific routing flag */ 301.Ed 302.Pp 303Specifiers for metric values in rmx_locks and rtm_inits are: 304.Bd -literal 305#define RTV_MTU 0x1 /* init or lock _mtu */ 306#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 307#define RTV_EXPIRE 0x4 /* init or lock _expire */ 308#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 309#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 310#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 311#define RTV_RTT 0x40 /* init or lock _rtt */ 312#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 313.Ed 314.Pp 315Specifiers for which addresses are present in the messages are: 316.Bd -literal 317#define RTA_DST 0x1 /* destination sockaddr present */ 318#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 319#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 320#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 321#define RTA_IFP 0x10 /* interface name sockaddr present */ 322#define RTA_IFA 0x20 /* interface addr sockaddr present */ 323#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 324#define RTA_BRD 0x80 /* for NEWADDR, broadcast or p-p dest addr */ 325.Ed 326.Sh SEE ALSO 327.Xr socket 2 , 328.Xr sysctl 3 329