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?
TCP/IP — The Protocol Used by Linux. Linux and other Unix operating systems use the TCP/IP protocol. It is not a single network protocol, but a family of network protocols that offer various services.
Network communication is accomplished via read / write or recv / send calls for TCP sockets and recvfrom / sendto for UDP sockets. Transmission and reception operations are transparent to the application, leaving encapsulation and transmission over network at the kernel's discretion.
In the Linux operating system, the network stack is useful to communicate the application with the physical network devices. The network stack is divided into multiple layers. There are different network layers. The same different roles are playing their individual role.
TCP happens in the kernel.
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.
static const struct proto_ops rfcomm_sock_ops = {
.family = PF_BLUETOOTH,
.owner = THIS_MODULE,
.bind = rfcomm_sock_bind,
.connect = rfcomm_sock_connect,
.listen = rfcomm_sock_listen,
.
.
.
.accept = rfcomm_sock_accept,
};
static const struct net_proto_family rfcomm_sock_family_ops = {
.family = PF_BLUETOOTH,
.owner = THIS_MODULE,
.create = rfcomm_sock_create
};
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 skbuffs (see here, and here) 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With