So guys, now let us write a one unified single API which will take the responsibility to send any type
of message from our userspace program to kernel space via netlink socket.
Right.
So the name of the API is send netlink message to kernel.
So as you can see that this API accepts five argument.
The first argument is the file descriptor of the netlink socket which the user space program will create
when the user space program boots up.
Right.
The second argument is the payload which the user space program will send to the kernel space.
The third argument is the size of the payload.
The fourth argument is the type of the netlink message which this user space program is sending to the
kernel space.
So the fourth field is the netlink message type field of the Netlink header.
In our case, this field will be equal to the enumeration, which is great.
Right?
And finally, the fifth field is the flags, which user space program must specify in order to give
directions to the kernel space regarding what to do with the message.
So now let us discuss the implementation of this API in four simple steps.
First, we will take an overview of these four simple steps.
Then in the next lecture video, we will go through the actual implementation of this API.
So as a part of writing this API, the very first step that we need to do is to prepare a message of
type Netlink message header followed by the payload.
Right.
Remember any message that is exchanged between user space and kernel space should be exchanged by using
netlink message header followed by payload.
So now this diagram is not new to you.
We will reformat the message in the following format.
That is netlink message header followed by payload.
Then in the next step we have to use the intermediate data structure called Iok.
Iok stands for input output vector.
So this data structure, as you can see, is a very simple data structure comprises of only two fields.
The first field is a EOF underscore base and it is of data type void star and the second field is of
data type sys underscore tty.
Right.
So what we will going to do is that we will store the address of the netlink message header in this
eof underscore base field and the total length of this message that is starting from the Netlink message
header to the end of the payload, we will going to specify in Ireland.
So basically we have encapsulated the netlink message which we will going to send to the kernel space
indirectly inside the vector data structure.
But the story doesn't ends here.
We will do one more level of wrapping.
Now in the step three, we will going to wrap this vector data structure inside inside another structure
called struct message header.
Right?
So let us call this structure as outer message header.
So in this type of messages, we have to fill these four fields.
So the first field is nothing, but it is the destination address which we will going to specify now
how to specify the destination address that we will going to discuss when we will walk through the code.
The second field contains the size of the destination address.
Again, this We will discuss when we will be discussing the code in the third field.
We have to supply the address of the vector data structure which we have defined in step two.
Right.
And in the fourth field we have to specify how many units of vector data structure we are sending to
the kernel space.
So in this example, we have taken only one vector data structure and therefore you can specify one
in this field.
Now to discuss about this destination address, this destination address is of data type struct soc
address underscore channel.
So this is a socket address data structure which is used for netlink sockets.
And inside this data structure we have to specify only two fields.
The Netlink family, which is the address family for the netlink socket.
So always we have to specify a F underscore netlink and we have to specify the port ID of the destination
process.
Now in this example, because we are communicating from userspace to kernel space for kernel space,
the port ID is always zero, right.
So we have to specify only these two fields in the destination address and watch the data structure
that is used as a destination address.
This data structure is provided by the netlink socket header files.
Now as a final step in step number four, we are good to go to send our user space message to our kernel
space via netlink socket.
The first argument we have to specify the file descriptor of the socket which our user space program
has already created.
The second argument is the address of this outer message header.
And the third argument you can always specify as zero.
So now that we have discussed the high level overview regarding the steps for sending the message from
the user space to kernel space.
Now let us discuss the step by step implementation and discuss the implementation of this API.
We will have to use this API every time.
We will going to send message from our user space program to kernel space.
And you can see that this API is very generic.
We can send any type of message or payload from user space to kernel space using this function.
Right.