I know that in the linux kernel we can add our own protocol at the transport layer, similar to TCP, UDP etc.
Are there any hooks to register a new protocol, at the network layer, similar to IP, ARP, which could transfer the packets to the application and how to add this protocol in the linux kernel?
To implement the protocol, write a kernel module.
The module should create a new device in
/dev
. The application can then useioctl()
to talk to your module to specify things like target host, options, etc.See Chapter 7 of The Linux Kernel Module Programming Guide for details how to implement
ioctl()
in a kernel module.This blog post also seems like a good introduction to the topic.
To handle communication from userspace to your protocol, register your protocol with the kernel sockets API. This allows you to create a normal socket from userspace.
Have a look at the bluetooth/RFCOM socket implementation for relevant code samples.
To register a protocol you will have to fill the proto_ops structure. This structure follows the object oriented pattern observed elsewhere inside the kernel. This structure defines an interface to follow for developers implementing their own socket interface.
Implement the functions the interface defines such as bind, connect, listen, and assign the function pointer to the structure entry. Define ioctl's for functionality not covered by the operations interface.
You end up with a structure that later you embed at the socket struct we return from the create function.
Struct net_proto_family defines a new protocol family. This structure includes the create function where your function implementation should populate a socket struct filled with the proto_ops struct.
After that register the family with sock_register, and if everything is ok you should be able to create a proper socket from userspace.
Internally the protocol should probably use skbuffs1,[2],[3](pdf). to communicate with the networking devices.
skbuffs are the universal way of handling network packets in the linux kernel. The packets are received by the network card, put into some skbuffs and then passed to the network stack, which uses the skbuff all the time.
This is the basic data structure and io path to implement a networking protocol inside the linux kernel.
I am not aware of a document that describes this procedure from start to finish. The source is with you on this one.