WEBVTT

00:06.500 --> 00:06.640
But.

00:07.900 --> 00:09.920
So, guys, I am inside the directory.

00:09.940 --> 00:10.960
Greetings.

00:11.080 --> 00:17.590
And inside this directory I will going to create a new file called greet Netlink lcmc.

00:17.920 --> 00:21.550
So this file will going to represent our linux kernel module.

00:21.580 --> 00:22.270
Right?

00:22.750 --> 00:25.450
So let's begin with absolute scratch.

00:26.900 --> 00:32.820
So in the very first step, we have already learned that we need to write in it and clean up functions.

00:32.840 --> 00:37.250
So in this Linux kernel module, we will going to do nothing different.

00:37.400 --> 00:44.780
We will again going to write the init function which is netlink greetings underscore init followed by

00:44.780 --> 00:47.300
an exit function or clean up function.

00:47.330 --> 00:50.690
Of course you can choose the name anything you want.

00:50.990 --> 00:55.130
And finally you would want to register it with the GPL license.

00:55.760 --> 01:00.020
And of course we need to implement this in IT and exit functions also.

01:01.970 --> 01:09.530
So just going by what we studied in the previous example, when we demonstrated the Linux kernel module,

01:10.430 --> 01:15.740
we will simply print some message which will tell us that our Linux kernel module has been successfully

01:15.740 --> 01:17.600
inserted, right.

01:17.600 --> 01:23.390
And simply we will going to return zero zero means that the insertion of our Linux kernel module is

01:23.390 --> 01:24.320
successful.

01:24.320 --> 01:25.010
Right?

01:26.280 --> 01:33.990
Similarly, we will going to code our exit function and the return type of the exit function is wide.

01:33.990 --> 01:40.050
And here also we will going to simply print some message which will tell us that our Linux kernel module

01:40.050 --> 01:46.620
has been successfully removed from the Linux kernel right now.

01:46.620 --> 01:52.680
As the next step, we need to define somehow that when the user space application will send data to

01:52.680 --> 01:58.200
the kernel space, how our Linux kernel module will going to receive that data, right?

01:58.710 --> 02:05.490
So for that we will going to define a variable of type netlink kernel cfg.

02:05.820 --> 02:06.510
Right.

02:06.540 --> 02:09.900
This data structure is provided by Linux kernel.

02:09.900 --> 02:14.340
And inside this data structure there is a member called input.

02:14.340 --> 02:18.240
So this input member is actually a pointer to the function.

02:18.420 --> 02:19.170
Right?

02:19.170 --> 02:22.590
So we will going to define our own function shortly.

02:22.590 --> 02:25.990
We will going to see what will be the signature of this function.

02:25.990 --> 02:31.540
But for now we will going to assign the pointer to the function to this member of this structure.

02:31.570 --> 02:35.050
Right now, let us see the definition of this structure.

02:36.250 --> 02:39.910
So now you already know how to browse the Linux kernel code.

02:39.910 --> 02:46.960
So in the file Linux slash netlink dot h, you can see that Linux kernel defines the data structure

02:46.960 --> 02:48.640
called Netlink kernel cfg.

02:48.880 --> 02:51.550
It is the same data structure that we are using.

02:51.550 --> 02:52.150
Right?

02:52.150 --> 02:55.780
And inside this data structures there is a member called input.

02:55.780 --> 03:03.430
So you can see that input is actually a pointer to the function which accepts an argument of type struct

03:03.430 --> 03:04.540
as buff.

03:04.540 --> 03:05.260
Right?

03:05.770 --> 03:12.970
So what we have done is that in our Linux module we have taken a function called netlink receive message

03:12.970 --> 03:15.310
function, which we will going to define shortly.

03:15.310 --> 03:21.130
And we have assigned the pointer of this function to this input member of this data structure right

03:21.130 --> 03:21.580
now.

03:21.580 --> 03:23.500
What is the purpose of this function?

03:23.920 --> 03:28.630
This function is meant to receive the data coming from the user space.

03:28.630 --> 03:34.450
So whenever our Linux kernel module, we are going to receive data from the user space, it is this

03:34.450 --> 03:38.170
function which will going to be invoked in the kernel space.

03:39.340 --> 03:42.760
So by now we know what should be the prototype of this function.

03:42.790 --> 03:49.960
The prototype of this function should be same as the data type of this input pointer to the function

03:49.960 --> 03:52.000
defined in this data structure.

03:52.000 --> 03:53.350
Netlink kernel cfg.

03:53.380 --> 03:53.970
Right.

03:53.980 --> 04:01.510
So basically this netlink kernel cfg data structure allows us to define various parameters of our netlink

04:01.510 --> 04:03.660
communication in the kernel space.

04:03.670 --> 04:10.300
For now we are using this data structure to define only the function which we are going to receive the

04:10.300 --> 04:12.160
data from the user space.

04:13.690 --> 04:18.970
So you can see as soon as we will going to receive the data from the user space, it is this function

04:18.970 --> 04:20.590
which will going to invoke.

04:20.830 --> 04:26.740
Now the next thing that we need to do is to tell the Linux kernel that hey, Linux kernel, whenever

04:26.740 --> 04:32.980
we receive the data from the user space, please invoke our function Netlink receive message function.

04:32.980 --> 04:38.840
So for that we will going to do something in our netlink Greetings init module.

04:38.840 --> 04:39.530
Right?

04:41.000 --> 04:46.910
So as soon as we have inserted our Linux kernel module, our Linux kernel module must create a netlink

04:46.910 --> 04:47.630
socket.

04:47.720 --> 04:53.720
That Netlink socket defines that our Linux kernel module will going to receive what type of data from

04:53.720 --> 04:54.920
the user space.

04:55.250 --> 04:56.000
Right.

04:57.660 --> 05:02.130
So netlink socket is created using an netlink kernel create.

05:02.160 --> 05:02.740
Right.

05:02.760 --> 05:08.910
The first argument of this API is actually a global variable which is provided by the kernel.

05:08.940 --> 05:11.750
This global variable is init net.

05:11.760 --> 05:12.360
Right?

05:12.360 --> 05:17.110
And you can see that we are simply passing the address of this global variable.

05:17.130 --> 05:26.460
So basically init net is nothing, but it actually represents the complete networking subsystem which

05:26.460 --> 05:28.710
is running in the kernel space.

05:29.970 --> 05:35.160
The second argument to this API is our netlink protocol number.

05:36.420 --> 05:42.900
Now, we have already discussed that because we are writing our own new Linux kernel subsystem.

05:42.900 --> 05:49.050
Therefore, in order to set up the netlink based communication with the user space, we need to have

05:49.380 --> 05:50.700
some protocol number.

05:50.700 --> 05:51.240
Right?

05:51.240 --> 05:52.920
So this is Netlink test.

05:52.920 --> 06:00.000
Protocol is a protocol number which we will going to have to define it so we can define its value as

06:00.000 --> 06:02.010
31, right?

06:02.010 --> 06:08.400
So it's an unused value because all other values are being used by some other netlink protocols.

06:08.400 --> 06:13.530
And the third argument of this API is the configuration parameter which we have defined above.

06:13.560 --> 06:15.330
That is netlink kernel cfg.

06:15.600 --> 06:16.320
Right.

06:16.320 --> 06:20.430
So basically this line is creating a netlink socket.

06:20.460 --> 06:27.390
This netlink socket should be a global variable so that we can use this netlink socket in this entire

06:27.390 --> 06:28.050
file.

06:28.050 --> 06:28.650
Right.

06:28.650 --> 06:31.770
So we have defined it globally at the top of the file.

06:32.910 --> 06:38.140
So using this netlink socket we will go into send and receive the data with the user space.

06:38.770 --> 06:45.130
Moreover, going forward in the init module, we can simply print the function that netlink socket has

06:45.130 --> 06:46.690
been created successfully.

06:47.470 --> 06:54.040
In fact, you can also check for errors for some reason that if netlink socket creation fails for some

06:54.040 --> 06:57.850
reason, then we can print some other error message.

06:57.880 --> 06:58.660
Right.

06:58.660 --> 07:01.660
It will help us to debug it easier.

07:01.660 --> 07:07.810
If our netlink socket creation fails for some reason, then we will know that what is the reason behind

07:07.810 --> 07:08.350
it?

07:08.350 --> 07:14.440
And always remember that whenever there is a failure of resource allocation to our Linux kernel module,

07:14.440 --> 07:17.020
we should return some error message.

07:17.020 --> 07:17.800
Right.

07:17.950 --> 07:24.790
As long as we are not returning zero, it means that the insertion of our Linux kernel module into the

07:24.790 --> 07:27.400
Linux kernel has been unsuccessful.

07:27.400 --> 07:28.120
Right.

07:28.300 --> 07:30.820
So we need to return some error message.

07:30.820 --> 07:36.760
In this case we are returning no memory because the netlink socket creation has been failed because

07:36.760 --> 07:37.690
of some reason.

07:40.010 --> 07:46.790
Now that in this init function we have learned how to create a netlink socket in the exit function we

07:46.790 --> 07:48.620
should do exactly opposite.

07:48.980 --> 07:54.890
When the user removes the Linux kernel module from the Linux kernel, all the resources that were created

07:54.890 --> 07:59.180
by our Linux kernel module in the Linux kernel space should be released.

07:59.210 --> 08:04.270
Otherwise our Linux kernel module will going to be removed from the Linux kernel.

08:04.280 --> 08:10.520
But the resources that it had created in the Linux kernel would continue to exist in the kernel space,

08:10.520 --> 08:15.440
but there will be no kernel code which will be using those resources.

08:15.440 --> 08:18.650
So this is called kernel resource leaking.

08:19.430 --> 08:25.250
So in this example, the only resource that our Linux kernel module has created is the netlink socket.

08:25.250 --> 08:31.370
And therefore at the time of removing this kernel module from the Linux kernel, we should actually

08:31.370 --> 08:33.860
release or close this netlink socket.

08:33.860 --> 08:37.550
So netlink socket is released or closed using the API.

08:37.580 --> 08:39.670
Netlink kernel release is.

08:40.430 --> 08:46.460
So this API will look after to release and free all the memory that is occupied by this netlink socket.

08:46.460 --> 08:50.270
And you can assign this global variable back to the null.

08:51.500 --> 08:52.390
Right.

08:52.400 --> 08:58.160
So we have learned that in the init module how to create a netlink socket in the netlink socket.

08:58.190 --> 09:02.120
We need to specify the protocol number as well as indirectly.

09:02.120 --> 09:09.140
We have to specify the function which our Linux kernel module will going to invoke when it receives

09:09.140 --> 09:10.830
the data from the userspace.

09:10.850 --> 09:17.240
We will see when we will be writing userspace application that how userspace application sends the data

09:17.240 --> 09:22.040
only to this Linux kernel module and not to any other Linux kernel module.

09:22.370 --> 09:28.190
How userspace application identifies that which linux kernel module it has to communicate with?

09:29.370 --> 09:30.650
Now after that.

09:30.660 --> 09:37.380
Now let us hash include all the required header file, which we need in order to write this Linux kernel

09:37.380 --> 09:38.010
module.

09:38.040 --> 09:38.670
Right.

09:38.670 --> 09:45.420
So Linux slash module dot h is the header file that you must always include if you are writing any type

09:45.420 --> 09:46.760
of Linux kernel module.

09:46.770 --> 09:47.370
Right.

09:47.370 --> 09:53.010
And since we will going to write a code that is related to netlink sockets, all the netlink socket

09:53.010 --> 09:58.500
data structures and APIs are defined in the file Linux slash Netlink dot h.

09:58.500 --> 09:59.220
Right.

09:59.220 --> 10:06.210
And since we have created a socket using socket API in order to make use of the socket API, we need

10:06.210 --> 10:09.930
to have to include the file net slash socket dot h.

10:10.470 --> 10:16.740
And if in the kernel space you want to use any API which is related to string handling, you should

10:16.740 --> 10:21.030
include the header file linux slash string dot h.

10:21.030 --> 10:21.630
Right.

10:21.630 --> 10:28.230
So currently we have not used any string handling API in our Linux kernel module, but shortly we will

10:28.230 --> 10:29.590
going to use one.

10:29.590 --> 10:32.980
Therefore we have hash included this file in advance.

10:33.460 --> 10:37.690
So with this we have completed the step number one, two, three and four.

10:37.690 --> 10:44.200
In the next lecture video we will going to see how our Linux kernel module will going to receive user

10:44.200 --> 10:48.580
space message and process it and reply back to the user space.
