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