1.\" $OpenBSD: route.4,v 1.28 2009/03/31 01:37:33 dlg 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: March 31 2009 $ 34.Dt ROUTE 4 35.Os 36.Sh NAME 37.Nm route 38.Nd kernel packet forwarding database 39.Sh SYNOPSIS 40.Fd #include <sys/socket.h> 41.Fd #include <net/if.h> 42.Fd #include <net/route.h> 43.Ft int 44.Fn socket PF_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 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. 102Routes created by redirects from wildcard routes and other routes 103will be marked 104.Em cloned , 105until their 106.Dq parent 107from which they were created has disappeared. 108.Pp 109Route labels can be attached to routes and may contain arbitrary 110information about the route. 111Labels are sent over the routing socket (see below) as 112.Vt sockaddr_rtlabel 113structures. 114.Ss The Routing Socket 115One opens the channel for passing routing control messages 116by using the 117.Xr socket 2 118call shown in the 119.Sx SYNOPSIS 120above. 121.Pp 122The 123.Fa family 124parameter may be 125.Dv AF_UNSPEC , 126which will provide 127routing information for all address families, or can be restricted 128to a specific address family by specifying which one is desired. 129There can be more than one routing socket open per system. 130.Pp 131Messages are formed by a header followed by a small 132number of 133.Vt sockaddr 134structures (which are variable length), 135interpreted by position, and delimited 136by the length entry in the 137.Vt sockaddr . 138An example of a message with four addresses might be an 139IPv4 route addition: the destination, netmask, gateway, and label, 140since both netmasks and labels are sent over the routing socket as 141.Vt sockaddr 142structures. 143The interpretation of which addresses are present is given by a 144bit mask within the header, and the sequence is least significant 145to most significant bit within the vector. 146.Pp 147Any messages sent to the kernel are returned, and copies are sent 148to all interested listeners. 149The kernel will provide the process ID 150for the sender, and the sender may use an additional sequence 151field to distinguish between outstanding messages. 152However, message replies may be lost when kernel buffers are exhausted. 153.Pp 154The kernel may reject certain messages, and will indicate this 155by filling in the 156.Va rtm_errno 157field. 158The routing code returns 159.Er EEXIST 160if 161requested to duplicate an existing entry, 162.Er ESRCH 163if 164requested to delete a non-existent entry, 165or 166.Er ENOBUFS 167if insufficient resources were available 168to install a new route. 169In the current implementation, all routing processes run locally, 170and the values for 171.Va rtm_errno 172are available through the normal 173.Va errno 174mechanism, even if the routing reply message is lost. 175.Pp 176A process may avoid the expense of reading replies to 177its own messages by issuing a 178.Xr setsockopt 2 179call indicating that the 180.Dv SO_USELOOPBACK 181option at the 182.Dv SOL_SOCKET 183level is to be turned off. 184A process may ignore all messages from the routing socket 185by doing a 186.Xr shutdown 2 187system call for further input. 188.Pp 189A process can specify which route message types it's interested in 190by using 191.Dv ROUTE_FILTER(int type) 192and issuing a setsockopt call with the 193.Dv ROUTE_MSGFILTER 194option at the 195.Dv AF_ROUTE 196level. 197For example, to only get interface specific messages: 198.Bd -literal -offset indent 199unsigned int rtfilter; 200 201rtfilter = ROUTE_FILTER(RTM_IFINFO) | 202 ROUTE_FILTER(RTM_IFANNOUNCE); 203 204if (setsockopt(routefd, PF_ROUTE, ROUTE_MSGFILTER, 205 &rtfilter, sizeof(rtfilter)) == -1) 206 err(1, "setsockopt(ROUTE_MSGFILTER)"); 207.Ed 208.Pp 209If a route is in use when it is deleted, 210the routing entry will be marked down and removed from the routing table, 211but the resources associated with it will not 212be reclaimed until all references to it are released. 213User processes can obtain information about the routing 214entry to a specific destination by using a 215.Dv RTM_GET 216message or via the 217.Dv PF_ROUTE 218.Xr sysctl 3 . 219.Pp 220Messages include: 221.Bd -literal 222#define RTM_ADD 0x1 /* Add Route */ 223#define RTM_DELETE 0x2 /* Delete Route */ 224#define RTM_CHANGE 0x3 /* Change Metrics or flags */ 225#define RTM_GET 0x4 /* Report Metrics */ 226#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 227#define RTM_REDIRECT 0x6 /* Told to use different route */ 228#define RTM_MISS 0x7 /* Lookup failed on this address */ 229#define RTM_LOCK 0x8 /* fix specified metrics */ 230#define RTM_RESOLVE 0xb /* req to resolve dst to LL addr */ 231#define RTM_NEWADDR 0xc /* address being added to iface */ 232#define RTM_DELADDR 0xd /* address being removed from iface */ 233#define RTM_IFINFO 0xe /* iface going up/down etc. */ 234#define RTM_IFANNOUNCE 0xf /* iface arrival/departure */ 235.Ed 236.Pp 237A message header consists of one of the following: 238.Bd -literal 239struct rt_msghdr { 240 u_short rtm_msglen; /* to skip over non-understood messages */ 241 u_char rtm_version; /* future binary compatibility */ 242 u_char rtm_type; /* message type */ 243 u_short rtm_index; /* index for associated ifp */ 244 int rtm_flags; /* flags, incl. kern & message, eg DONE */ 245 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 246 pid_t rtm_pid; /* identify sender */ 247 int rtm_seq; /* for sender to identify action */ 248 int rtm_errno; /* why failed */ 249 int rtm_use; /* deprecated use rtm_rmx->rmx_pksent */ 250#define rtm_fmask rtm_use /* bitmask used in RTM_CHANGE message */ 251 u_long rtm_inits; /* which metrics we are initializing */ 252 struct rt_metrics rtm_rmx; /* metrics themselves */ 253}; 254 255struct if_msghdr { 256 u_short ifm_msglen; /* to skip over non-understood messages */ 257 u_char ifm_version; /* future binary compatibility */ 258 u_char ifm_type; /* message type */ 259 int ifm_addrs; /* like rtm_addrs */ 260 int ifm_flags; /* value of if_flags */ 261 u_short ifm_index; /* index for associated ifp */ 262 struct if_data ifm_data;/* statistics and other data about if */ 263}; 264 265struct ifa_msghdr { 266 u_short ifam_msglen; /* to skip over non-understood messages */ 267 u_char ifam_version; /* future binary compatibility */ 268 u_char ifam_type; /* message type */ 269 int ifam_addrs; /* like rtm_addrs */ 270 int ifam_flags; /* value of ifa_flags */ 271 u_short ifam_index; /* index for associated ifp */ 272 int ifam_metric; /* value of ifa_metric */ 273}; 274 275struct if_announcemsghdr { 276 u_short ifan_msglen; /* to skip over non-understood messages */ 277 u_char ifan_version; /* future binary compatibility */ 278 u_char ifan_type; /* message type */ 279 u_short ifan_index; /* index for associated ifp */ 280 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 281 u_short ifan_what; /* what type of announcement */ 282}; 283.Ed 284.Pp 285The 286.Dv RTM_IFINFO 287message uses an 288.Vt if_msghdr 289header, the 290.Dv RTM_NEWADDR 291and 292.Dv RTM_DELADDR 293messages use an 294.Vt ifa_msghdr 295header, 296the 297.Dv RTM_IFANNOUNCE 298message uses an 299.Vt if_announcemsghdr 300header, 301and all other messages use the 302.Vt rt_msghdr 303header. 304.Pp 305The metrics structure is: 306.Bd -literal 307struct rt_metrics { 308 u_long rmx_locks; /* Kernel must leave these values alone */ 309 u_long rmx_mtu; /* MTU for this path */ 310 u_long rmx_hopcount; /* max hops expected */ 311 u_long rmx_expire; /* lifetime for route, e.g. redirect */ 312 u_long rmx_recvpipe; /* inbound delay-bandwidth product */ 313 u_long rmx_sendpipe; /* outbound delay-bandwidth product */ 314 u_long rmx_ssthresh; /* outbound gateway buffer limit */ 315 u_long rmx_rtt; /* estimated round trip time */ 316 u_long rmx_rttvar; /* estimated rtt variance */ 317 u_long rmx_pksent; /* packets sent using this route */ 318}; 319.Ed 320.Pp 321Only 322.Va rmx_mtu , rmx_expire , rmx_pksent , 323and 324.Va rmx_locks 325are used by the kernel routing table. 326All other values will be ignored when inserting them into the kernel and are 327set to zero in routing messages sent by the kernel. 328They are left for compatibility reasons with other systems. 329.Pp 330Flags include the values: 331.Bd -literal 332#define RTF_UP 0x1 /* route usable */ 333#define RTF_GATEWAY 0x2 /* destination is a gateway */ 334#define RTF_HOST 0x4 /* host entry (net otherwise) */ 335#define RTF_REJECT 0x8 /* host or net unreachable */ 336#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 337#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 338#define RTF_DONE 0x40 /* message confirmed */ 339#define RTF_MASK 0x80 /* subnet mask present */ 340#define RTF_CLONING 0x100 /* generate new routes on use */ 341#define RTF_XRESOLVE 0x200 /* external daemon resolves name */ 342#define RTF_LLINFO 0x400 /* generated by ARP or ESIS */ 343#define RTF_STATIC 0x800 /* manually added */ 344#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 345#define RTF_PROTO2 0x4000 /* protocol specific routing flag */ 346#define RTF_PROTO1 0x8000 /* protocol specific routing flag */ 347#define RTF_CLONED 0x10000 /* this is a cloned route */ 348#define RTF_MPATH 0x40000 /* multipath route or operation */ 349.Ed 350.Pp 351Specifiers for metric values in 352.Va rmx_locks 353and 354.Va rtm_inits 355are: 356.Bd -literal 357#define RTV_MTU 0x1 /* init or lock _mtu */ 358#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 359#define RTV_EXPIRE 0x4 /* init or lock _hopcount */ 360#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 361#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 362#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 363#define RTV_RTT 0x40 /* init or lock _rtt */ 364#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 365.Ed 366.Pp 367Only 368.Dv RTV_MTU 369and 370.Dv RTV_EXPIRE 371should be used; all other flags are ignored. 372.Pp 373Specifiers for which addresses are present in the messages are: 374.Bd -literal 375#define RTA_DST 0x1 /* destination sockaddr present */ 376#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 377#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 378#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 379#define RTA_IFP 0x10 /* interface name sockaddr present */ 380#define RTA_IFA 0x20 /* interface addr sockaddr present */ 381#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 382#define RTA_BRD 0x80 /* for NEWADDR, bcast or p-p dest addr */ 383#define RTA_LABEL 0x400 /* route label present */ 384.Ed 385.Sh SEE ALSO 386.Xr netstat 1 , 387.Xr socket 2 , 388.Xr sysctl 3 , 389.Xr mygate 5 , 390.Xr route 8 391.Sh HISTORY 392A 393.Dv PF_ROUTE 394protocol family first appeared in 395.Bx 4.3 Reno . 396