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