Chapter 12: Networks
void (*input)(struct sk_buff *skb),
struct mutex *cb_mutex, struct module *module);
netdenotes the networking namespace,unitspecifies the protocol family member, andinputis a call-
back function that is activated when data arrives for the socket.^39 If aNULLpointer is passed forinput,the
socket will only be able to transport data from kernel to userland, but not vice versa. The tasks performed
innetlink_kernel_createare summarized by the code flow diagram in Figure 12-36.
netlink_kernel_create
sock_create_lite
_ _netlink_create
netlink_insert
Store input function
Figure 12-36: Code flow diagram for
netlink_kernel_create(multicast
handling is omitted).
- All required data structures need to be allocated, especially an instance ofstruct socket
andstruct netlink_sock.sock_create_litehandles the first requirement, and allocating
netlink_sockis delegated to the auxiliary function__netlink_create. - If an input function is specified, it is stored innetlink_sock->netlink_rcv.
- The newsockinstance is inserted into the netlink hash vianetlink_insert.
Consider, for instance, how the generic object model creates a netlink socket for the uevent mechanism
(refer to Section 7.4.2 on how to use this connection):
lib/kobject_uevent.c
static int __init kobject_uevent_init(void)
{
uevent_sock = netlink_kernel_create(&init_net, NETLINK_KOBJECT_UEVENT,
1, NULL, NULL, THIS_MODULE);
...
return 0;
}
Since uevent messages do not require any input from userland, it is not necessary to specify aninput
function.
After the socket is created, the kernel can constructsk_buffinstances and send them off with either
netlink_unicastornetlink_broadcast.
(^39) There are some more parameters that are not necessary to consider in detail.groupsgives the number of multicast groups, but
I will not discuss the associated possibilities any further. It is also possible to specify a locking mutex (cb_mutex)thatprotectsa
netlink callback, but since I have also omitted to discuss thismechanism, you can likewise ignore this parameter. Usually, aNULL
pointer is specified as mutex argument, and the kernel falls back to a default locking solution.