1.\" $OpenBSD: route.4,v 1.24 2008/05/07 12:04:26 claudio 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: May 7 2008 $ 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 182at the 183.Dv SOL_SOCKET 184level is to be turned off. 185A process may ignore all messages from the routing socket 186by doing a 187.Xr shutdown 2 188system call for further input. 189.Pp 190If a route is in use when it is deleted, 191the routing entry will be marked down and removed from the routing table, 192but the resources associated with it will not 193be reclaimed until all references to it are released. 194User processes can obtain information about the routing 195entry to a specific destination by using a 196.Dv RTM_GET 197message or via the 198.Dv PF_ROUTE 199.Xr sysctl 3 . 200.Pp 201Messages include: 202.Bd -literal 203#define RTM_ADD 0x1 /* Add Route */ 204#define RTM_DELETE 0x2 /* Delete Route */ 205#define RTM_CHANGE 0x3 /* Change Metrics or flags */ 206#define RTM_GET 0x4 /* Report Metrics */ 207#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 208#define RTM_REDIRECT 0x6 /* Told to use different route */ 209#define RTM_MISS 0x7 /* Lookup failed on this address */ 210#define RTM_LOCK 0x8 /* fix specified metrics */ 211#define RTM_RESOLVE 0xb /* req to resolve dst to LL addr */ 212#define RTM_NEWADDR 0xc /* address being added to iface */ 213#define RTM_DELADDR 0xd /* address being removed from iface */ 214#define RTM_IFINFO 0xe /* iface going up/down etc. */ 215#define RTM_IFANNOUNCE 0xf /* iface arrival/departure */ 216.Ed 217.Pp 218A message header consists of one of the following: 219.Bd -literal 220struct rt_msghdr { 221 u_short rtm_msglen; /* to skip over non-understood messages */ 222 u_char rtm_version; /* future binary compatibility */ 223 u_char rtm_type; /* message type */ 224 u_short rtm_index; /* index for associated ifp */ 225 int rtm_flags; /* flags, incl. kern & message, eg DONE */ 226 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 227 pid_t rtm_pid; /* identify sender */ 228 int rtm_seq; /* for sender to identify action */ 229 int rtm_errno; /* why failed */ 230 int rtm_use; /* deprecated use rtm_rmx->rmx_pksent */ 231#define rtm_fmask rtm_use /* bitmask used in RTM_CHANGE message */ 232 u_long rtm_inits; /* which metrics we are initializing */ 233 struct rt_metrics rtm_rmx; /* metrics themselves */ 234}; 235 236struct if_msghdr { 237 u_short ifm_msglen; /* to skip over non-understood messages */ 238 u_char ifm_version; /* future binary compatibility */ 239 u_char ifm_type; /* message type */ 240 int ifm_addrs; /* like rtm_addrs */ 241 int ifm_flags; /* value of if_flags */ 242 u_short ifm_index; /* index for associated ifp */ 243 struct if_data ifm_data;/* statistics and other data about if */ 244}; 245 246struct ifa_msghdr { 247 u_short ifam_msglen; /* to skip over non-understood messages */ 248 u_char ifam_version; /* future binary compatibility */ 249 u_char ifam_type; /* message type */ 250 int ifam_addrs; /* like rtm_addrs */ 251 int ifam_flags; /* value of ifa_flags */ 252 u_short ifam_index; /* index for associated ifp */ 253 int ifam_metric; /* value of ifa_metric */ 254}; 255 256struct if_announcemsghdr { 257 u_short ifan_msglen; /* to skip over non-understood messages */ 258 u_char ifan_version; /* future binary compatibility */ 259 u_char ifan_type; /* message type */ 260 u_short ifan_index; /* index for associated ifp */ 261 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 262 u_short ifan_what; /* what type of announcement */ 263}; 264.Ed 265.Pp 266The 267.Dv RTM_IFINFO 268message uses an 269.Vt if_msghdr 270header, the 271.Dv RTM_NEWADDR 272and 273.Dv RTM_DELADDR 274messages use an 275.Vt ifa_msghdr 276header, 277the 278.Dv RTM_IFANNOUNCE 279message uses an 280.Vt if_announcemsghdr 281header, 282and all other messages use the 283.Vt rt_msghdr 284header. 285.Pp 286The metrics structure is: 287.Bd -literal 288struct rt_metrics { 289 u_long rmx_locks; /* Kernel must leave these values alone */ 290 u_long rmx_mtu; /* MTU for this path */ 291 u_long rmx_hopcount; /* max hops expected */ 292 u_long rmx_expire; /* lifetime for route, e.g. redirect */ 293 u_long rmx_recvpipe; /* inbound delay-bandwidth product */ 294 u_long rmx_sendpipe; /* outbound delay-bandwidth product */ 295 u_long rmx_ssthresh; /* outbound gateway buffer limit */ 296 u_long rmx_rtt; /* estimated round trip time */ 297 u_long rmx_rttvar; /* estimated rtt variance */ 298 u_long rmx_pksent; /* packets sent using this route */ 299}; 300.Ed 301.Pp 302Only 303.Va rmx_mtu , rmx_expire , rmx_pksent , 304and 305.Va rmx_locks 306are used by the kernel routing table. 307All other values will be ignored when inserting them into the kernel and are 308set to zero in routing messages sent by the kernel. 309They are left for compatibility reasons with other systems. 310.Pp 311Flags include the values: 312.Bd -literal 313#define RTF_UP 0x1 /* route usable */ 314#define RTF_GATEWAY 0x2 /* destination is a gateway */ 315#define RTF_HOST 0x4 /* host entry (net otherwise) */ 316#define RTF_REJECT 0x8 /* host or net unreachable */ 317#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 318#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 319#define RTF_DONE 0x40 /* message confirmed */ 320#define RTF_MASK 0x80 /* subnet mask present */ 321#define RTF_CLONING 0x100 /* generate new routes on use */ 322#define RTF_XRESOLVE 0x200 /* external daemon resolves name */ 323#define RTF_LLINFO 0x400 /* generated by ARP or ESIS */ 324#define RTF_STATIC 0x800 /* manually added */ 325#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 326#define RTF_PROTO2 0x4000 /* protocol specific routing flag */ 327#define RTF_PROTO1 0x8000 /* protocol specific routing flag */ 328#define RTF_CLONED 0x10000 /* this is a cloned route */ 329#define RTF_MPATH 0x40000 /* multipath route or operation */ 330.Ed 331.Pp 332Specifiers for metric values in 333.Va rmx_locks 334and 335.Va rtm_inits 336are: 337.Bd -literal 338#define RTV_MTU 0x1 /* init or lock _mtu */ 339#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 340#define RTV_EXPIRE 0x4 /* init or lock _hopcount */ 341#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 342#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 343#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 344#define RTV_RTT 0x40 /* init or lock _rtt */ 345#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 346.Ed 347.Pp 348Only 349.Dv RTV_MTU 350and 351.Dv RTV_EXPIRE 352should be used; all other flags are ignored. 353.Pp 354Specifiers for which addresses are present in the messages are: 355.Bd -literal 356#define RTA_DST 0x1 /* destination sockaddr present */ 357#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 358#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 359#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 360#define RTA_IFP 0x10 /* interface name sockaddr present */ 361#define RTA_IFA 0x20 /* interface addr sockaddr present */ 362#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 363#define RTA_BRD 0x80 /* for NEWADDR, bcast or p-p dest addr */ 364#define RTA_LABEL 0x400 /* route label present */ 365.Ed 366.Sh SEE ALSO 367.Xr netstat 1 , 368.Xr socket 2 , 369.Xr sysctl 3 , 370.Xr mygate 5 , 371.Xr route 8 372.Sh HISTORY 373A 374.Dv PF_ROUTE 375protocol family first appeared in 376.Bx 4.3 Reno . 377