But.
So, guys, I am inside the directory.
Greetings.
And inside this directory I will going to create a new file called greet Netlink lcmc.
So this file will going to represent our linux kernel module.
Right?
So let's begin with absolute scratch.
So in the very first step, we have already learned that we need to write in it and clean up functions.
So in this Linux kernel module, we will going to do nothing different.
We will again going to write the init function which is netlink greetings underscore init followed by
an exit function or clean up function.
Of course you can choose the name anything you want.
And finally you would want to register it with the GPL license.
And of course we need to implement this in IT and exit functions also.
So just going by what we studied in the previous example, when we demonstrated the Linux kernel module,
we will simply print some message which will tell us that our Linux kernel module has been successfully
inserted, right.
And simply we will going to return zero zero means that the insertion of our Linux kernel module is
successful.
Right?
Similarly, we will going to code our exit function and the return type of the exit function is wide.
And here also we will going to simply print some message which will tell us that our Linux kernel module
has been successfully removed from the Linux kernel right now.
As the next step, we need to define somehow that when the user space application will send data to
the kernel space, how our Linux kernel module will going to receive that data, right?
So for that we will going to define a variable of type netlink kernel cfg.
Right.
This data structure is provided by Linux kernel.
And inside this data structure there is a member called input.
So this input member is actually a pointer to the function.
Right?
So we will going to define our own function shortly.
We will going to see what will be the signature of this function.
But for now we will going to assign the pointer to the function to this member of this structure.
Right now, let us see the definition of this structure.
So now you already know how to browse the Linux kernel code.
So in the file Linux slash netlink dot h, you can see that Linux kernel defines the data structure
called Netlink kernel cfg.
It is the same data structure that we are using.
Right?
And inside this data structures there is a member called input.
So you can see that input is actually a pointer to the function which accepts an argument of type struct
as buff.
Right?
So what we have done is that in our Linux module we have taken a function called netlink receive message
function, which we will going to define shortly.
And we have assigned the pointer of this function to this input member of this data structure right
now.
What is the purpose of this function?
This function is meant to receive the data coming from the user space.
So whenever our Linux kernel module, we are going to receive data from the user space, it is this
function which will going to be invoked in the kernel space.
So by now we know what should be the prototype of this function.
The prototype of this function should be same as the data type of this input pointer to the function
defined in this data structure.
Netlink kernel cfg.
Right.
So basically this netlink kernel cfg data structure allows us to define various parameters of our netlink
communication in the kernel space.
For now we are using this data structure to define only the function which we are going to receive the
data from the user space.
So you can see as soon as we will going to receive the data from the user space, it is this function
which will going to invoke.
Now the next thing that we need to do is to tell the Linux kernel that hey, Linux kernel, whenever
we receive the data from the user space, please invoke our function Netlink receive message function.
So for that we will going to do something in our netlink Greetings init module.
Right?
So as soon as we have inserted our Linux kernel module, our Linux kernel module must create a netlink
socket.
That Netlink socket defines that our Linux kernel module will going to receive what type of data from
the user space.
Right.
So netlink socket is created using an netlink kernel create.
Right.
The first argument of this API is actually a global variable which is provided by the kernel.
This global variable is init net.
Right?
And you can see that we are simply passing the address of this global variable.
So basically init net is nothing, but it actually represents the complete networking subsystem which
is running in the kernel space.
The second argument to this API is our netlink protocol number.
Now, we have already discussed that because we are writing our own new Linux kernel subsystem.
Therefore, in order to set up the netlink based communication with the user space, we need to have
some protocol number.
Right?
So this is Netlink test.
Protocol is a protocol number which we will going to have to define it so we can define its value as
31, right?
So it's an unused value because all other values are being used by some other netlink protocols.
And the third argument of this API is the configuration parameter which we have defined above.
That is netlink kernel cfg.
Right.
So basically this line is creating a netlink socket.
This netlink socket should be a global variable so that we can use this netlink socket in this entire
file.
Right.
So we have defined it globally at the top of the file.
So using this netlink socket we will go into send and receive the data with the user space.
Moreover, going forward in the init module, we can simply print the function that netlink socket has
been created successfully.
In fact, you can also check for errors for some reason that if netlink socket creation fails for some
reason, then we can print some other error message.
Right.
It will help us to debug it easier.
If our netlink socket creation fails for some reason, then we will know that what is the reason behind
it?
And always remember that whenever there is a failure of resource allocation to our Linux kernel module,
we should return some error message.
Right.
As long as we are not returning zero, it means that the insertion of our Linux kernel module into the
Linux kernel has been unsuccessful.
Right.
So we need to return some error message.
In this case we are returning no memory because the netlink socket creation has been failed because
of some reason.
Now that in this init function we have learned how to create a netlink socket in the exit function we
should do exactly opposite.
When the user removes the Linux kernel module from the Linux kernel, all the resources that were created
by our Linux kernel module in the Linux kernel space should be released.
Otherwise our Linux kernel module will going to be removed from the Linux kernel.
But the resources that it had created in the Linux kernel would continue to exist in the kernel space,
but there will be no kernel code which will be using those resources.
So this is called kernel resource leaking.
So in this example, the only resource that our Linux kernel module has created is the netlink socket.
And therefore at the time of removing this kernel module from the Linux kernel, we should actually
release or close this netlink socket.
So netlink socket is released or closed using the API.
Netlink kernel release is.
So this API will look after to release and free all the memory that is occupied by this netlink socket.
And you can assign this global variable back to the null.
Right.
So we have learned that in the init module how to create a netlink socket in the netlink socket.
We need to specify the protocol number as well as indirectly.
We have to specify the function which our Linux kernel module will going to invoke when it receives
the data from the userspace.
We will see when we will be writing userspace application that how userspace application sends the data
only to this Linux kernel module and not to any other Linux kernel module.
How userspace application identifies that which linux kernel module it has to communicate with?
Now after that.
Now let us hash include all the required header file, which we need in order to write this Linux kernel
module.
Right.
So Linux slash module dot h is the header file that you must always include if you are writing any type
of Linux kernel module.
Right.
And since we will going to write a code that is related to netlink sockets, all the netlink socket
data structures and APIs are defined in the file Linux slash Netlink dot h.
Right.
And since we have created a socket using socket API in order to make use of the socket API, we need
to have to include the file net slash socket dot h.
And if in the kernel space you want to use any API which is related to string handling, you should
include the header file linux slash string dot h.
Right.
So currently we have not used any string handling API in our Linux kernel module, but shortly we will
going to use one.
Therefore we have hash included this file in advance.
So with this we have completed the step number one, two, three and four.
In the next lecture video we will going to see how our Linux kernel module will going to receive user
space message and process it and reply back to the user space.