Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


the protocol family. Netlink usesnetlink_createfor this purpose.^36 The function allocates an instance
ofstruct sockthat is connected with the socket viasocket->sk. However, space is not only reserved
forstruct sockbut for a larger structure that is(simplified) defined as follows:

net/netlink/af_netlink.c
struct netlink_sock {
/* struct sock has to be the first member of netlink_sock */
struct sock sk;
u32 pid;
u32 dst_pid;
...
void (*netlink_rcv)(struct sk_buff *skb);
...
};

In reality, there are many more netlink-specific elements, and the above code is a selection of the most
essential ones.

Thesockinstance is directly embedded intonetlink_sock. Given an instance ofstruct sockfor netlink
sockets, the associated netlink-specific structurenetlink_socketcan be obtained using the auxiliary
functionnlk_sk. The port IDs of both ends of the connection are kept inpidanddst_pid.netlink_rcv
points to a function that is called to receive data.

Message Format


Netlink messages need to obey a certain format as depicted in Figure 12-35.

Message 1 Message 2
struct nlmsg_hdr
Header Payload Header
aligned on
NLMSG_ALIGNTO

Padding

Figure 12-35: Format of a netlink message.

Each message consists of two components: the header and the payload. While the header is required to
be represented bystruct nlmsghdr, the payload can be arbitrary.^37 The required contents of the header
are given by the following data structure:

<netlink.h>
struct nlmsghdr
{
__u32 nlmsg_len; /* Length of message including header */
__u16 nlmsg_type; /* Message content */
__u16 nlmsg_flags; /* Additional flags */

(^36) The protocol family operationsnetlink_family_opspoint to this function. Recall from Section 12.10.4 that the creation func-
tion is automatically called when a new socket is created.
(^37) The kernel offers the standard data structurestruct nlattrif netlink is used to transport attributes. This possibility is
not discussed in detail, but note that all attribute definitions and a useful set of auxiliary helper functions can be found in
include/net/netlink.h.

Free download pdf