1.\" $OpenBSD: route.4,v 1.42 2016/08/31 21:32:06 bluhm 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: August 31 2016 $ 34.Dt ROUTE 4 35.Os 36.Sh NAME 37.Nm route 38.Nd kernel packet forwarding database 39.Sh SYNOPSIS 40.In sys/socket.h 41.In net/if.h 42.In 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 an alternate routing table by using the 190.Dv SO_RTABLE 191.Xr setsockopt 2 . 192.Pp 193A process can specify which route message types it's interested in 194by using 195.Dv ROUTE_FILTER(int type) 196and issuing a setsockopt call with the 197.Dv ROUTE_MSGFILTER 198option at the 199.Dv AF_ROUTE 200level. 201For example, to only get interface specific messages: 202.Bd -literal -offset indent 203unsigned int rtfilter; 204 205rtfilter = ROUTE_FILTER(RTM_IFINFO) | 206 ROUTE_FILTER(RTM_IFANNOUNCE); 207 208if (setsockopt(routefd, PF_ROUTE, ROUTE_MSGFILTER, 209 &rtfilter, sizeof(rtfilter)) == -1) 210 err(1, "setsockopt(ROUTE_MSGFILTER)"); 211.Ed 212.Pp 213If a route is in use when it is deleted, 214the routing entry will be marked down and removed from the routing table, 215but the resources associated with it will not 216be reclaimed until all references to it are released. 217User processes can obtain information about the routing 218entry to a specific destination by using a 219.Dv RTM_GET 220message or via the 221.Dv PF_ROUTE 222.Xr sysctl 3 . 223.Pp 224Messages include: 225.Bd -literal 226#define RTM_ADD 0x1 /* Add Route */ 227#define RTM_DELETE 0x2 /* Delete Route */ 228#define RTM_CHANGE 0x3 /* Change Metrics or flags */ 229#define RTM_GET 0x4 /* Report Metrics */ 230#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 231#define RTM_REDIRECT 0x6 /* Told to use different route */ 232#define RTM_MISS 0x7 /* Lookup failed on this address */ 233#define RTM_LOCK 0x8 /* fix specified metrics */ 234#define RTM_RESOLVE 0xb /* req to resolve dst to LL addr */ 235#define RTM_NEWADDR 0xc /* address being added to iface */ 236#define RTM_DELADDR 0xd /* address being removed from iface */ 237#define RTM_IFINFO 0xe /* iface going up/down etc. */ 238#define RTM_IFANNOUNCE 0xf /* iface arrival/departure */ 239#define RTM_DESYNC 0x10 /* route socket buffer overflow */ 240.Ed 241.Pp 242A message header consists of one of the following: 243.Bd -literal 244struct rt_msghdr { 245 u_short rtm_msglen; /* to skip over non-understood messages */ 246 u_char rtm_version; /* future binary compatibility */ 247 u_char rtm_type; /* message type */ 248 u_short rtm_hdrlen; /* sizeof(rt_msghdr) to skip over the header */ 249 u_short rtm_index; /* index for associated ifp */ 250 u_short rtm_tableid; /* routing table id */ 251 u_char rtm_priority; /* routing priority */ 252 u_char rtm_mpls; /* MPLS additional infos */ 253 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 254 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */ 255 int rtm_fmask; /* bitmask used in RTM_CHANGE message */ 256 pid_t rtm_pid; /* identify sender */ 257 int rtm_seq; /* for sender to identify action */ 258 int rtm_errno; /* why failed */ 259 u_int rtm_inits; /* which metrics we are initializing */ 260 struct rt_metrics rtm_rmx; /* metrics themselves */ 261}; 262 263struct if_msghdr { 264 u_short ifm_msglen; /* to skip over non-understood messages */ 265 u_char ifm_version; /* future binary compatibility */ 266 u_char ifm_type; /* message type */ 267 u_short ifm_hdrlen; /* sizeof(if_msghdr) to skip over the header */ 268 u_short ifm_index; /* index for associated ifp */ 269 u_short ifm_tableid; /* routing table id */ 270 u_char ifm_pad1; 271 u_char ifm_pad2; 272 int ifm_addrs; /* like rtm_addrs */ 273 int ifm_flags; /* value of if_flags */ 274 int ifm_xflags; 275 struct if_data ifm_data;/* statistics and other data about if */ 276}; 277 278struct ifa_msghdr { 279 u_short ifam_msglen; /* to skip over non-understood messages */ 280 u_char ifam_version; /* future binary compatibility */ 281 u_char ifam_type; /* message type */ 282 u_short ifam_hdrlen; /* sizeof(ifa_msghdr) to skip over the header */ 283 u_short ifam_index; /* index for associated ifp */ 284 u_short ifam_tableid; /* routing table id */ 285 u_char ifam_pad1; 286 u_char ifam_pad2; 287 int ifam_addrs; /* like rtm_addrs */ 288 int ifam_flags; /* value of ifa_flags */ 289 int ifam_metric; /* value of ifa_metric */ 290}; 291 292struct if_announcemsghdr { 293 u_short ifan_msglen; /* to skip over non-understood messages */ 294 u_char ifan_version; /* future binary compatibility */ 295 u_char ifan_type; /* message type */ 296 u_short ifan_hdrlen; /* sizeof(ifa_msghdr) to skip over the header */ 297 u_short ifan_index; /* index for associated ifp */ 298 u_short ifan_what; /* what type of announcement */ 299 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 300}; 301.Ed 302.Pp 303The 304.Dv RTM_IFINFO 305message uses an 306.Vt if_msghdr 307header, the 308.Dv RTM_NEWADDR 309and 310.Dv RTM_DELADDR 311messages use an 312.Vt ifa_msghdr 313header, 314the 315.Dv RTM_IFANNOUNCE 316message uses an 317.Vt if_announcemsghdr 318header, 319and all other messages use the 320.Vt rt_msghdr 321header. 322.Pp 323The metrics structure is: 324.Bd -literal 325struct rt_metrics { 326 u_int64_t rmx_pksent; /* packets sent using this route */ 327 int64_t rmx_expire; /* lifetime for route, e.g. redirect */ 328 u_int rmx_locks; /* Kernel must leave these values */ 329 u_int rmx_mtu; /* MTU for this path */ 330 u_int rmx_refcnt; /* # references hold */ 331 u_int rmx_hopcount; /* max hops expected */ 332 u_int rmx_recvpipe; /* inbound delay-bandwidth product */ 333 u_int rmx_sendpipe; /* outbound delay-bandwidth product */ 334 u_int rmx_ssthresh; /* outbound gateway buffer limit */ 335 u_int rmx_rtt; /* estimated round trip time */ 336 u_int rmx_rttvar; /* estimated rtt variance */ 337 u_int rmx_pad; 338}; 339.Ed 340.Pp 341Only 342.Va rmx_mtu , rmx_expire , rmx_pksent , 343and 344.Va rmx_locks 345are used by the kernel routing table. 346All other values will be ignored when inserting them into the kernel and are 347set to zero in routing messages sent by the kernel. 348They are left for compatibility reasons with other systems. 349.Pp 350Flags include the values: 351.Bd -literal 352#define RTF_UP 0x1 /* route usable */ 353#define RTF_GATEWAY 0x2 /* destination is a gateway */ 354#define RTF_HOST 0x4 /* host entry (net otherwise) */ 355#define RTF_REJECT 0x8 /* host or net unreachable */ 356#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 357#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 358#define RTF_DONE 0x40 /* message confirmed */ 359#define RTF_CLONING 0x100 /* generate new routes on use */ 360#define RTF_MULTICAST 0x200 /* route associated to a mcast addr. */ 361#define RTF_LLINFO 0x400 /* generated by ARP or NDP */ 362#define RTF_STATIC 0x800 /* manually added */ 363#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 364#define RTF_PROTO3 0x2000 /* protocol specific routing flag */ 365#define RTF_PROTO2 0x4000 /* protocol specific routing flag */ 366#define RTF_PROTO1 0x8000 /* protocol specific routing flag */ 367#define RTF_CLONED 0x10000 /* this is a cloned route */ 368#define RTF_MPATH 0x40000 /* multipath route or operation */ 369#define RTF_MPLS 0x100000 /* MPLS additional infos */ 370#define RTF_LOCAL 0x200000 /* route to a local address */ 371#define RTF_BROADCAST 0x400000 /* route associated to a bcast addr. */ 372#define RTF_CONNECTED 0x800000 /* interface route */ 373.Ed 374.Pp 375The following flags (defined as 376.Dv RTF_FMASK ) 377can be changed by an RTM_CHANGE request: 378.Dv RTF_LLINFO , 379.Dv RTF_PROTO1 , 380.Dv RTF_PROTO2 , 381.Dv RTF_PROTO3 , 382.Dv RTF_BLACKHOLE , 383.Dv RTF_REJECT , 384.Dv RTF_STATIC 385and 386.Dv RTF_MPLS . 387.Pp 388Specifiers for metric values in 389.Va rmx_locks 390and 391.Va rtm_inits 392are: 393.Bd -literal 394#define RTV_MTU 0x1 /* init or lock _mtu */ 395#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 396#define RTV_EXPIRE 0x4 /* init or lock _hopcount */ 397#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 398#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 399#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 400#define RTV_RTT 0x40 /* init or lock _rtt */ 401#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 402.Ed 403.Pp 404Only 405.Dv RTV_MTU 406and 407.Dv RTV_EXPIRE 408should be used; all other flags are ignored. 409.Pp 410Specifiers for which addresses are present in the messages are: 411.Bd -literal 412#define RTA_DST 0x1 /* destination sockaddr present */ 413#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 414#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 415#define RTA_IFP 0x10 /* interface name sockaddr present */ 416#define RTA_IFA 0x20 /* interface addr sockaddr present */ 417#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 418#define RTA_BRD 0x80 /* for NEWADDR, bcast or p-p dest addr */ 419#define RTA_SRC 0x100 /* source sockaddr present */ 420#define RTA_SRCMASK 0x200 /* source netmask present */ 421#define RTA_LABEL 0x400 /* route label present */ 422.Ed 423.Sh SEE ALSO 424.Xr netstat 1 , 425.Xr socket 2 , 426.Xr sysctl 3 , 427.Xr mygate 5 , 428.Xr route 8 , 429.Xr route 9 430.Sh HISTORY 431A 432.Dv PF_ROUTE 433protocol family first appeared in 434.Bx 4.3 Reno . 435