Professional Documents
Culture Documents
A Ghafoor
Contents
• Introduction
• Routing table
• Operation on routing sockets
• Datalink socket address structure
• Structures for routing sockets
• Reading and writing
• sysctl operation
• Interface name and index function
Introduction
• Access the Unix routing table within the kernel
– ioctl command (SIOCADDRT, SIODELRT) is used to
add and delete an entry to/from routing table but entire
routing table can’t be accessed
– netstat: read the kernel memory to obtain the contents
of the routing table
• Routing daemons (gated)
– Need to monitor ICMP redirect messages that are
received by the kernel (RAW_SOCK)
– The raw sockets created with AF_ROUTE domain
sockets can cleaned up the interface to the kernel’s
routing system
Routing table
Operation on routing socket
S – A process can send a message to the kernel by
U
P writing to a routing socket(addition and
E deletion of routes)
R
U – A process can read a message from the kernel
S on a routing socket. In this way a process can
E
R perform route resolution and receives ICMP
notifications.
P
R
O
– A process can use the sysctl function to either
C
E
dump the routing table or to list all the
S
S
configured interface
Datalink socket address structure
• Returned by routing socket <net/if_dl.h>
struct sockaddr_dl
{
uint8_t sdl_len;
sa_family_t sdl_family; /*AF_LINK*/
uint16_t sdl_index; /*system assigned index*/
uint8_t sdl_type; /*IFT_ETHER*/
uint8_t sdl_nlen; /*name length*/
uint8_t sdl_alen; /*link-layer address length*/
uint8_t sdl_slen; /* link-layer selector length */
char sdl_data[12]; /*minimum work area, name
and link-layer address*/
};
Datalink socket address structure
rt_msghdr{} rt_msghdr{}
rtm_type = rtm_type =
RTM_GET RTM_GET
destination destination
socket socket RTA_DST
address address
structure
structure
RTA_DST gateway
socket RTA_GAT
address EWAY
structure
netmask
socket RTA_NET
address MASK
structure
genmask
socket RTA_GE
address NMASK
structure
Reading routing table (RTM_GET)
#include "unproute.h"
#define BUFLEN (sizeof(struct rt_msghdr) + 512)
/* 8 * sizeof(struct sockaddr_in6) = 192 */
#define SEQ 9999
int main(int argc, char **argv)
{
int sockfd;
char *buf;
pid_t pid;
ssize_t n;
struct rt_msghdr *rtm;
struct sockaddr *sa, *rti_info[RTAX_MAX];
struct sockaddr_in *sin;
if (argc != 2) err_quit("usage: getrt <IPaddress>");
sockfd = socket(AF_ROUTE, SOCK_RAW, 0); /* need superuser privileges */
buf = calloc(1, BUFLEN); /* and initialized to 0 */
rtm = (struct rt_msghdr *) buf;
rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
rtm->rtm_version = RTM_VERSION;
rtm->rtm_type = RTM_GET;
rtm->rtm_addrs = RTA_DST;
rtm->rtm_pid = pid = getpid();
rtm->rtm_seq = SEQ;
rt_msghdr{}
rtm_type =
RTM_GET
rti_info[RTAX_DST] destination
socket RTA_DST
rti_info[RTAX_GATEWAY] address
rti_info[RTAX_NETMASK] structure
gateway
rti_info[RTAX_GENMASK] socket RTA_GAT
address EWAY
rti_info[RTAX_IFP] NULL
structure
rti_info[RTAX_IFA] NULL netmask
socket RTA_NET
rti_info[RTAX_AUTHOR] NULL
address MASK
rti_info[RTAX_BRD] NULL structure
genmask
socket RTA_GE
address NMASK
structure
sysctl operation
• Any<sys/param.h>
#include process can examine both the routing table
and<sys/sysctl.h>
#include the interface list.
• Any user can view the routing table and
interface
int sysctl(int list. u_int namelen, void *oldp, size_t
*name,
*oldlenp, void *newp, size_t newlen);
0 CTL_NET
CTL_NET CTL_NET
1 AF_ROUTE
AF_ROUTE AF_ROUTE
2 0
0 0
3 AF_INET
AF_INET AF_INET
5 0 RTF_LLINFO 0
Three operations are supported by
name[4]
• NET_RT_DUMP:
– returns the routing table for the address family specified in n
ame[3]
– If address family is 0, routing table for all address families a
re returned
– Return: destination, gateway, network mask, and cloning m
ask
• NET_RT_FLAGS:
– Only the routing table with RTF_xxx flag values that contai
n the flags specified by name[5]
– All ARP cache entries in routing table have the RTF_FLAG
bit set
Information returned for sysctl,CTL_NET,NET_RT_IFLIST
command buffer returned
by kernel
netmask
socket
address
One per address:
structure
unicast addr configured for the
socket interface
address
structure
broadcast addr
socket
address
structure
UDP Checksum
#include "unproute.h"
#include <netinet/udp.h>
#include <netinet/ip_var.h>
#include <netinet/udp_var.h> /* for
UDPCTL_xxx constants */
#include <net/if.h>
unsigned int if_nametoindex(const char *ifname);
returns:positive interface index if OK, 0 on error
char *if_indextoname(unsigned int ifindex, char *ifname);
returns:pointer to interface name if OK, NULL on error
struct if_nameindex *if_nameindex(void);
returns:nonnull pointer if OK, NULL on error
void if_freenameindex(struct if_nameindex *ptr);
Struct if_nameindex {
unsigned int if_index;/*1, 2,…...*/
char *if_name;/*null terminated name*/
}
Home Assignment