1.\" $OpenBSD: route.4,v 1.56 2021/11/10 20:24:22 bket 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: November 10 2021 $ 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 AF_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 95If there are two identical destinations, the route priority acts as a 96tie-breaker. 97If there are multiple routes to the same destination, 98the one with the lowest priority wins. 99The kernel assigns certain default priorities based on the type of 100route, as given in the table below. 101For connected and static routes, this default priority is added to the 102interface's priority. 103.Pp 104A wildcard routing entry is specified with a zero 105destination address value and a mask of all zeroes. 106Wildcard routes will be used 107when the system fails to find other routes matching the 108destination. 109The combination of wildcard routes and routing redirects can provide 110an economical mechanism for routing traffic. 111Routes created by redirects from wildcard routes and other routes 112will be marked 113.Em cloned , 114until their 115.Dq parent 116from which they were created has disappeared. 117.Pp 118Route labels can be attached to routes and may contain arbitrary 119information about the route. 120Labels are sent over the routing socket (see below) as 121.Vt sockaddr_rtlabel 122structures. 123.Ss The Routing Socket 124One opens the channel for passing routing control messages 125by using the 126.Xr socket 2 127call shown in the 128.Sx SYNOPSIS 129above. 130.Pp 131The 132.Fa family 133parameter may be 134.Dv AF_UNSPEC , 135which will provide 136routing information for all address families, or can be restricted 137to a specific address family by specifying which one is desired. 138There can be more than one routing socket open per system. 139.Pp 140Messages are formed by a header followed by a small 141number of 142.Vt sockaddr 143structures (which are variable length), 144interpreted by position, and delimited 145by the length entry in the 146.Vt sockaddr . 147An example of a message with four addresses might be an 148IPv4 route addition: the destination, netmask, gateway, and label, 149since both netmasks and labels are sent over the routing socket as 150.Vt sockaddr 151structures. 152The interpretation of which addresses are present is given by a 153bit mask within the header, and the sequence is least significant 154to most significant bit within the vector. 155.Pp 156Any messages sent to the kernel are returned, and copies are sent 157to all interested listeners. 158The kernel will provide the process ID 159of the sender, and the sender may use an additional sequence 160field to distinguish between outstanding messages. 161However, message replies may be lost when kernel buffers are exhausted. 162.Pp 163The kernel may reject certain messages, and will indicate this 164by filling in the 165.Va rtm_errno 166field. 167The routing code returns 168.Er EEXIST 169if 170requested to duplicate an existing entry, 171.Er ESRCH 172if 173requested to delete a non-existent entry, 174or 175.Er ENOBUFS 176if insufficient resources were available 177to install a new route. 178In the current implementation, all routing processes run locally, 179and the values for 180.Va rtm_errno 181are available through the normal 182.Va errno 183mechanism, even if the routing reply message is lost. 184.Pp 185A process may avoid the expense of reading replies to 186its own messages by issuing a 187.Xr setsockopt 2 188call indicating that the 189.Dv SO_USELOOPBACK 190option at the 191.Dv SOL_SOCKET 192level is to be turned off. 193A process may ignore all messages from the routing socket 194by doing a 195.Xr shutdown 2 196system call for further input. 197.Pp 198There are three filter options that can be used to restrict the received route messages 199to a subset of all the route messages processed by the kernel: 200.Pp 201.Bl -tag -width xxxxxxxx -compact 202.It ROUTE_TABLEFILTER 203.It ROUTE_MSGFILTER 204.It ROUTE_FLAGFILTER 205.El 206.Pp 207A process can specify an alternate routing table by using the 208.Dv ROUTE_TABLEFILTER 209.Xr setsockopt 2 . 210A value of 211.Dv RTABLE_ANY 212specifies all routing tables. 213For example, to receive messages for routing table 5: 214.Bd -literal -offset indent 215unsigned int rdomain = 5; 216 217if (setsockopt(routefd, AF_ROUTE, ROUTE_TABLEFILTER, 218 &rdomain, sizeof(rdomain)) == -1) 219 err(1, "setsockopt(ROUTE_TABLEFILTER)"); 220.Ed 221.Pp 222A process can specify which route message types it's interested in 223by using 224.Dv ROUTE_FILTER(int type) 225and issuing a setsockopt call with the 226.Dv ROUTE_MSGFILTER 227option at the 228.Dv AF_ROUTE 229level. 230For example, to only get interface specific messages: 231.Bd -literal -offset indent 232unsigned int rtfilter; 233 234rtfilter = ROUTE_FILTER(RTM_IFINFO) | 235 ROUTE_FILTER(RTM_IFANNOUNCE); 236 237if (setsockopt(routefd, AF_ROUTE, ROUTE_MSGFILTER, 238 &rtfilter, sizeof(rtfilter)) == -1) 239 err(1, "setsockopt(ROUTE_MSGFILTER)"); 240.Ed 241.Pp 242Similarly, a process can specify that it is only interested in messages 243relating to routes where the priority is no more than a certain value 244by issuing a setsockopt call with the 245.Dv ROUTE_PRIOFILTER 246option. 247For example, to select only local, directly connected and static routes: 248.Bd -literal -offset indent 249unsigned int maxprio = RTP_STATIC; 250 251if (setsockopt(routefd, AF_ROUTE, ROUTE_PRIOFILTER, 252 &maxprio, sizeof(maxprio)) == -1) 253 err(1, "setsockopt(ROUTE_PRIOFILTER)"); 254.Ed 255.Pp 256The 257.Dv ROUTE_FLAGFILTER 258socket option can be used to exclude a subset of 259.Vt rtm_flags 260flags from the received route messages: 261.Bd -literal -offset indent 262int rtfilter = RTF_LLINFO | RTF_BROADCAST; 263 264if (setsockopt(routefd, AF_ROUTE, ROUTE_FLAGFILTER, &rtfilter, 265 sizeof(rtfilter)) == -1) 266 err(1, "setsockopt(ROUTE_FLAGFILTER)"); 267.Ed 268.Pp 269The predefined constants for the routing priorities are: 270.Bd -literal 271#define RTP_NONE 0 /* unset priority use sane default */ 272#define RTP_LOCAL 1 /* local address routes (must be the highest) */ 273#define RTP_CONNECTED 4 /* directly connected routes */ 274#define RTP_STATIC 8 /* static routes base priority */ 275#define RTP_EIGRP 28 /* EIGRP routes */ 276#define RTP_OSPF 32 /* OSPF routes */ 277#define RTP_ISIS 36 /* IS-IS routes */ 278#define RTP_RIP 40 /* RIP routes */ 279#define RTP_BGP 48 /* BGP routes */ 280#define RTP_DEFAULT 56 /* routes that have nothing set */ 281#define RTP_PROPOSAL_STATIC 57 282#define RTP_PROPOSAL_DHCLIENT 58 283#define RTP_PROPOSAL_SLAAC 59 284#define RTP_PROPOSAL_UMB 60 285#define RTP_PROPOSAL_PPP 61 286#define RTP_PROPOSAL_SOLICIT 62 /* request reply of all RTM_PROPOSAL */ 287#define RTP_MAX 63 /* maximum priority */ 288#define RTP_ANY 64 /* any of the above */ 289#define RTP_MASK 0x7f 290#define RTP_DOWN 0x80 /* route/link is down */ 291.Ed 292.Pp 293If a route is in use when it is deleted, 294the routing entry will be marked down and removed from the routing table, 295but the resources associated with it will not 296be reclaimed until all references to it are released. 297User processes can obtain information about the routing 298entry to a specific destination by using a 299.Dv RTM_GET 300message or via the 301.Dv PF_ROUTE 302.Xr sysctl 2 . 303.Pp 304Messages include: 305.Bd -literal 306#define RTM_ADD 0x1 /* Add Route */ 307#define RTM_DELETE 0x2 /* Delete Route */ 308#define RTM_CHANGE 0x3 /* Change Metrics or flags */ 309#define RTM_GET 0x4 /* Report Metrics */ 310#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 311#define RTM_REDIRECT 0x6 /* Told to use different route */ 312#define RTM_MISS 0x7 /* Lookup failed on this address */ 313#define RTM_RESOLVE 0xb /* req to resolve dst to LL addr */ 314#define RTM_NEWADDR 0xc /* address being added to iface */ 315#define RTM_DELADDR 0xd /* address being removed from iface */ 316#define RTM_IFINFO 0xe /* iface going up/down etc. */ 317#define RTM_IFANNOUNCE 0xf /* iface arrival/departure */ 318#define RTM_DESYNC 0x10 /* route socket buffer overflow */ 319#define RTM_INVALIDATE 0x11 /* Invalidate cache of L2 route */ 320.Ed 321.Pp 322A message header consists of one of the following: 323.Bd -literal 324struct rt_msghdr { 325 u_short rtm_msglen; /* to skip over non-understood messages */ 326 u_char rtm_version; /* future binary compatibility */ 327 u_char rtm_type; /* message type */ 328 u_short rtm_hdrlen; /* sizeof(rt_msghdr) to skip over the header */ 329 u_short rtm_index; /* index for associated ifp */ 330 u_short rtm_tableid; /* routing table id */ 331 u_char rtm_priority; /* routing priority */ 332 u_char rtm_mpls; /* MPLS additional infos */ 333 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 334 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */ 335 int rtm_fmask; /* bitmask used in RTM_CHANGE message */ 336 pid_t rtm_pid; /* identify sender */ 337 int rtm_seq; /* for sender to identify action */ 338 int rtm_errno; /* why failed */ 339 u_int rtm_inits; /* which metrics we are initializing */ 340 struct rt_metrics rtm_rmx; /* metrics themselves */ 341}; 342 343struct if_msghdr { 344 u_short ifm_msglen; /* to skip over non-understood messages */ 345 u_char ifm_version; /* future binary compatibility */ 346 u_char ifm_type; /* message type */ 347 u_short ifm_hdrlen; /* sizeof(if_msghdr) to skip over the header */ 348 u_short ifm_index; /* index for associated ifp */ 349 u_short ifm_tableid; /* routing table id */ 350 u_char ifm_pad1; 351 u_char ifm_pad2; 352 int ifm_addrs; /* like rtm_addrs */ 353 int ifm_flags; /* value of if_flags */ 354 int ifm_xflags; 355 struct if_data ifm_data;/* statistics and other data about if */ 356}; 357 358struct ifa_msghdr { 359 u_short ifam_msglen; /* to skip over non-understood messages */ 360 u_char ifam_version; /* future binary compatibility */ 361 u_char ifam_type; /* message type */ 362 u_short ifam_hdrlen; /* sizeof(ifa_msghdr) to skip over the header */ 363 u_short ifam_index; /* index for associated ifp */ 364 u_short ifam_tableid; /* routing table id */ 365 u_char ifam_pad1; 366 u_char ifam_pad2; 367 int ifam_addrs; /* like rtm_addrs */ 368 int ifam_flags; /* value of ifa_flags */ 369 int ifam_metric; /* value of ifa_metric */ 370}; 371 372struct if_announcemsghdr { 373 u_short ifan_msglen; /* to skip over non-understood messages */ 374 u_char ifan_version; /* future binary compatibility */ 375 u_char ifan_type; /* message type */ 376 u_short ifan_hdrlen; /* sizeof(ifa_msghdr) to skip over the header */ 377 u_short ifan_index; /* index for associated ifp */ 378 u_short ifan_what; /* what type of announcement */ 379 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 380}; 381.Ed 382.Pp 383The 384.Dv RTM_IFINFO 385message uses an 386.Vt if_msghdr 387header, the 388.Dv RTM_NEWADDR 389and 390.Dv RTM_DELADDR 391messages use an 392.Vt ifa_msghdr 393header, 394the 395.Dv RTM_IFANNOUNCE 396message uses an 397.Vt if_announcemsghdr 398header, 399.Dv RTM_INVALIDATE 400is used only internally in the kernel and should never appear in a route 401message, 402and all other messages use the 403.Vt rt_msghdr 404header. 405.Pp 406The metrics structure is: 407.Bd -literal 408struct rt_metrics { 409 u_int64_t rmx_pksent; /* packets sent using this route */ 410 int64_t rmx_expire; /* lifetime for route, e.g. redirect */ 411 u_int rmx_locks; /* Kernel must leave these values */ 412 u_int rmx_mtu; /* MTU for this path */ 413 u_int rmx_refcnt; /* # references hold */ 414 u_int rmx_hopcount; /* max hops expected */ 415 u_int rmx_recvpipe; /* inbound delay-bandwidth product */ 416 u_int rmx_sendpipe; /* outbound delay-bandwidth product */ 417 u_int rmx_ssthresh; /* outbound gateway buffer limit */ 418 u_int rmx_rtt; /* estimated round trip time */ 419 u_int rmx_rttvar; /* estimated rtt variance */ 420 u_int rmx_pad; 421}; 422.Ed 423.Pp 424Only 425.Va rmx_mtu , rmx_expire , rmx_pksent , 426and 427.Va rmx_locks 428are used by the kernel routing table. 429All other values will be ignored when inserting them into the kernel and are 430set to zero in routing messages sent by the kernel. 431They are left for compatibility reasons with other systems. 432.Pp 433Flags include the values: 434.Bd -literal 435#define RTF_UP 0x1 /* route usable */ 436#define RTF_GATEWAY 0x2 /* destination is a gateway */ 437#define RTF_HOST 0x4 /* host entry (net otherwise) */ 438#define RTF_REJECT 0x8 /* host or net unreachable */ 439#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 440#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 441#define RTF_DONE 0x40 /* message confirmed */ 442#define RTF_CLONING 0x100 /* generate new routes on use */ 443#define RTF_MULTICAST 0x200 /* route associated to a mcast addr. */ 444#define RTF_LLINFO 0x400 /* generated by ARP or NDP */ 445#define RTF_STATIC 0x800 /* manually added */ 446#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 447#define RTF_PROTO3 0x2000 /* protocol specific routing flag */ 448#define RTF_PROTO2 0x4000 /* protocol specific routing flag */ 449#define RTF_PROTO1 0x8000 /* protocol specific routing flag */ 450#define RTF_CLONED 0x10000 /* this is a cloned route */ 451#define RTF_MPATH 0x40000 /* multipath route or operation */ 452#define RTF_MPLS 0x100000 /* MPLS additional infos */ 453#define RTF_LOCAL 0x200000 /* route to a local address */ 454#define RTF_BROADCAST 0x400000 /* route associated to a bcast addr. */ 455#define RTF_CONNECTED 0x800000 /* interface route */ 456.Ed 457.Pp 458The following flags (defined as 459.Dv RTF_FMASK ) 460can be changed by an RTM_CHANGE request: 461.Dv RTF_LLINFO , 462.Dv RTF_PROTO1 , 463.Dv RTF_PROTO2 , 464.Dv RTF_PROTO3 , 465.Dv RTF_BLACKHOLE , 466.Dv RTF_REJECT , 467.Dv RTF_STATIC 468and 469.Dv RTF_MPLS . 470.Pp 471Specifiers for metric values in 472.Va rmx_locks 473and 474.Va rtm_inits 475are: 476.Bd -literal 477#define RTV_MTU 0x1 /* init or lock _mtu */ 478#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 479#define RTV_EXPIRE 0x4 /* init or lock _expire */ 480#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 481#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 482#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 483#define RTV_RTT 0x40 /* init or lock _rtt */ 484#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 485.Ed 486.Pp 487Only 488.Dv RTV_MTU 489and 490.Dv RTV_EXPIRE 491should be used; all other flags are ignored. 492.Pp 493Specifiers for which addresses are present in the messages are: 494.Bd -literal 495#define RTA_DST 0x1 /* destination sockaddr present */ 496#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 497#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 498#define RTA_IFP 0x10 /* interface name sockaddr present */ 499#define RTA_IFA 0x20 /* interface addr sockaddr present */ 500#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 501#define RTA_BRD 0x80 /* for NEWADDR, bcast or p-p dest addr */ 502#define RTA_SRC 0x100 /* source sockaddr present */ 503#define RTA_SRCMASK 0x200 /* source netmask present */ 504#define RTA_LABEL 0x400 /* route label present */ 505.Ed 506.Sh SEE ALSO 507.Xr netstat 1 , 508.Xr socket 2 , 509.Xr sysctl 2 , 510.Xr rtable 4 , 511.Xr mygate 5 , 512.Xr route 8 , 513.Xr route 9 514.Sh HISTORY 515A 516.Dv PF_ROUTE 517protocol family first appeared in 518.Bx 4.3 Reno . 519