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.\" From: @(#)route.4 8.6 (Berkeley) 4/19/94 29.\" $FreeBSD$ 30.\" 31.Dd November 4, 2004 32.Dt ROUTE 4 33.Os 34.Sh NAME 35.Nm route 36.Nd kernel packet forwarding database 37.Sh SYNOPSIS 38.In sys/types.h 39.In sys/time.h 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 "int family" 45.Sh DESCRIPTION 46.Fx 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 re-direct, 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 80a protocol family usually requests the packet be sent to the 81same host specified in the packet. 82Otherwise, the interface 83is requested to address the packet to the gateway listed in the routing entry 84(i.e., the packet is forwarded). 85.Pp 86When routing a packet, 87the kernel will attempt to find 88the most specific route matching the destination. 89(If there are two different mask and value-under-the-mask pairs 90that match, the more specific is the one with more bits in the mask. 91A route to a host is regarded as being supplied with a mask of 92as many ones as there are bits in the destination). 93If no entry is found, the destination is declared to be unreachable, 94and a routing-miss message is generated if there are any 95listeners on the routing control socket described below. 96.Pp 97A wildcard routing entry is specified with a zero 98destination address value, and a mask of all zeroes. 99Wildcard routes will be used 100when the system fails to find other routes matching the 101destination. 102The combination of wildcard 103routes and routing redirects can provide an economical 104mechanism for routing traffic. 105.Pp 106One opens the channel for passing routing control messages 107by using the socket call shown in the synopsis above: 108.Pp 109The 110.Fa family 111parameter may be 112.Dv AF_UNSPEC 113which will provide 114routing information for all address families, or can be restricted 115to a specific address family by specifying which one is desired. 116There can be more than one routing socket open per system. 117.Pp 118Messages are formed by a header followed by a small 119number of sockaddrs (now variable length particularly 120in the 121.Tn ISO 122case), interpreted by position, and delimited 123by the new length entry in the sockaddr. 124An example of a message with four addresses might be an 125.Tn ISO 126redirect: 127Destination, Netmask, Gateway, and Author of the redirect. 128The interpretation of which address are present is given by a 129bit mask within the header, and the sequence is least significant 130to most significant bit within the vector. 131.Pp 132Any messages sent to the kernel are returned, and copies are sent 133to all interested listeners. 134The kernel will provide the process 135ID for the sender, and the sender may use an additional sequence 136field to distinguish between outstanding messages. 137However, message replies may be lost when kernel buffers are exhausted. 138.Pp 139The kernel may reject certain messages, and will indicate this 140by filling in the 141.Ar rtm_errno 142field. 143The routing code returns 144.Er EEXIST 145if 146requested to duplicate an existing entry, 147.Er ESRCH 148if 149requested to delete a non-existent entry, 150or 151.Er ENOBUFS 152if insufficient resources were available 153to install a new route. 154In the current implementation, all routing processes run locally, 155and the values for 156.Ar rtm_errno 157are available through the normal 158.Em errno 159mechanism, even if the routing reply message is lost. 160.Pp 161A process may avoid the expense of reading replies to 162its own messages by issuing a 163.Xr setsockopt 2 164call indicating that the 165.Dv SO_USELOOPBACK 166option 167at the 168.Dv SOL_SOCKET 169level is to be turned off. 170A process may ignore all messages from the routing socket 171by doing a 172.Xr shutdown 2 173system call for further input. 174.Pp 175If a route is in use when it is deleted, 176the routing entry will be marked down and removed from the routing table, 177but the resources associated with it will not 178be reclaimed until all references to it are released. 179User processes can obtain information about the routing 180entry to a specific destination by using a 181.Dv RTM_GET 182message, or by calling 183.Xr sysctl 3 . 184.Pp 185Messages include: 186.Bd -literal 187#define RTM_ADD 0x1 /* Add Route */ 188#define RTM_DELETE 0x2 /* Delete Route */ 189#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 190#define RTM_GET 0x4 /* Report Information */ 191#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */ 192#define RTM_REDIRECT 0x6 /* Told to use different route */ 193#define RTM_MISS 0x7 /* Lookup failed on this address */ 194#define RTM_LOCK 0x8 /* fix specified metrics */ 195#define RTM_OLDADD 0x9 /* caused by SIOCADDRT */ 196#define RTM_OLDDEL 0xa /* caused by SIOCDELRT */ 197#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr - unused */ 198#define RTM_NEWADDR 0xc /* address being added to iface */ 199#define RTM_DELADDR 0xd /* address being removed from iface */ 200#define RTM_IFINFO 0xe /* iface going up/down etc. */ 201#define RTM_NEWMADDR 0xf /* mcast group membership being added to if */ 202#define RTM_DELMADDR 0x10 /* mcast group membership being deleted */ 203#define RTM_IFANNOUNCE 0x11 /* iface arrival/departure */ 204#define RTM_IEEE80211 0x12 /* IEEE80211 wireless event */ 205.Ed 206.Pp 207A message header consists of one of the following: 208.Bd -literal 209struct rt_msghdr { 210 u_short rtm_msglen; /* to skip over non-understood messages */ 211 u_char rtm_version; /* future binary compatibility */ 212 u_char rtm_type; /* message type */ 213 u_short rtm_index; /* index for associated ifp */ 214 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */ 215 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 216 pid_t rtm_pid; /* identify sender */ 217 int rtm_seq; /* for sender to identify action */ 218 int rtm_errno; /* why failed */ 219 int rtm_fmask; /* bitmask used in RTM_CHANGE message */ 220 u_long rtm_inits; /* which metrics we are initializing */ 221 struct rt_metrics rtm_rmx; /* metrics themselves */ 222}; 223 224struct if_msghdr { 225 u_short ifm_msglen; /* to skip over non-understood messages */ 226 u_char ifm_version; /* future binary compatibility */ 227 u_char ifm_type; /* message type */ 228 int ifm_addrs; /* like rtm_addrs */ 229 int ifm_flags; /* value of if_flags */ 230 u_short ifm_index; /* index for associated ifp */ 231 struct if_data ifm_data; /* statistics and other data about if */ 232}; 233 234struct ifa_msghdr { 235 u_short ifam_msglen; /* to skip over non-understood messages */ 236 u_char ifam_version; /* future binary compatibility */ 237 u_char ifam_type; /* message type */ 238 int ifam_addrs; /* like rtm_addrs */ 239 int ifam_flags; /* value of ifa_flags */ 240 u_short ifam_index; /* index for associated ifp */ 241 int ifam_metric; /* value of ifa_metric */ 242}; 243 244struct ifma_msghdr { 245 u_short ifmam_msglen; /* to skip over non-understood messages */ 246 u_char ifmam_version; /* future binary compatibility */ 247 u_char ifmam_type; /* message type */ 248 int ifmam_addrs; /* like rtm_addrs */ 249 int ifmam_flags; /* value of ifa_flags */ 250 u_short ifmam_index; /* index for associated ifp */ 251}; 252 253struct if_announcemsghdr { 254 u_short ifan_msglen; /* to skip over non-understood messages */ 255 u_char ifan_version; /* future binary compatibility */ 256 u_char ifan_type; /* message type */ 257 u_short ifan_index; /* index for associated ifp */ 258 char ifan_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 259 u_short ifan_what; /* what type of announcement */ 260}; 261.Ed 262.Pp 263The 264.Dv RTM_IFINFO 265message uses a 266.Ar if_msghdr 267header, the 268.Dv RTM_NEWADDR 269and 270.Dv RTM_DELADDR 271messages use a 272.Ar ifa_msghdr 273header, the 274.Dv RTM_NEWMADDR 275and 276.Dv RTM_DELMADDR 277messages use a 278.Vt ifma_msghdr 279header, the 280.Dv RTM_IFANNOUNCE 281message uses a 282.Vt if_announcemsghdr 283header, 284and all other messages use the 285.Ar rt_msghdr 286header. 287.Pp 288The 289.Dq Li "struct rt_metrics" 290and the flag bits are as defined in 291.Xr rtentry 9 . 292.Pp 293Specifiers for metric values in rmx_locks and rtm_inits are: 294.Bd -literal 295#define RTV_MTU 0x1 /* init or lock _mtu */ 296#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */ 297#define RTV_EXPIRE 0x4 /* init or lock _expire */ 298#define RTV_RPIPE 0x8 /* init or lock _recvpipe */ 299#define RTV_SPIPE 0x10 /* init or lock _sendpipe */ 300#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */ 301#define RTV_RTT 0x40 /* init or lock _rtt */ 302#define RTV_RTTVAR 0x80 /* init or lock _rttvar */ 303#define RTV_WEIGHT 0x100 /* init or lock _weight */ 304.Ed 305.Pp 306Specifiers for which addresses are present in the messages are: 307.Bd -literal 308#define RTA_DST 0x1 /* destination sockaddr present */ 309#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 310#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 311#define RTA_GENMASK 0x8 /* cloning mask sockaddr present - unused */ 312#define RTA_IFP 0x10 /* interface name sockaddr present */ 313#define RTA_IFA 0x20 /* interface addr sockaddr present */ 314#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 315#define RTA_BRD 0x80 /* for NEWADDR, broadcast or p-p dest addr */ 316.Ed 317.Sh SEE ALSO 318.Xr sysctl 3 , 319.Xr route 8 , 320.Xr rtentry 9 321.Pp 322The constants for the 323.Va rtm_flags 324field are documented in the manual page for the 325.Xr route 8 326utility. 327.Sh HISTORY 328A 329.Dv PF_ROUTE 330protocol family first appeared in 331.Bx 4.3 reno . 332