1 / 34

TCP/IP Illustrated Volume 2 Chapter 19 Routing Requests and Routing Messages

TCP/IP Illustrated Volume 2 Chapter 19 Routing Requests and Routing Messages. Table of Contents. 19.1 Introduction 19.2 rtalloc and rtalloc1 Functions 19.3 RTFREE Macro and rtfree Function 19.4 rtrequest Function 19.5 rt_setgate Function 19.6 rtinit Function 19.7 rtredirect Function

Télécharger la présentation

TCP/IP Illustrated Volume 2 Chapter 19 Routing Requests and Routing Messages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TCP/IP Illustrated Volume 2Chapter 19 Routing Requests and Routing Messages Internetworking Multimedia

  2. Table of Contents 19.1 Introduction 19.2 rtalloc and rtalloc1 Functions 19.3 RTFREE Macro and rtfree Function 19.4 rtrequest Function 19.5 rt_setgate Function 19.6 rtinit Function 19.7 rtredirect Function 19.8 Routing Message Structure 19.9 rt_missmsg Function 19.10 rt_ifmsg Function 19.11 rt_newaddrmsg Function 19.12 rt_msg1 Function 19.13 rt_msg2 Function 19.14 sysctl_rtable Function 19.15 sysctl_dumpentry Function 19.16 sysctl_iflist Function 19.17 Summary Internetworking Multimedia

  3. 19.17 Summary • routing messages all have the same format • fixed-length structure (different fixed length according to the type) followed by a variable number of socket address structures • length • version • type : 3 (different fixed length) • socket address structures • one messages per read or write between process and kernel • across a routing socket • a super user process reads and writes access to the kernel’s routing table • routed, gated (super user processes) • without a routing socket • any process can read the kernel’s routing table using the sysctl Internetworking Multimedia

  4. 19.1 Introduction • rtalloc • routing table lookup • rtalloc1 • routing table lookup • rtrequest • adds & deletes routing tables • rtinit • when theinterface goes up or down • sysctl • to read entire routing table or • a list of all configured interfaces and interface addresses • routing messages • route command, routed, gated generate to a routing socket, causing the kernel • to add a new route • to delete an existing route • to modify an existing route • kernel generates to any routing socket interested • an interface gone down • a redirect received Internetworking Multimedia

  5. 19.2 rtalloc and rtalloc1 Functions • rtalloc • lookup an entry in the routing table • ro: the pointer to a route structure contained in an Internet PCB (used by UDP & TCP, Chapter 22) • returns if • ro points to an rtentry (ro_rt is nonnull) • rtentry points to an interface • the route is up • otherwise call rtalloc1 Internetworking Multimedia

  6. 19.2 rtalloc and rtalloc1 Functions • rtalloc1 • call rn_match • calls rnh_matchaddr (initialized to rn_match for Internet addresses, Figure 18.17) • search is successful • A routing table exists for the protocol family • rn_match returns a nonnull pointer • the matching radix_node does not have thee RNF_ROOT flag set • 2 leaves end that mark the end of the tree: RNF_ROOT flag set • Search fails • rts_unreach incremented • if rtalloc1(2nd argument report) is nonzero, a routing message (type RTM_MISS) is generated • matching radix_node is stored in rt and newrt • 1st of 2 radix_node structures of rtentry contains the leaf node Internetworking Multimedia

  7. 19.2 rtalloc and rtalloc1 Functions • Create clone entries • nonzero 2nd argument && RTF_CLONING lag set, then calls rtrequest with RTM_REOVE to create a new rtentry (clone) • used by ARP andmulticast addresses • Clone creation fails • newrt is set back to the entry returned by rn_match • increment the reference count • jump to miss for generating RTM_MISS • Check for external resolution • If the newly cloned entry has a RTF_XRESOLVE flag set, jump to miss, to generate an RTM_RESOLVE message (to notify a user process when the route is created) • Increment reference count for normal successful search • search success but RTF_CLONING flag is not set, increment the entry’s reference count: nonnull pointer returns (normal flow) • p604 Figure 19.3 Summary of operation of rtalloc1 • If default router exists, the first two rows (entry not found) are impossible • increment rt_refcnt and call rtrequst with RTM_RESOLVE is OKs Internetworking Multimedia

  8. 19.3 RTFREE Macro and rtfree Function • RTFREE calls rtfree, of the reference count <= 1, otherwise decrements • rtfree releases rtentry when no more references (refer to p719 Figure 22.7) • if RNT_ACTIVE (still part of the routing table) or RNF_ROOT (end marker) is set, then internal error • rttrash • a debugging counter of the number of routing entries not in the routing tree • incremented by rtrequest when it begins deleting a route, decremented in rtfree • normally 0 • Release interface reference • if the reference count not negative, then IFAFREE decrements the reference count for ifaddr and releases it (by ifafree) when it reaches 0 • Releases routing memory • Free (allocated in rt_setgate in a contiguous chunk, for releasing them bya single call) • routing entry key • its gateway • Free rtentry itself Internetworking Multimedia

  9. 19.3 RTFREE Macro and rtfree Function • Routing Table Reference Counts • rt_refcnt differs from others • p560 Figure 18.2: yet the routing table entries without any references are not deleted • in rtfree: a reference count 0 is not deleted unless the entry’s RTF_UP flag is not set (cleared by rtrequest when a route is deleted from the routing tree) • Most routes are used in the following fashion • automatic creation of a route: typical Ethernet interface & point-to-point interface • rtinit calls rtrequest with RTM_ADD, reference count to 1, then rtinit decrements to 0 • manual creation fo a route: route command, routing daemon • route_output calling rtrequest with RTM_ADD, then route_output decrements it to 0 • IP datagram (TCP or UDP) • ip_output calls rtalloc, which calls rtalloc1, rtalloc1 increment the reference count if the route is found • The located route (held route) points to the routing table entry in route structure within a protocol control block • rtentry held by someone else (reference count nonzero) cannot be deleted • RTFREE or rtfree releases a held route • p231 Figure 8.24 (ip_output), Capter22 (PCB) • call rtalloc1 • to look up a route (a route to the destination exists), but the caller doesn’t want to hold the route • increments the counter, then the caller immediately decrements it • delete a route by rtrequest • If the RTF_UP flag of a route is cleared && no one holds the route (its reference count is 0), then rtfree should be called • rtfree considers it an error for the reference count to go below 0, so rtrequest increments it (to 1) and calls rtfree (to 0) Internetworking Multimedia

  10. 19.4 rtrequest Function • p607 Figure 19.6 Summary of functions that call rtrequest • rtalloc1 • rtredirect • rtinit • route_output • switch (req) command • RTM_DELETE • RTM_RESOLVE • RTM_ADD • arguments • dst: a socket address (key) • sa_family from this key selects the routing table • flag • a host route, netmask: null (ignoring any value the caller passed) • a route to a network Internetworking Multimedia

  11. 19.4 rtrequest Function • RTM_DELETE • Deleting from routing table tree • returns a pointer to rtentry • rnh_deladdr (rn_delete, p575 Figure 18.17) • RTF_UP cleared • Remove reference to gateway routing table entry • indirect route through a gateway • RTFREE decrements the rt_refcnt of the gateway’s entry, and deletes it if the count reaches 0 • rt_gwroute is set to null • rt is set back • Call interface request function • if ifa->rtrequest (used by ARP) is defined, call ifa->rtrequest (Chapter 21) • Return pointer or release reference • rttrash global incremented • if rt_nrt is nonnull (caller wants the pointer to the rtentry), then that pointer is returned, but the entry cannot be released: the caller calls rtfree • if rt_nrt is null, then the entry can be released: if the reference count is less than or equal to 0, then increments it and calls rtfree • RTM_RESOLVE • called only from rtalloc1, if RTF_CLONING flag set (p603 Figure 19.2) • ret_nrt points to the entry with the RTF_CLONING flag set • new entry has the same rt_ifa, flag (RTF_CLONING flag cleared), the rt_gateway • if the rt_genmask is null • then a host route: RTF_HOST set • else a network route: the network mask is copied from the rt_genmask • goto makeroute Internetworking Multimedia

  12. 19.4 rtrequest Function • RTM_ADD • Locate corresponding interface • ifa_ifwithroute finds the ifaddr for dst • Allocate memory for routing table entry • R_Malloc • rtenetry: zeroes 2 radix_node for the routing tree and the other routing information • rt_flags are set (RTF_UP set) • Allocate and copy gateway address • rtsetgate allocates for dst and gateway (copied) • sets rt_key, rt_gateway, rt_gwroute • Copy destination address • dst copied to rn_key (a new key) • if network mask is supplied • then rt_maskedcopy logically ANDs dst and netmask (key in the table already ANDed with mask) forming the new key (ndst) : the search key needs to be ANDed • else dst is copied into the rn_key • $ ifconfig le0 inet e104.2252.12.63 0xffffff30 alias • $ netstat (first logically AND the address with the mask) Internetworking Multimedia

  13. 19.4 rtrequest Function • Add entry to routing tree • rnh_addaddr (rn_addroute from Figure p575 18.17) adds rtentry (dst, mask) to the routing table tree • if error (already in), EEXIST • Store interface pointers • increments ifaddr reference count and the pointer to ifaddr • stores the pointers to its ifaddr ifnet • Copy metrics for newly cloned route • RTM_RESOLVE case, copy entire metrics • RTM_ADD case, caller set the metrics • Call interface request function • if an ifa_rtrequest is defined, calls ifa_rtrequest (ARP uses this for RTM_ADD and RTM_RESOLVE, Section 21.13) • line 373 SA ( ) ? • Return pointer and increment reference count • if the caller wants, returns a copy of the pointer to the new structure through ret_nrt • increments rt_refcnt Internetworking Multimedia

  14. 19.4 rtrequest Function • Example: Cloned Routes with Network Masks • RTM_RESOLVE in rtrequest creates the cloned route using rt_genmask • if rt_genmask is nonnull, then the socket address pointed by rt_genmask becomes the network mask of the newly created route • p560 Figure 18.2 cloned routes for the local Ethernet and for multicast addresses • class B network, 128.1, a point-to-point link, subnet mask 0xffffff00 need: • a routing table entry for all possible 254 subnets • a gateway value of a router that knows how to reach the link to the 128.1.0.0 • Assume: • the gateway router 128.1.0.0 (mask 0xffff0000) is not a default router • rach of 254 subnets have different RTTs, MTUs, delays • If a separate routing table entry for each subnet, whenever a connection is closed , TCP would update the routing table with statistics (RTT, RTT variance, Figure 27.3) • by hand using route command: one per subnet • automatically using cloning feature • creates one entry (dst 128.1.0.0, network mask 0xfff0000) • sets RTF_CLONING flag • sets genmask 0xfffff00 • searches for 128.1.2.3: 128.1.2 subnet not exist, 128.1 is the best match • creates a new entry (dst 128.1.2, genmask 0xffffff00) Internetworking Multimedia

  15. 19.5 rt_setgate Function • each leaf in the routing tree • a key (rt_key) (rn_key member of radix_node: (r)->rt_nodes->rn_key): socket addresses • a gateway (rt_gateway): socket addresses • (examples) refer to p560 Figure 18.2 • 127.0.0.1 • gateway member: Internet socket address • by route system call • 140.252.13.33 • gateway member: data-link socket address (Ethernet address) • by ARP • rt_setgate allocates memory for both (Internet socket address and Ethernet address) Internetworking Multimedia

  16. 19.5 rt_setgate Function • Set lengths from socket address structure • dlen: length of the destination socket address structure • glen: length of the gateway socket address structure • ROUNDUP macro rounds up the value up to the next multiple of 4 bytes • Allocate memory • not yet allocated || glen > sa_len, R_Malloc(new) rn_key pointto the new memory • Use memory already allocated for key and gateway • point to the existing memory • Copy newgateway • Bcopy(gate to rt->rt_gateway), sets rt->gateway to the socket address (new+dlen) • Copy key from old memory to new memory • copy dst to the new piece of memory (new), releases the old piece of memory • Release gateway routing pointer • if nonnull rt->rt_gwroute, then RTFREE(rt) • Locate and store new gateway routing pointer • if indirect route, rtalloc1 locates the entry for the new gateway (stored in rt_gwroute) Internetworking Multimedia

  17. 19.6 rtinit Function • 4 calls to rtinit • in_control calls twice when the destination address of a point-to-point interface is set (p172 Figure 6.21) • RTM_DELETE to delete any existing route to the destination • RTM_ADD to add the new route • in_ifinit calls to add a network route for a broadcast network or a host route for a point-to-point link (p171 Figure 6.19) • in-ifscrub calls to delete an existing route for an interface • Commandargument (cmd) is always RTM_ADD or RTM_DELETE • Get deadstination address for route • if a host, the dst is the the other end of the point-to-point link • otherwise, a network route, dst is the unicast address of the interface (ifa_netmask) • Mask network address with network mask • if RTM_DELETE, if a network route with a network mask, then allocate an mbuf, rt_maskedcopy (logically ANDing), dst points to the masked copy (destination looked up) • Search for routing table entry • rtallocc1 searches the routing table for dst • if found, reference count is decremented (since rtalloc1 incremented it) Internetworking Multimedia

  18. 19.6 rtinit Function • process request • rtrequest executes the command (RTM_ADD or RTM_DELETE),releases the allocated mbuf • Generate routing message on successful delete • returns 0 with a pointer (nrt) to the rt entry deleted • generates a routing socket message by rt_newaddmsg • if the reference count <= 0, increments it and releases it by rtfree • Successful add • returns 0 with a pointer (nrt) to the rt entry added • decrements the reference count (since rtrequest incremented it) • Incorrect interface • if the pointer to the interface’s ifaddr in the new routing table entry does not equals the caller’s argument (ifa, located by ia_ifwithroute), then error (ifa_rtrequest with RTM_DELETE, releases ifaddr, sets rt_ifa to the caller’s value, recalls ifa_rtrequest with RTM_ADD) • Generate routing message • generates a routing socket message by rt_newaddrmsg for the RTM_ADD Internetworking Multimedia

  19. 19.7 rtredirect Function • ICMP redirect • imcp_input calls rtredirect, then calls pfctlinput (udp_ctlinput, tcp_ctlinput: goes through all Internet protocol control blocks; inpcbs) (p322 Figure 11.27) • if the PCB is connected to the foreign address (redirected) and holds a route to that foreign address, then releases the route by rtfree • calls rtalloc to look up the foreign address in the routing table, possibly finds a new (redirected) route • rtredirect • validates the information in the redirect • updates the routing table immediately • generates a routing socket message • arguments • dst: the destination IP address not the datagram that caused the redirect (HD in p224 Figure 8.18) • gateway: IP address of the router to use as the new gatewayfield for the desctination (R2 in p224 Figure 8.18) • netmask: null when called by icmp_input • flags: RTF_GATEWAY, RTF_HOST • src: IP address of the router that sent the redirect (R1 in p224 Figure 8.18) • rtp: null when called by icmp_input Internetworking Multimedia

  20. 19.7 rtredirect Function • New gateway must be connected • The new gateway must be directly connected • Locate routing table entry for destination and validate redirect • rtalloc1 searches the routing table for a route to the destination • must be true • the RTF_DONE not set • rtalloc locates a routing table entry for dst • the address of the router that sent the redirect (src) == the current rt_gateway for the destination • the interface for the new gateway (the ifa returned by ifa_ifwithnet) == the current interface for the destination (rt_ifa): the new gateway must be on the same network as the current gateway • the new gateway cannot redirect this host to itself, that is, there cannot exist an attached interface with a unicast address or a broadcast address equal to gateway: ifa_ifwithaddr(gateway) should be null (?) • Must create a new route • if the route not found, if the located routing table entry is the default route, then creates a new entry • A host with access to multiple routers can use this to learn of the correct router when the default is not correct • The test for default route is whether the routing table entry has an associated mask and if the length field of the mask < 2 (the mask for the default route is rn_zeroes, p588 Figure 18.35) Internetworking Multimedia

  21. 19.7 rtredirect Function • Create new host route • If a network route, a host redirect (not a network redirect), then creates a new host route for dst, the existing network route is left alone • Net/3 ICMP considers all received redirects as host redirects (RTF_HOST) • a hostre direct .vs. a network redirect (?) • Create route • rtrequest (setting RTF_GATEWAY && RTF_DYNAMIC (?)) • netmask is null, since the new route is a host route (implied mask of all one bits) • Modify existing host route • when the current route to the destination is already a host route, do not create a new entry, but the existing entry is modified (rt_setgate with RTF_MODIFY) • Ignore if destination is directly connected • If the current route to dst is direct route (RTF_GATEWAY not set), a redirect for a dst already directly connected, then returns EHOSTUNREACH • Return pointer and Increment statistics • a located routing table entry, either returned (nonnull rtp and no errors) or released by rtfree, increments statistics • Generate routing message • clears rt_addrinfo, generates a routing socket message by rt_missmsg, then sends it by raw_input to any process interested in the redirect Internetworking Multimedia

  22. 19.8 Routing Message Structure • a fixed-length header + up to 8 socket address • 3 types of fixed length headers • rt_msghdr • if_meghr • ifa_msghdr • p 571 Figure 18.11 Relationships between the various routing functions • p 570 Figure 18.9 Types of messages exchanged across a routing socket • the first three (the message length, version, and type) are the same • a member (a bit mask) that encodes which of the 8 potential socket address structures follows the header • rtm_addrs • ifm_addrs • ifam_addrs • p622 Figure 19.16 rt_msghr (the most common) • p622 Figure 19.16 if_msghr (RTM_IFINFO) • p622 Figure 19.16 ifa_msghr (RTM_NEWADDR, RTM_DELADDR) • p623 Figure 19.19 Constants used to members of rti_info array • bitmask is only for messages etwen a process and the kernel • array indexes are used within the kernel, refers to its rt_addrinfo • socket addresses always occur in order of increasing array index • (ex) bitmask 0x87 (RTA_DST, RTA_GATEWAY, RTA_NETMASK, RTA_BRD) Internetworking Multimedia

  23. 19.8 Routing Message Structure • p623 Figure 19.20 rt_addrinfo • RTA_GATEWAY bit set in the rti_addrs, then the member rti_info[RTAX_GATEWAY] is a pointer to a gateway socket address (sockaddr_in containing the gateway’s IP) • rosock.c defines: dst, gate, netmask, genmask, ifpaddr, afaaddr, brdaddr #define dst info.rti_info[RTAX_DST] • rt_addrinfo encountered in • rtalloc1 (p 603 Figure 19.2): p624 Figure 19.21 • rtredirect (p 618 Figure 19.14): p624 Figure 19.22 • rti_addrs • within the kernel: not initialized with the bitmask (a null pointer in rti_info arrays is a nonexistent socket address) • messages between a process and the kernel: needs bitmask • p623 Figure 19.23 route_cb • 4 counters of the number of routing sockets for IP, XNS, OSI, any • By keeping track of routing socket listeners, the kernel avoids building a routing message and calling raw_input (to send the message when there aren’t any processes waiting for a message) Internetworking Multimedia

  24. 19.9 rt_missmsg Function • calls rt_msg1 to build a corresponding variable-length message for process in an mbuf chain , an then calls raw_input to pass the mbuf chain to al appropriate routing sockets • if there aren’t any routing socket listeners, then returns • Build message in mbuf chain • p626 Figure 19.25 Mbuf chain built by rt_msg1 corresponding p624 Figure 19.22 • raw_input calls sbappendaddr to append the mbuf chain to a socket’s receive buffer • Finish building message • sets rtm_flag, rtm_errno • copies rti_addrs to rtm_addrs • 0, but rt_msg1 calculates and stores the appropriate bitmask, based on which pointers in rti_info are nonnull • Set protocol of message, call raw_input • raw_input • struct sockaddr route_dst = { 2, PF_ROUTE, }; (where?) • struct sockaddr route_src = { 2, PF_ROUTE, }; (where?) • struct sockproto route_proto = { PF_ROUTE, }; /* p626 Figure 19.26 */ • the family never changes, but the protocol is set each time raw_input is called • 3rd argument of socket system call specifies the protocol • The caller of raw_input sets the sp_protocol member of the route_proto to the protocol of the routing message (sa_family AF_NET) Internetworking Multimedia

  25. 19.10 rt_ifmsg Function • if_up & if_down calls rt_ifmsg to generate a routing socket message • ifconfig can enable (if_up) and disable (if_down) by setting or clearing IFF_UP through SIOCSIFFLAGS command • p123 Figure 4.30 • if no socket listener, returns • Build message in mbuf chain • rt_addrinfo set to 0 • rt_msg2 build an appropriate message in an mbuf chain • all socket address pointers in rt_addrinfo are null, only fixed-length if_msghdr becomes the routing message, no addresses • Finish building message • copies the interface’s index, flags, if_data in the mbuf • set ifm_addrs bitmask 0 • Set protocol of message, call raw_input • protocol of the routing message = 0, because it can apply to all protocol suites • raw_input delivers the message (a message about an interface, not about some specific destination) to the appropriate listeners Internetworking Multimedia

  26. 19.11 rt_newaddrmsg Function • rtinit calls rt_newaddrmsg with RTM_ADD orRTM_DELETE which adds or deletes an address to an interface • if no routing socket listeners, returns • Generate two routing messages • RTM_ADD case: 1st RTM_NEWADDR & 2nd RTM_ADD • RTM_DELETE case: 1st RTM_DELETE & 2nd RTM_DELADDR • RTM_RTM_NEWADDR & RTM_DELADDR built from an ifa_msghdr (for address) • RTM_RTM_ADD & RTM_DELETE built from an rt_msghdr (for inteface) • Generate message with up to four addresses • 4 socket address • ifaddr, ifpaddr, netmask, brdaddr reference elements in the rti_info array • rt_msg1 builds thee appropriate message in an mbuf chains • sa points to the ifa_addr, the family of this socket address becones the protocol of the routing message • creates an rt_msghdr message with the information about the routing entry that was added or deleted • Build message • sets rti_info array: sets (netmask, dst, gate, in rtsock.c) to rt_mask, rt_key, rt_gateway • sets sa • rt_msg1 builds a message in an mbuf chain • fills additional fieldsincluding the bitmask set by rt_msg1 • Set protocol of message, call raw_input • sets the protocol of routing message • raw_input passes the message to the appropriate listeners Internetworking Multimedia

  27. 19.12 rt_msg1 Function • 3 functions (rt_missmsg, rt_ifmsg, rt_newaddrmsg) calls rt_msg1 to build a routing message(p624 Figure 19.21, p 626 Figure 19.25) in mbuf, then calls raw_input to append the mbuf chain to socket’s receive buffer • from rt_msghdr and rt_addrinfo (p624 Figure 19.22) • Get mbuf and determine fixed size of message • m: mbuf • len: the length of the fixed-size message • p570 Figure 18.9, 2 ifa_msghdr(RTM_DELADDR, RTM_NEWADDR), 1 if_msghdr(RTM_IFINFO), 9 rt_msghdr • Verify structure fits in mbuf • mbuf is referenced by pointer • the largest is if_msghdr 84 bytes • MHLEN is 100 • Initialize mbuf packet header and zero structure • initializes and bzeroes rtm • Copy socket address structure into mbuf chain • RTAX_MAX=8 • generate RTA_xxx bitmask (p623 Figure 19.19), • the value 1 is left shifted by the RTAX_xxx index, then logically ORed into the rti_addrs member • ROUNDUP socket address, then copy socket address (all null pointers) into the mbuf by m_copyback • Store length, version, and type • stores the first 3 memners • this code works with all 3 xxxx_msgher s Internetworking Multimedia

  28. 19.13 rt_msg2 Function • build a routing message in a memory buffer (different from rt_msg1) • from route_output to process the RTM_GET command (p651 Figure 20.7 final argument is null) • from sysctl_dumpentry (p641 Figure 19.39) and sysctl_iflist (p643 Figure 19.40) to process a sysctl system (final argument is nonnull) • has an argument to a walkarg • p632 Figure 19.31 • resulting message in memory buffer (pointed by cp) • cp: start of the buffer • The caller sets cp null and claas rt_msg2, then rt_msg2 returns only the length  route_output (p651 Figure 20.7) uses this feature and calls rt_msg2 twice • w • null when called by route_out • nonnull when called by sysctl • Determine size of structure • based on the message type • Copy socket address structures • sets rti_addr bitmask • copies socket address (if cp is nonnull) • updates the length • loops twice for handles walkarg • whenever w is nonnull, cpis nulls Internetworking Multimedia

  29. 19.13 rt_msg2 Function • Check if data to be stored • increments w_neede • if user’s buffer size =500, w_needed = -500 • as long as w_neede is negative, there is room in buffer • if w_where (a pointer to the buffer) null, the process doesn’t want the result, just want the size of the result, no need for rt_msg2 to malloc a buffer, so the process can allocate a buffer and call sysctl again • 5 scenarios: p635 Figure 19.34 • Allocate buffer first time or if message length increases • w_tmemsize is initialized to 0 by sysctl_rtable, if w_rmem allocated, w_tmemsize is determined, if the size of the result increases, allocates a new larger buffer • Go around again and store result • if w_tmem is nonnull, sets cp to point w_tmem, sets second_time 1, and jumps to again • if w_tmem null, sets the pointer to the buffer null • Store length, version, and type • if cp is nonnull, the first 3 elements are stored, returns the length of the message Internetworking Multimedia

  30. 19.14 sysctl_rtable Function • handles the sysctl on a routing socket called by net_sysctl (p571 Figure 18.11)s • Figure 19.35 Example of sysctl with routing table (from arp example) • mib[0], mib[1], mib[2], mib[3]: cause the kernel to call sysctl_rtable • 3 mib[4] operations • NET_RT_DUMP • returns the routing table (family mib[3], 0: all) • 2,3,4 socket address per message • rt_key, r_gateway, rt_netmask, rt_genmask (might be null) • NET_RT_FLAGS • mib[5] RTF_xxx flag (p580 Figure 18.25): returns only the entries with this flag set • NET_RT_IFLIST • return information on all the configured interfaces on the ifnet • if mib[5] specified, returns the only the interfaces with this flag set • returns information • 1 RTM_IFINFO message for each interface • RTM_NEWADDR message for each ifaddr on the interface’s if_addrlist • the amount of information returned can vary, depending the number of routing table entries or the number of interfaces • The 1st sysctl call: sets the 3rd argument of sysctl null, don’t return any data, just return the length (4th argument) • Then call malloc • The 2nd sysctl call: fetches the information • steps through the routing messages in the buffer by rtm_msglen • p637 Figure 19.26 Example of programs that call sysctl to obtain routing table and interface list • arp, route, netstat: fetch entries from the routing table • routed, gated, rwhod: fetches the interface list • routed: mib[3]=AF_INET • gated: mib[3]=0 Internetworking Multimedia

  31. 19.14 sysctl_rtable Function • p638 Figure 19.37 Functions that support the sysctl system call for routing sockets • p639 Figure 19.38 sysctl_rtable function: process sysctl system call requests • Validate arguments • new should be null • namelen must be 3 • name[0]: mib[3] the address family • name[1]: mib[4] the operation (NET_RT_xxx, p636) • name[2], mib[5] the flags • Initialize walkarg structure • sets walkarg 0 • initialize the members • w_where: the pointer in the buffer • w_given: the size of the buffer • w_needed: negative • w_op=name[1]; /* mib[4] the operation (NET_RT_xxx, p636) */ • w_arg=name[2]; /* mib[5] the flags */ • Dump routing table • rnh_walktree walk through the tree • 1st argument: the pointer to the touring table • 2nd arguiment: the pointer to sysctl_dumpentry • 3rd argument: the pointer to anything in the routing table • Return interface list • NET_RT_IFLIST calls sysctl_ifctl (goes through ifnet) • Release buffer • release the beffer allocated by rt_msg2 • Update w_needed • w_needed = 0 –w_given + totalbytes (sum of the message legnth) • w_needed = 0 –w_given + totalbytes (sum of the message legnth) + w_given = totalbytes • Return actual size of message • if where is nonnull • Return estimated size of message • if where is null • 10% fudge size is added Internetworking Multimedia

  32. 19.15 sysctl_dumpentry Function • sysctl_rtable calls rn_walktree which in turn calles sysctl_dumpentry • 1st argument points to a radix_node (pointer to a rtentry) • 2nd argument points to walkarg Iinitialized by sysctl_rtable) • Check flags of routing table entry • skips if rt_flags is not equal to the flag (mib[5], i.e. p636 RTF_LLINFO in ARP) • Form routing message • copies 4 pointers (dst, gate, netmask, genmask) from the routing tanle entry (dst, gate are always nonnull) • rt_msg2 forms an RTM_GET message • Copy message back to process • rt_msg2 allocates a buffer • forms the remainder of the routing message pointed to by w_tmem • copies the message back to the process by copyout Internetworking Multimedia

  33. 19.16 sysctl_iflist Function • sysctl_rtable directly calls sysctl_iflist • for loop iteratates through each interface (ifnet) • while loop proceeds through the limked list of ifaddr • Check interface index • selects if if_index equals to the nonzero flags (mib[5]) • Build routing message • rt_msg2 with RTM_IFINFO returns only the socket address ifpaddr • sets ifpaddr to 0 (since the same info is used for generating the subsequent RTM_NEWADDR) • Copy message back to process • fills the remainder of if_msghdr, copyout it to the process, incremets w_where • Iterate through address structures, check address family • processes each ifaddr for the interface and select only the interface address of given family (if nonzero address family (mib[3]) • Build routing message • rt_msg2 with RTM_NEWADDR returns upto 3 socket addresses (ifaaddr, netmask, brdaddr) • Copy message back to process • fills in the remaindr of ifa_msghdr, copyout to the process, increments w_where • set ifaaddr, netmask, brdaddr to 0 (since the same array is used for the next interface message) Internetworking Multimedia

  34. Utilities for Network Administration • netstat –rn • search /etc/networks • route • in /etc/rc2.d/S69inet • /etc/defaultrouter • ifconfig –a • ping –sv csmail • snoop csblade csmail • snoop –tr –p 520 Internetworking Multimedia

More Related