1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" %sccs.include.redist.man% 5.\" 6.\" @(#)route.4 8.3 (Berkeley) 03/27/94 7.\" 8.Dd 9.Dt ROUTE 4 10.Os 11.Sh NAME 12.Nm route 13.Nd kernel packet forwarding database 14.Sh SYNOPSIS 15.Fd #include <sys/socket.h> 16.Fd #include <net/if.h> 17.Fd #include <net/route.h> 18.Ft int 19.Fn socket PF_ROUTE SOCK_RAW "int family" 20.Sh DESCRIPTION 21.Tn UNIX 22provides some packet routing facilities. 23The kernel maintains a routing information database, which 24is used in selecting the appropriate network interface when 25transmitting packets. 26.Pp 27A user process (or possibly multiple co-operating processes) 28maintains this database by sending messages over a special kind 29of socket. 30This supplants fixed size 31.Xr ioctl 2 Ns 's 32used in earlier releases. 33Routing table changes may only be carried out by the super user. 34.Pp 35The operating system may spontaneously emit routing messages in response 36to external events, such as receipt of a re-direct, or failure to 37locate a suitable route for a request. 38The message types are described in greater detail below. 39.Pp 40Routing database entries come in two flavors: for a specific 41host, or for all hosts on a generic subnetwork (as specified 42by a bit mask and value under the mask. 43The effect of wildcard or default route may be achieved by using 44a mask of all zeros, and there may be hierarchical routes. 45.Pp 46When the system is booted and addresses are assigned 47to the network interfaces, each protocol family 48installs a routing table entry for each interface when it is ready for traffic. 49Normally the protocol specifies the route 50through each interface as a 51.Dq direct 52connection to the destination host 53or network. If the route is direct, the transport layer of 54a protocol family usually requests the packet be sent to the 55same host specified in the packet. Otherwise, the interface 56is requested to address the packet to the gateway listed in the routing entry 57(i.e. the packet is forwarded). 58.Pp 59When routing a packet, 60the kernel will attempt to find 61the most specific route matching the destination. 62(If there are two different mask and value-under-the-mask pairs 63that match, the more specific is the one with more bits in the mask. 64A route to a host is regarded as being supplied with a mask of 65as many ones as there are bits in the destination). 66If no entry is found, the destination is declared to be unreachable, 67and a routing\-miss message is generated if there are any 68listers on the routing control socket described below. 69.Pp 70A wildcard routing entry is specified with a zero 71destination address value, and a mask of all zeroes. 72Wildcard routes will be used 73when the system fails to find other routes matching the 74destination. The combination of wildcard 75routes and routing redirects can provide an economical 76mechanism for routing traffic. 77.Pp 78One opens the channel for passing routing control messasges 79by using the socket call shown in the synopsis above: 80.Pp 81The 82.Fa family 83parameter may be 84.Dv AF_UNSPEC 85which will provide 86routing information for all address families, or can be restricted 87to a specific address family by specifying which one is desired. 88There can be more than one routing socket open per system. 89.Pp 90Messages are formed by a header followed by a small 91number of sockadders (now variable length particularly 92in the 93.Tn ISO 94case), interpreted by position, and delimited 95by the new length entry in the sockaddr. 96An example of a message with four addresses might be an 97.Tn ISO 98redirect: 99Destination, Netmask, Gateway, and Author of the redirect. 100The interpretation of which address are present is given by a 101bit mask within the header, and the sequence is least significant 102to most significant bit within the vector. 103.Pp 104Any messages sent to the kernel are returned, and copies are sent 105to all interested listeners. The kernel will provide the process 106id. for the sender, and the sender may use an additional sequence 107field to distinguish between outstanding messages. However, 108message replies may be lost when kernel buffers are exhausted. 109.Pp 110The kernel may reject certain messages, and will indicate this 111by filling in the 112.Ar rtm_errno 113field. 114The routing code returns 115.Dv EEXIST 116if 117requested to duplicate an existing entry, 118.Dv ESRCH 119if 120requested to delete a non-existent entry, 121or 122.Dv ENOBUFS 123if insufficient resources were available 124to install a new route. 125In the current implementation, all routing process run locally, 126and the values for 127.Ar rtm_errno 128are available through the normal 129.Em errno 130mechanism, even if the routing reply message is lost. 131.Pp 132A process may avoid the expense of reading replies to 133its own messages by issuing a 134.Xr setsockopt 2 135call indicating that the 136.Dv SO_USELOOPBACK 137option 138at the 139.Dv SOL_SOCKET 140level is to be turned off. 141A process may ignore all messages from the routing socket 142by doing a 143.Xr shutdown 2 144system call for further input. 145.Pp 146If a route is in use when it is deleted, 147the routing entry will be marked down and removed from the routing table, 148but the resources associated with it will not 149be reclaimed until all references to it are released. 150User processes can obtain information about the routing 151entry to a specific destination by using a 152.Dv RTM_GET 153message, 154or by reading the 155.Pa /dev/kmem 156device, or by issuing a 157.Xr getkerninfo 2 158system call. 159.Pp 160Messages include: 161.Bd -literal 162#define RTM_ADD 0x1 /* Add Route */ 163#define RTM_DELETE 0x2 /* Delete Route */ 164#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 165#define RTM_GET 0x4 /* Report Information */ 166#define RTM_LOOSING 0x5 /* Kernel Suspects Partitioning */ 167#define RTM_REDIRECT 0x6 /* Told to use different route */ 168#define RTM_MISS 0x7 /* Lookup failed on this address */ 169#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr */ 170.Ed 171.Pp 172A message header consists of: 173.Bd -literal 174struct rt_msghdr { 175 u_short rmt_msglen; /* to skip over non-understood messages */ 176 u_char rtm_version; /* future binary compatability */ 177 u_char rtm_type; /* message type */ 178 u_short rmt_index; /* index for associated ifp */ 179 pid_t rmt_pid; /* identify sender */ 180 int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 181 int rtm_seq; /* for sender to identify action */ 182 int rtm_errno; /* why failed */ 183 int rtm_flags; /* flags, incl kern & message, e.g. DONE */ 184 int rtm_use; /* from rtentry */ 185 u_long rtm_inits; /* which values we are initializing */ 186 struct rt_metrics rtm_rmx; /* metrics themselves */ 187}; 188.Ed 189.Pp 190where 191.Bd -literal 192struct rt_metrics { 193 u_long rmx_locks; /* Kernel must leave these values alone */ 194 u_long rmx_mtu; /* MTU for this path */ 195 u_long rmx_hopcount; /* max hops expected */ 196 u_long rmx_expire; /* lifetime for route, e.g. redirect */ 197 u_long rmx_recvpipe; /* inbound delay-bandwith product */ 198 u_long rmx_sendpipe; /* outbound delay-bandwith product */ 199 u_long rmx_ssthresh; /* outbound gateway buffer limit */ 200 u_long rmx_rtt; /* estimated round trip time */ 201 u_long rmx_rttvar; /* estimated rtt variance */ 202}; 203.Ed 204.Pp 205Flags include the values: 206.Bd -literal 207#define RTF_UP 0x1 /* route useable */ 208#define RTF_GATEWAY 0x2 /* destination is a gateway */ 209#define RTF_HOST 0x4 /* host entry (net otherwise) */ 210#define RTF_REJECT 0x8 /* host or net unreachable */ 211#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 212#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 213#define RTF_DONE 0x40 /* message confirmed */ 214#define RTF_MASK 0x80 /* subnet mask present */ 215#define RTF_CLONING 0x100 /* generate new routes on use */ 216#define RTF_XRESOLVE 0x200 /* external daemon resolves name */ 217#define RTF_LLINFO 0x400 /* generated by ARP or ESIS */ 218#define RTF_STATIC 0x800 /* manually added */ 219#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 220#define RTF_PROTO2 0x4000 /* protocol specific routing flag #1 */ 221#define RTF_PROTO1 0x8000 /* protocol specific routing flag #2 */ 222.Ed 223.Pp 224Specfiers for metric values in rmx_locks and rtm_inits are: 225.Bd -literal 226#define RTV_SSTHRESH 0x1 /* init or lock _ssthresh */ 227#define RTV_RPIPE 0x2 /* init or lock _recvpipe */ 228#define RTV_SPIPE 0x4 /* init or lock _sendpipe */ 229#define RTV_HOPCOUNT 0x8 /* init or lock _hopcount */ 230#define RTV_RTT 0x10 /* init or lock _rtt */ 231#define RTV_RTTVAR 0x20 /* init or lock _rttvar */ 232#define RTV_MTU 0x40 /* init or lock _mtu */ 233.Ed 234.Pp 235Specifiers for which addresses are present in the messages are: 236.Bd -literal 237#define RTA_DST 0x1 /* destination sockaddr present */ 238#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 239#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 240#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 241#define RTA_IFP 0x10 /* interface name sockaddr present */ 242#define RTA_IFA 0x20 /* interface addr sockaddr present */ 243#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 244.Ed 245