163008Sbostic.\" Copyright (c) 1990, 1991, 1993 263008Sbostic.\" The Regents of the University of California. All rights reserved. 342485Ssklower.\" 442485Ssklower.\" %sccs.include.redist.man% 542485Ssklower.\" 6*66964Sbostic.\" @(#)route.4 8.6 (Berkeley) 04/19/94 742485Ssklower.\" 847675Scael.Dd 947675Scael.Dt ROUTE 4 1047675Scael.Os 1147675Scael.Sh NAME 1247675Scael.Nm route 1347675Scael.Nd kernel packet forwarding database 1447675Scael.Sh SYNOPSIS 1547675Scael.Fd #include <sys/socket.h> 1647675Scael.Fd #include <net/if.h> 1747675Scael.Fd #include <net/route.h> 1847675Scael.Ft int 1947675Scael.Fn socket PF_ROUTE SOCK_RAW "int family" 2047675Scael.Sh DESCRIPTION 2147675Scael.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. 2647675Scael.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 3147675Scael.Xr ioctl 2 Ns 's 3242485Ssklowerused in earlier releases. 3342485SsklowerRouting table changes may only be carried out by the super user. 3447675Scael.Pp 3542485SsklowerThe operating system may spontaneously emit routing messages in response 3664994Smckusickto external events, such as receipt of a re-direct, or failure to 3742485Ssklowerlocate a suitable route for a request. 3842485SsklowerThe message types are described in greater detail below. 3947675Scael.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. 4547675Scael.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 5047675Scaelthrough each interface as a 5147675Scael.Dq direct 5247675Scaelconnection 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). 5847675Scael.Pp 5942485SsklowerWhen routing a packet, 6066493Ssklowerthe kernel will attempt to find 6166493Ssklowerthe most specific route matching the destination. 6266493Ssklower(If there are two different mask and value-under-the-mask pairs 6366493Ssklowerthat match, the more specific is the one with more bits in the mask. 6466493SsklowerA route to a host is regarded as being supplied with a mask of 6566493Sskloweras many ones as there are bits in the destination). 6642485SsklowerIf no entry is found, the destination is declared to be unreachable, 6742485Ssklowerand a routing\-miss message is generated if there are any 6842485Ssklowerlisters on the routing control socket described below. 6947675Scael.Pp 7042485SsklowerA wildcard routing entry is specified with a zero 7166493Ssklowerdestination address value, and a mask of all zeroes. 7266493SsklowerWildcard routes will be used 7366493Ssklowerwhen the system fails to find other routes matching the 7466493Ssklowerdestination. The combination of wildcard 7542485Ssklowerroutes and routing redirects can provide an economical 7642485Ssklowermechanism for routing traffic. 7747675Scael.Pp 7866963SbosticOne opens the channel for passing routing control messages 7942485Ssklowerby using the socket call shown in the synopsis above: 8047675Scael.Pp 8147675ScaelThe 8247675Scael.Fa family 8364994Smckusickparameter may be 8447675Scael.Dv AF_UNSPEC 8547675Scaelwhich will provide 8642485Ssklowerrouting information for all address families, or can be restricted 8742485Ssklowerto a specific address family by specifying which one is desired. 8842485SsklowerThere can be more than one routing socket open per system. 8947675Scael.Pp 9042485SsklowerMessages are formed by a header followed by a small 9142485Ssklowernumber of sockadders (now variable length particularly 9247675Scaelin the 9347675Scael.Tn ISO 9447675Scaelcase), interpreted by position, and delimited 9542485Ssklowerby the new length entry in the sockaddr. 9647675ScaelAn example of a message with four addresses might be an 9747675Scael.Tn ISO 9847675Scaelredirect: 9942485SsklowerDestination, Netmask, Gateway, and Author of the redirect. 10042485SsklowerThe interpretation of which address are present is given by a 10142485Ssklowerbit mask within the header, and the sequence is least significant 10242485Ssklowerto most significant bit within the vector. 10347675Scael.Pp 10442485SsklowerAny messages sent to the kernel are returned, and copies are sent 10542485Ssklowerto all interested listeners. The kernel will provide the process 10642485Ssklowerid. for the sender, and the sender may use an additional sequence 10742485Ssklowerfield to distinguish between outstanding messages. However, 10842485Ssklowermessage replies may be lost when kernel buffers are exhausted. 10947675Scael.Pp 11042485SsklowerThe kernel may reject certain messages, and will indicate this 11147675Scaelby filling in the 11247675Scael.Ar rtm_errno 11347675Scaelfield. 11447675ScaelThe routing code returns 11547675Scael.Dv EEXIST 11647675Scaelif 11747675Scaelrequested to duplicate an existing entry, 11847675Scael.Dv ESRCH 11947675Scaelif 12042485Ssklowerrequested to delete a non-existent entry, 12147675Scaelor 12247675Scael.Dv ENOBUFS 12347675Scaelif insufficient resources were available 12442485Ssklowerto install a new route. 12542485SsklowerIn the current implementation, all routing process run locally, 12647675Scaeland the values for 12747675Scael.Ar rtm_errno 12847675Scaelare available through the normal 12947675Scael.Em errno 13047675Scaelmechanism, even if the routing reply message is lost. 13147675Scael.Pp 13242485SsklowerA process may avoid the expense of reading replies to 13342485Ssklowerits own messages by issuing a 13447675Scael.Xr setsockopt 2 13547675Scaelcall indicating that the 13647675Scael.Dv SO_USELOOPBACK 13747675Scaeloption 13847675Scaelat the 13947675Scael.Dv SOL_SOCKET 14047675Scaellevel is to be turned off. 14142485SsklowerA process may ignore all messages from the routing socket 14242485Ssklowerby doing a 14347675Scael.Xr shutdown 2 14442485Ssklowersystem call for further input. 14547675Scael.Pp 14642485SsklowerIf a route is in use when it is deleted, 14742485Ssklowerthe routing entry will be marked down and removed from the routing table, 14842485Ssklowerbut the resources associated with it will not 14942485Ssklowerbe reclaimed until all references to it are released. 15042485SsklowerUser processes can obtain information about the routing 15147675Scaelentry to a specific destination by using a 15247675Scael.Dv RTM_GET 15347675Scaelmessage, 15442485Sskloweror by reading the 15547675Scael.Pa /dev/kmem 15642485Ssklowerdevice, or by issuing a 15747675Scael.Xr getkerninfo 2 15842485Ssklowersystem call. 15947675Scael.Pp 16042485SsklowerMessages include: 16147675Scael.Bd -literal 16247675Scael#define RTM_ADD 0x1 /* Add Route */ 16347675Scael#define RTM_DELETE 0x2 /* Delete Route */ 16447675Scael#define RTM_CHANGE 0x3 /* Change Metrics, Flags, or Gateway */ 16547675Scael#define RTM_GET 0x4 /* Report Information */ 16647675Scael#define RTM_LOOSING 0x5 /* Kernel Suspects Partitioning */ 16747675Scael#define RTM_REDIRECT 0x6 /* Told to use different route */ 16847675Scael#define RTM_MISS 0x7 /* Lookup failed on this address */ 16947675Scael#define RTM_RESOLVE 0xb /* request to resolve dst to LL addr */ 17047675Scael.Ed 17147675Scael.Pp 17242485SsklowerA message header consists of: 17347675Scael.Bd -literal 17442485Ssklowerstruct rt_msghdr { 17547675Scael u_short rmt_msglen; /* to skip over non-understood messages */ 17666961Sbostic u_char rtm_version; /* future binary compatibility */ 17747675Scael u_char rtm_type; /* message type */ 17847675Scael u_short rmt_index; /* index for associated ifp */ 17947675Scael pid_t rmt_pid; /* identify sender */ 18047675Scael int rtm_addrs; /* bitmask identifying sockaddrs in msg */ 18147675Scael int rtm_seq; /* for sender to identify action */ 18247675Scael int rtm_errno; /* why failed */ 18347675Scael int rtm_flags; /* flags, incl kern & message, e.g. DONE */ 18447675Scael int rtm_use; /* from rtentry */ 18547675Scael u_long rtm_inits; /* which values we are initializing */ 18647675Scael struct rt_metrics rtm_rmx; /* metrics themselves */ 18742485Ssklower}; 18847675Scael.Ed 18947675Scael.Pp 19042485Ssklowerwhere 19147675Scael.Bd -literal 19242485Ssklowerstruct rt_metrics { 19347675Scael u_long rmx_locks; /* Kernel must leave these values alone */ 19447675Scael u_long rmx_mtu; /* MTU for this path */ 19547675Scael u_long rmx_hopcount; /* max hops expected */ 19647675Scael u_long rmx_expire; /* lifetime for route, e.g. redirect */ 19747675Scael u_long rmx_recvpipe; /* inbound delay-bandwith product */ 19847675Scael u_long rmx_sendpipe; /* outbound delay-bandwith product */ 19947675Scael u_long rmx_ssthresh; /* outbound gateway buffer limit */ 20047675Scael u_long rmx_rtt; /* estimated round trip time */ 20147675Scael u_long rmx_rttvar; /* estimated rtt variance */ 20242485Ssklower}; 20347675Scael.Ed 20447675Scael.Pp 20542485SsklowerFlags include the values: 20647675Scael.Bd -literal 207*66964Sbostic#define RTF_UP 0x1 /* route usable */ 20866493Ssklower#define RTF_GATEWAY 0x2 /* destination is a gateway */ 20966493Ssklower#define RTF_HOST 0x4 /* host entry (net otherwise) */ 21066493Ssklower#define RTF_REJECT 0x8 /* host or net unreachable */ 21166493Ssklower#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */ 21266493Ssklower#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */ 21366493Ssklower#define RTF_DONE 0x40 /* message confirmed */ 21466493Ssklower#define RTF_MASK 0x80 /* subnet mask present */ 21566493Ssklower#define RTF_CLONING 0x100 /* generate new routes on use */ 21666493Ssklower#define RTF_XRESOLVE 0x200 /* external daemon resolves name */ 21766493Ssklower#define RTF_LLINFO 0x400 /* generated by ARP or ESIS */ 21866493Ssklower#define RTF_STATIC 0x800 /* manually added */ 21966493Ssklower#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */ 22066493Ssklower#define RTF_PROTO2 0x4000 /* protocol specific routing flag #1 */ 22166493Ssklower#define RTF_PROTO1 0x8000 /* protocol specific routing flag #2 */ 22247675Scael.Ed 22347675Scael.Pp 224*66964SbosticSpecifiers for metric values in rmx_locks and rtm_inits are: 22547675Scael.Bd -literal 22647675Scael#define RTV_SSTHRESH 0x1 /* init or lock _ssthresh */ 22747675Scael#define RTV_RPIPE 0x2 /* init or lock _recvpipe */ 22847675Scael#define RTV_SPIPE 0x4 /* init or lock _sendpipe */ 22947675Scael#define RTV_HOPCOUNT 0x8 /* init or lock _hopcount */ 23047675Scael#define RTV_RTT 0x10 /* init or lock _rtt */ 23147675Scael#define RTV_RTTVAR 0x20 /* init or lock _rttvar */ 23247675Scael#define RTV_MTU 0x40 /* init or lock _mtu */ 23347675Scael.Ed 23447675Scael.Pp 23542485SsklowerSpecifiers for which addresses are present in the messages are: 23647675Scael.Bd -literal 23747675Scael#define RTA_DST 0x1 /* destination sockaddr present */ 23847675Scael#define RTA_GATEWAY 0x2 /* gateway sockaddr present */ 23947675Scael#define RTA_NETMASK 0x4 /* netmask sockaddr present */ 24047675Scael#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */ 24147675Scael#define RTA_IFP 0x10 /* interface name sockaddr present */ 24247675Scael#define RTA_IFA 0x20 /* interface addr sockaddr present */ 24347675Scael#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */ 24447675Scael.Ed 245