Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


netlink_insertis used to insert new entries into the hash table, whilenetlink_lookupallows for find-
ingsockinstances:

net/netlink/af_netlink.c
static int netlink_insert(struct sock *sk, struct net *net, u32 pid);
static __inline__ struct sock *netlink_lookup(struct net *net, int protocol,
u32 pid);

Note that the hashing data structures are not designed to operate on a per-namespace basis since there is
only one global structure for the whole system. Nevertheless, the code is networking-namespace-aware:
When asockis looked up, the code ensures that the result lives in the proper namespace. Connections
with identical port IDs that originate from different namespaces can exist on the same hash chain simul-
taneously without problems.

Protocol-Specific Operations


Since userland applications use the standard socket interface to deal with netlink connections, the kernel
must provide a set of protocol operations. They are defined as follows:

net/netlink/af_netlink.c
static const struct proto_ops netlink_ops = {
.family = PF_NETLINK,
.owner = THIS_MODULE,
.release = netlink_release,
.bind = netlink_bind,
.connect = netlink_connect,
.socketpair = sock_no_socketpair,
.accept = sock_no_accept,
.getname = netlink_getname,
.poll = datagram_poll,
.ioctl = sock_no_ioctl,
.listen = sock_no_listen,
.shutdown = sock_no_shutdown,
.setsockopt = netlink_setsockopt,
.getsockopt = netlink_getsockopt,
.sendmsg = netlink_sendmsg,
.recvmsg = netlink_recvmsg,
.mmap = sock_no_mmap,
.sendpage = sock_no_sendpage,
};

Programming Interface


The generic socket implementation provides most of the basic functionality required for netlink. Netlink
sockets can be opened both from the kernel and from userland. In the first case,netlink_kernel_create
is employed, while in the second case, the bind method ofnetlink_opsis triggered via the standard
networking paths. For reasons of space, I do not want to discuss the implementation of the userland
protocol handlers in detail, but focus on how connections are initialized from the kernel. The function
requires various parameters:

net/netlink/af_netlink.c
struct sock *
netlink_kernel_create(struct net *net, int unit, unsigned int groups,
Free download pdf