1*47675Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California. 242485Ssklower.\" All rights reserved. 342485Ssklower.\" 442485Ssklower.\" %sccs.include.redist.man% 542485Ssklower.\" 6*47675Scael.\" @(#)route.4 6.3 (Berkeley) 03/28/91 742485Ssklower.\" 8*47675Scael.Dd 9*47675Scael.Dt ROUTE 4 10*47675Scael.Os 11*47675Scael.Sh NAME 12*47675Scael.Nm route 13*47675Scael.Nd kernel packet forwarding database 14*47675Scael.Sh SYNOPSIS 15*47675Scael.Fd #include <sys/socket.h> 16*47675Scael.Fd #include <net/if.h> 17*47675Scael.Fd #include <net/route.h> 18*47675Scael.Ft int 19*47675Scael.Fn socket PF_ROUTE SOCK_RAW "int family" 20*47675Scael.Sh DESCRIPTION 21*47675Scael.Tn UNIX 2242485Ssklowerprovides some packet routing facilities. 2342485SsklowerThe kernel maintains a routing information database, which 2442485Sskloweris used in selecting the appropriate network interface when 2542485Ssklowertransmitting packets. 26*47675Scael.Pp 2742485SsklowerA user process (or possibly multiple co-operating processes) 2842485Ssklowermaintains this database by sending messages over a special kind 2942485Ssklowerof socket. 3042485SsklowerThis supplants fixed size 31*47675Scael.Xr ioctl 2 Ns 's 3242485Ssklowerused in earlier releases. 3342485SsklowerRouting table changes may only be carried out by the super user. 34*47675Scael.Pp 3542485SsklowerThe operating system may spontaneously emit routing messages in response 3642485Ssklowerto external events, such as recipt of a re-direct, or failure to 3742485Ssklowerlocate a suitable route for a request. 3842485SsklowerThe message types are described in greater detail below. 39*47675Scael.Pp 4042487SsklowerRouting database entries come in two flavors: for a specific 4142487Ssklowerhost, or for all hosts on a generic subnetwork (as specified 4242487Ssklowerby a bit mask and value under the mask. 4342487SsklowerThe effect of wildcard or default route may be achieved by using 4442487Ssklowera mask of all zeros, and there may be hierarchical routes. 45*47675Scael.Pp 4642485SsklowerWhen the system is booted and addresses are assigned 4742485Ssklowerto the network interfaces, each protocol family 4842485Ssklowerinstalls a routing table entry for each interface when it is ready for traffic. 4942485SsklowerNormally the protocol specifies the route 50*47675Scaelthrough each interface as a 51*47675Scael.Dq direct 52*47675Scaelconnection to the destination host 5342485Sskloweror network. If the route is direct, the transport layer of 5442485Ssklowera protocol family usually requests the packet be sent to the 5542485Ssklowersame host specified in the packet. Otherwise, the interface 5642485Sskloweris requested to address the packet to the gateway listed in the routing entry 5742485Ssklower(i.e. the packet is forwarded). 58*47675Scael.Pp 5942485SsklowerWhen routing a packet, 6042485Ssklowerthe kernel will first attempt to find a route to the destination host. 6142485SsklowerFailing that, a search is made for a route to the network of the destination. 62*47675ScaelFinally, any route to a default 63*47675Scael.Pq Dq wildcard 64*47675Scaelgateway is chosen. 6542485SsklowerIf no entry is found, the destination is declared to be unreachable, 6642485Ssklowerand a routing\-miss message is generated if there are any 6742485Ssklowerlisters on the routing control socket described below. 68*47675Scael.Pp 6942485SsklowerA wildcard routing entry is specified with a zero 7042485Ssklowerdestination address value. Wildcard routes are used 7142485Sskloweronly when the system fails to find a route to the 7242485Ssklowerdestination host and network. The combination of wildcard 7342485Ssklowerroutes and routing redirects can provide an economical 7442485Ssklowermechanism for routing traffic. 75*47675Scael.Pp 7642485SsklowerOne opens the channel for passing routing control messasges 7742485Ssklowerby using the socket call shown in the synopsis above: 78*47675Scael.Pp 79*47675ScaelThe 80*47675Scael.Fa family 81*47675Scaelparamter may be 82*47675Scael.Dv AF_UNSPEC 83*47675Scaelwhich will provide 8442485Ssklowerrouting information for all address families, or can be restricted 8542485Ssklowerto a specific address family by specifying which one is desired. 8642485SsklowerThere can be more than one routing socket open per system. 87*47675Scael.Pp 8842485SsklowerMessages are formed by a header followed by a small 8942485Ssklowernumber of sockadders (now variable length particularly 90*47675Scaelin the 91*47675Scael.Tn ISO 92*47675Scaelcase), interpreted by position, and delimited 9342485Ssklowerby the new length entry in the sockaddr. 94*47675ScaelAn example of a message with four addresses might be an 95*47675Scael.Tn ISO 96*47675Scaelredirect: 9742485SsklowerDestination, Netmask, Gateway, and Author of the redirect. 9842485SsklowerThe interpretation of which address are present is given by a 9942485Ssklowerbit mask within the header, and the sequence is least significant 10042485Ssklowerto most significant bit within the vector. 101*47675Scael.Pp 10242485SsklowerAny messages sent to the kernel are returned, and copies are sent 10342485Ssklowerto all interested listeners. The kernel will provide the process 10442485Ssklowerid. for the sender, and the sender may use an additional sequence 10542485Ssklowerfield to distinguish between outstanding messages. However, 10642485Ssklowermessage replies may be lost when kernel buffers are exhausted. 107*47675Scael.Pp 10842485SsklowerThe kernel may reject certain messages, and will indicate this 109*47675Scaelby filling in the 110*47675Scael.Ar rtm_errno 111*47675Scaelfield. 112*47675ScaelThe routing code returns 113*47675Scael.Dv EEXIST 114*47675Scaelif 115*47675Scaelrequested to duplicate an existing entry, 116*47675Scael.Dv ESRCH 117*47675Scaelif 11842485Ssklowerrequested to delete a non-existent entry, 119*47675Scaelor 120*47675Scael.Dv ENOBUFS 121*47675Scaelif insufficient resources were available 12242485Ssklowerto install a new route. 12342485SsklowerIn the current implementation, all routing process run locally, 124*47675Scaeland the values for 125*47675Scael.Ar rtm_errno 126*47675Scaelare available through the normal 127*47675Scael.Em errno 128*47675Scaelmechanism, even if the routing reply message is lost. 129*47675Scael.Pp 13042485SsklowerA process may avoid the expense of reading replies to 13142485Ssklowerits own messages by issuing a 132*47675Scael.Xr setsockopt 2 133*47675Scaelcall indicating that the 134*47675Scael.Dv SO_USELOOPBACK 135*47675Scaeloption 136*47675Scaelat the 137*47675Scael.Dv SOL_SOCKET 138*47675Scaellevel is to be turned off. 13942485SsklowerA process may ignore all messages from the routing socket 14042485Ssklowerby doing a 141*47675Scael.Xr shutdown 2 14242485Ssklowersystem call for further input. 143*47675Scael.Pp 14442485SsklowerIf a route is in use when it is deleted, 14542485Ssklowerthe routing entry will be marked down and removed from the routing table, 14642485Ssklowerbut the resources associated with it will not 14742485Ssklowerbe reclaimed until all references to it are released. 14842485SsklowerUser processes can obtain information about the routing 149*47675Scaelentry to a specific destination by using a 150*47675Scael.Dv RTM_GET 151*47675Scaelmessage, 15242485Sskloweror by reading the 153*47675Scael.Pa /dev/kmem 15442485Ssklowerdevice, or by issuing a 155*47675Scael.Xr getkerninfo 2 15642485Ssklowersystem call. 157*47675Scael.Pp 15842485SsklowerMessages include: 159*47675Scael.Bd -literal 160*47675Scael#define RTM_ADD 0x1 /* Add Route */ 161*47675Scael#define RTM_DELETE 0x2 /* Delete Route */ 162*47675Scael#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 163*47675Scael#define RTM_GET 0x4 /* Report Information */ 164*47675Scael#define RTM_LOOSING 0x5 /* Kernel Suspects Partitioning */ 165*47675Scael#define RTM_REDIRECT 0x6 /* Told to use different route */ 166*47675Scael#define RTM_MISS 0x7 /* Lookup failed on this address */ 167*47675Scael#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr */ 168*47675Scael.Ed 169*47675Scael.Pp 17042485SsklowerA message header consists of: 171*47675Scael.Bd -literal 17242485Ssklowerstruct rt_msghdr { 173*47675Scael u_short rmt_msglen; /* to skip over non-understood messages */ 174*47675Scael u_char rtm_version; /* future binary compatability */ 175*47675Scael u_char rtm_type; /* message type */ 176*47675Scael u_short rmt_index; /* index for associated ifp */ 177*47675Scael pid_t rmt_pid; /* identify sender */ 178*47675Scael int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 179*47675Scael int rtm_seq; /* for sender to identify action */ 180*47675Scael int rtm_errno; /* why failed */ 181*47675Scael int rtm_flags; /* flags, incl kern & message, e.g. DONE */ 182*47675Scael int rtm_use; /* from rtentry */ 183*47675Scael u_long rtm_inits; /* which values we are initializing */ 184*47675Scael struct rt_metrics rtm_rmx; /* metrics themselves */ 18542485Ssklower}; 186*47675Scael.Ed 187*47675Scael.Pp 18842485Ssklowerwhere 189*47675Scael.Bd -literal 19042485Ssklowerstruct rt_metrics { 191*47675Scael u_long rmx_locks; /* Kernel must leave these values alone */ 192*47675Scael u_long rmx_mtu; /* MTU for this path */ 193*47675Scael u_long rmx_hopcount; /* max hops expected */ 194*47675Scael u_long rmx_expire; /* lifetime for route, e.g. redirect */ 195*47675Scael u_long rmx_recvpipe; /* inbound delay-bandwith product */ 196*47675Scael u_long rmx_sendpipe; /* outbound delay-bandwith product */ 197*47675Scael u_long rmx_ssthresh; /* outbound gateway buffer limit */ 198*47675Scael u_long rmx_rtt; /* estimated round trip time */ 199*47675Scael u_long rmx_rttvar; /* estimated rtt variance */ 20042485Ssklower}; 201*47675Scael.Ed 202*47675Scael.Pp 20342485SsklowerFlags include the values: 204*47675Scael.Bd -literal 205*47675Scael#define RTF_UP 0x1 /* route useable */ 206*47675Scael#define RTF_GATEWAY 0x2 /* destination is a gateway */ 207*47675Scael#define RTF_HOST 0x4 /* host entry (net otherwise) */ 208*47675Scael#define RTF_NORMAL 0x8 /* subnet mask is cannonical */ 209*47675Scael#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 210*47675Scael#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 211*47675Scael#define RTF_DONE 0x40 /* message confirmed */ 212*47675Scael#define RTF_MASK 0x80 /* subnet mask present */ 213*47675Scael.Ed 214*47675Scael.Pp 21542485SsklowerSpecfiers for metric values in rmx_locks and rtm_inits are: 216*47675Scael.Bd -literal 217*47675Scael#define RTV_SSTHRESH 0x1 /* init or lock _ssthresh */ 218*47675Scael#define RTV_RPIPE 0x2 /* init or lock _recvpipe */ 219*47675Scael#define RTV_SPIPE 0x4 /* init or lock _sendpipe */ 220*47675Scael#define RTV_HOPCOUNT 0x8 /* init or lock _hopcount */ 221*47675Scael#define RTV_RTT 0x10 /* init or lock _rtt */ 222*47675Scael#define RTV_RTTVAR 0x20 /* init or lock _rttvar */ 223*47675Scael#define RTV_MTU 0x40 /* init or lock _mtu */ 224*47675Scael.Ed 225*47675Scael.Pp 22642485SsklowerSpecifiers for which addresses are present in the messages are: 227*47675Scael.Bd -literal 228*47675Scael#define RTA_DST 0x1 /* destination sockaddr present */ 229*47675Scael#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 230*47675Scael#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 231*47675Scael#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 232*47675Scael#define RTA_IFP 0x10 /* interface name sockaddr present */ 233*47675Scael#define RTA_IFA 0x20 /* interface addr sockaddr present */ 234*47675Scael#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 235*47675Scael.Ed 236*47675Scael.Sh HISTORY 237*47675ScaelThe 238*47675Scael.Nm 239*47675Scaelforwarding database 240*47675Scael.Ud 241