1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd November 4, 2004 29.Dt ROUTE 4 30.Os 31.Sh NAME 32.Nm route 33.Nd kernel packet forwarding database 34.Sh SYNOPSIS 35.In sys/types.h 36.In sys/time.h 37.In sys/socket.h 38.In net/if.h 39.In net/route.h 40.Ft int 41.Fn socket PF_ROUTE SOCK_RAW "int family" 42.Sh DESCRIPTION 43.Fx 44provides some packet routing facilities. 45The kernel maintains a routing information database, which 46is used in selecting the appropriate network interface when 47transmitting packets. 48.Pp 49A user process (or possibly multiple co-operating processes) 50maintains this database by sending messages over a special kind 51of socket. 52This supplants fixed size 53.Xr ioctl 2 Ns 's 54used in earlier releases. 55Routing table changes may only be carried out by the super user. 56.Pp 57The operating system may spontaneously emit routing messages in response 58to external events, such as receipt of a re-direct, or failure to 59locate a suitable route for a request. 60The message types are described in greater detail below. 61.Pp 62Routing database entries come in two flavors: for a specific 63host, or for all hosts on a generic subnetwork (as specified 64by a bit mask and value under the mask. 65The effect of wildcard or default route may be achieved by using 66a mask of all zeros, and there may be hierarchical routes. 67.Pp 68When the system is booted and addresses are assigned 69to the network interfaces, each protocol family 70installs a routing table entry for each interface when it is ready for traffic. 71Normally the protocol specifies the route 72through each interface as a 73.Dq direct 74connection to the destination host 75or network. 76If the route is direct, the transport layer of 77a protocol family usually requests the packet be sent to the 78same host specified in the packet. 79Otherwise, 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. 99The combination of wildcard 100routes and routing redirects can provide an economical 101mechanism for routing traffic. 102.Pp 103One opens the channel for passing routing control messages 104by using the socket call shown in the synopsis above: 105.Pp 106The 107.Fa family 108parameter may be 109.Dv AF_UNSPEC 110which will provide 111routing information for all address families, or can be restricted 112to a specific address family by specifying which one is desired. 113There can be more than one routing socket open per system. 114.Pp 115Messages are formed by a header followed by a small 116number of sockaddrs (now variable length particularly 117in the 118.Tn ISO 119case), interpreted by position, and delimited 120by the new length entry in the sockaddr. 121An example of a message with four addresses might be an 122.Tn ISO 123redirect: 124Destination, Netmask, Gateway, and Author of the redirect. 125The interpretation of which address are present is given by a 126bit mask within the header, and the sequence is least significant 127to most significant bit within the vector. 128.Pp 129Any messages sent to the kernel are returned, and copies are sent 130to all interested listeners. 131The kernel will provide the process 132ID for the sender, and the sender may use an additional sequence 133field to distinguish between outstanding messages. 134However, message replies may be lost when kernel buffers are exhausted. 135.Pp 136The kernel may reject certain messages, and will indicate this 137by filling in the 138.Ar rtm_errno 139field. 140The routing code returns 141.Er EEXIST 142if 143requested to duplicate an existing entry, 144.Er ESRCH 145if 146requested to delete a non-existent entry, 147or 148.Er ENOBUFS 149if insufficient resources were available 150to install a new route. 151In the current implementation, all routing processes run locally, 152and the values for 153.Ar rtm_errno 154are available through the normal 155.Em errno 156mechanism, even if the routing reply message is lost. 157.Pp 158A process may avoid the expense of reading replies to 159its own messages by issuing a 160.Xr setsockopt 2 161call indicating that the 162.Dv SO_USELOOPBACK 163option 164at the 165.Dv SOL_SOCKET 166level is to be turned off. 167A process may ignore all messages from the routing socket 168by doing a 169.Xr shutdown 2 170system call for further input. 171.Pp 172If a route is in use when it is deleted, 173the routing entry will be marked down and removed from the routing table, 174but the resources associated with it will not 175be reclaimed until all references to it are released. 176User processes can obtain information about the routing 177entry to a specific destination by using a 178.Dv RTM_GET 179message, or by calling 180.Xr sysctl 3 . 181.Pp 182Messages include: 183.Bd -literal 184#define RTM_ADD 0x1 /* Add Route */ 185#define RTM_DELETE 0x2 /* Delete Route */ 186#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 187#define RTM_GET 0x4 /* Report Information */ 188#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 189#define RTM_REDIRECT 0x6 /* Told to use different route */ 190#define RTM_MISS 0x7 /* Lookup failed on this address */ 191#define RTM_LOCK 0x8 /* fix specified metrics */ 192#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr - unused */ 193#define RTM_NEWADDR 0xc /* address being added to iface */ 194#define RTM_DELADDR 0xd /* address being removed from iface */ 195#define RTM_IFINFO 0xe /* iface going up/down etc. */ 196#define RTM_NEWMADDR 0xf /* mcast group membership being added to if */ 197#define RTM_DELMADDR 0x10 /* mcast group membership being deleted */ 198#define RTM_IFANNOUNCE 0x11 /* iface arrival/departure */ 199#define RTM_IEEE80211 0x12 /* IEEE80211 wireless event */ 200.Ed 201.Pp 202A message header consists of one of the following: 203.Bd -literal 204struct rt_msghdr { 205 u_short rtm_msglen; /* to skip over non-understood messages */ 206 u_char rtm_version; /* future binary compatibility */ 207 u_char rtm_type; /* message type */ 208 u_short rtm_index; /* index for associated ifp */ 209 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */ 210 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 211 pid_t rtm_pid; /* identify sender */ 212 int rtm_seq; /* for sender to identify action */ 213 int rtm_errno; /* why failed */ 214 int rtm_fmask; /* bitmask used in RTM_CHANGE message */ 215 u_long rtm_inits; /* which metrics we are initializing */ 216 struct rt_metrics rtm_rmx; /* metrics themselves */ 217}; 218 219struct if_msghdr { 220 u_short ifm_msglen; /* to skip over non-understood messages */ 221 u_char ifm_version; /* future binary compatibility */ 222 u_char ifm_type; /* message type */ 223 int ifm_addrs; /* like rtm_addrs */ 224 int ifm_flags; /* value of if_flags */ 225 u_short ifm_index; /* index for associated ifp */ 226 struct if_data ifm_data; /* statistics and other data about if */ 227}; 228 229struct ifa_msghdr { 230 u_short ifam_msglen; /* to skip over non-understood messages */ 231 u_char ifam_version; /* future binary compatibility */ 232 u_char ifam_type; /* message type */ 233 int ifam_addrs; /* like rtm_addrs */ 234 int ifam_flags; /* value of ifa_flags */ 235 u_short ifam_index; /* index for associated ifp */ 236 int ifam_metric; /* value of ifa_metric */ 237}; 238 239struct ifma_msghdr { 240 u_short ifmam_msglen; /* to skip over non-understood messages */ 241 u_char ifmam_version; /* future binary compatibility */ 242 u_char ifmam_type; /* message type */ 243 int ifmam_addrs; /* like rtm_addrs */ 244 int ifmam_flags; /* value of ifa_flags */ 245 u_short ifmam_index; /* index for associated ifp */ 246}; 247 248struct if_announcemsghdr { 249 u_short ifan_msglen; /* to skip over non-understood messages */ 250 u_char ifan_version; /* future binary compatibility */ 251 u_char ifan_type; /* message type */ 252 u_short ifan_index; /* index for associated ifp */ 253 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 254 u_short ifan_what; /* what type of announcement */ 255}; 256.Ed 257.Pp 258The 259.Dv RTM_IFINFO 260message uses a 261.Ar if_msghdr 262header, the 263.Dv RTM_NEWADDR 264and 265.Dv RTM_DELADDR 266messages use a 267.Ar ifa_msghdr 268header, the 269.Dv RTM_NEWMADDR 270and 271.Dv RTM_DELMADDR 272messages use a 273.Vt ifma_msghdr 274header, the 275.Dv RTM_IFANNOUNCE 276message uses a 277.Vt if_announcemsghdr 278header, 279and all other messages use the 280.Ar rt_msghdr 281header. 282.Pp 283The 284.Dq Li "struct rt_metrics" 285and the flag bits are as defined in 286.Xr rtentry 9 . 287.Pp 288Specifiers for metric values in rmx_locks and rtm_inits are: 289.Bd -literal 290#define RTV_MTU 0x1 /* init or lock _mtu */ 291#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 292#define RTV_EXPIRE 0x4 /* init or lock _expire */ 293#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 294#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 295#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 296#define RTV_RTT 0x40 /* init or lock _rtt */ 297#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 298#define RTV_WEIGHT 0x100 /* init or lock _weight */ 299.Ed 300.Pp 301Specifiers for which addresses are present in the messages are: 302.Bd -literal 303#define RTA_DST 0x1 /* destination sockaddr present */ 304#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 305#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 306#define RTA_GENMASK 0x8 /* cloning mask sockaddr present - unused */ 307#define RTA_IFP 0x10 /* interface name sockaddr present */ 308#define RTA_IFA 0x20 /* interface addr sockaddr present */ 309#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 310#define RTA_BRD 0x80 /* for NEWADDR, broadcast or p-p dest addr */ 311.Ed 312.Sh SEE ALSO 313.Xr sysctl 3 , 314.Xr route 8 , 315.Xr rtentry 9 316.Pp 317The constants for the 318.Va rtm_flags 319field are documented in the manual page for the 320.Xr route 8 321utility. 322.Sh HISTORY 323A 324.Dv PF_ROUTE 325protocol family first appeared in 326.Bx 4.3 reno . 327