next up previous contents
Next: Illustration of the usage Up: Netlink Sockets - Overview Previous: NETLINK_ROUTE Family   Contents

Packaging a netlink packet

Packaging a netlink packet involves close association with the kernel, we have to take a look at the kernel data structures and the corresponding code to understand how they are interpreted, in other words there is a format in which parameters have to be filled in the netlink packet so that the appropriate module in the kernel can perform the necessary action required. In this section let us take a simple example to illustrate how the netlink packet is formed in the user space to add an entry in the kernel routing table.

Now there are two important structures, to be noted.

struct nlmsghdr
{
    __u32       nlmsg_len;  /* Length of message including header */
    __u16       nlmsg_type; /* Message content */
    __u16       nlmsg_flags;    /* Additional flags */
    __u32       nlmsg_seq;  /* Sequence number */
    __u32       nlmsg_pid;  /* Sending process PID */
};

struct rtmsg
{
    unsigned char       rtm_family;
    unsigned char       rtm_dst_len;
    unsigned char       rtm_src_len;
    unsigned char       rtm_tos;

    unsigned char       rtm_table;  /* Routing table id */
    unsigned char       rtm_protocol;   /* Routing protocol; see below  */
    unsigned char       rtm_scope;  /* See below */
    unsigned char       rtm_type;   /* See below    */

    unsigned        rtm_flags;
};

A netlink packet for interacting with the routing table should essentially consist of the following data structure, the members of the structure include the netlink message header, the rtmsg and the parameters to be passed in the buffer. For setting up queues, classes and filters the rtmsg structure is replaced with the traffic control structure tcmsg.

Specific details are covered in the following subsection.

struct 
{        
    struct nlmsghdr     netlink_header;
    struct rtmsg        rt_message;
    char            buffer[1024];
} request;

This is the general structure used for encapsulating route information. The character buffer in the above structure is filled with rtattributes, each consisting of a type/len value followed by the actual value. So, an rtattribute is specified as a tuple <length, type, value>, the length value includes the size of the structure rta and the value for the data.

struct rtattr
{
    unsigned short  rta_len;
    unsigned short  rta_type;
};




next up previous contents
Next: Illustration of the usage Up: Netlink Sockets - Overview Previous: NETLINK_ROUTE Family   Contents
Gowri Dhandapani
1999-10-03